Compile and install LNMP (linux+nginx+mysql+php) environment detailed

Source: Internet
Author: User
Tags chmod fpm install php openssl readline sleep socket vps

Description

PHP in compiling the installation, Nginx to be able to invoke PHP to provide dynamic PHP format of the Web page, must be implemented with FASTCGI, but fastcgi is just a framework to achieve FASTCGI framework PHP-FPM, But for the 5.2.x version of PHP, the default is not supported PHP-FPM, need to play a php-fpm patch, for 5.3.2 before the version is also need to play patches, and the process of patching is more troublesome. Fortunately, the 5.3.3 version of the PHP-FPM is directly into the source code package, when compiling the installation only need to enable the PHP-FPM function.

However, if you want to use PHP-FPM, you also need to provide the following features:

You need to provide documents that can parse XML format, and you need to install both LIBXML2 and libxml2-devel packages, which are installed by default after the two packages are installed in the development environment.

You need to install Libevent and after 1.4.12 version, unfortunately rhel5.4 version of this package is before 1.4.12, you need to compile the package from a new manual compilation.

Libiconv is used to provide a functional component of network connectivity that enables faster network access, which is not installed on the component system and requires manual compilation.

To build a compilation environment:

Yum-y install gcc openssl-devel zlib-devel pcre-devel
Yum Groupinstall "developement Tools" "Development Libraries"-yt
First install Nginx:
wget http://nginx.org/download/nginx-1.0.14.tar.gz
Tar zxvf nginx-1.0.14.tar.gz #
Useradd-s/sbin/nologin-m Nginx
CD nginx-1.0.14

./configure \
--PREFIX=/USR \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/tmp/nginx/client/\
--http-proxy-temp-path=/var/tmp/nginx/proxy/\
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/

Make && make install

To create a Nginx startup script:

Vim/etc/init.d/nginxd


#!/bin/sh
#
# Nginx-this script starts and stops the Nginx daemon
#
# Chkconfig:-85 15
# Description:nginx is a HTTP (s) server, HTTP (s) reverse \
# Proxy and IMAP/POP3 proxy server
# Processname:nginx
# config:/etc/nginx/nginx.conf
# config:/etc/sysconfig/nginx
# Pidfile:/var/run/nginx.pid

# Source function library.
. /etc/rc.d/init.d/functions

# Source Networking configuration.
. /etc/sysconfig/network

# Check that networking are up.
["$NETWORKING" = "no"] && exit 0

nginx= "/usr/sbin/nginx"
prog=$ (basename $nginx)

Nginx_conf_file= "/etc/nginx/nginx.conf"

[-f/etc/sysconfig/nginx] &&. /etc/sysconfig/nginx

Lockfile=/var/lock/subsys/nginx

Make_dirs () {
# Make required Directories
User= ' Nginx-v 2>&1 | grep "Configure arguments:" | Sed ' s/[^*]*--user=\ ([^]*\). */\1/g '-'
Options= ' $nginx-v 2>&1 | grep ' Configure arguments: '
for opt in $options; Todo
If [' Echo $opt | grep '. *-temp-path ']; Then
Value= ' echo $opt | Cut-d "=" F 2 '
if [!-D "$value"]; Then
# echo "Creating" $value
Mkdir-p $value && chown-r $user $value
Fi
Fi
Done
}

Start () {
[x $nginx] | | Exit 5
[f $NGINX _conf_file] | | Exit 6
Make_dirs
Echo-n $ "Starting $prog:"
Daemon $nginx-C $NGINX _conf_file
Retval=$?
Echo
[$retval-eq 0] && Touch $lockfile
Return $retval
}

Stop () {
Echo-n $ "Stopping $prog:"
Killproc $prog-quit
Retval=$?
Echo
[$retval-eq 0] && rm-f $lockfile
Return $retval
}

Restart () {
Configtest | | Return $?
Stop
Sleep 1
Start
}

Reload () {
Configtest | | Return $?
Echo-n $ "Reloading $prog:"
Killproc $nginx-hup
Retval=$?
Echo
}

Force_reload () {
Restart
}

Configtest () {
$nginx-T-C $NGINX _conf_file
}

Rh_status () {
Status $prog
}

Rh_status_q () {
Rh_status >/dev/null 2>&1
}

Case "$" in
Start
Rh_status_q && Exit 0
$
;;
Stop
Rh_status_q | | Exit 0
$
;;
Restart|configtest)
$
;;
Reload
Rh_status_q | | Exit 7
$
;;
Force-reload)
Force_reload
;;
Status
Rh_status
;;
Condrestart|try-restart)
Rh_status_q | | Exit 0
;;
*)
echo $ "Usage: $ {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
Exit 2
Esac
chmod +x/etc/init.d/nginxd #给予脚本执行权限
Chkconfig--add nginxd #加入开机启动选项中
Chkconfig Nginxd on #设置开机自动启动
Service Nginxd Start #启动nginx Services

Test access:

Install MySQL:

Download:

wget http://mysql.mirrors.hoobly.com/Downloads/MySQL-5.5/mysql-5.5.22.tar.gz
Tar zxvf mysql-5.5.22.tar.gz
CD mysql-5.5.22-linux2.6-i686
/usr/sbin/groupadd MySQL #添加mysql用户
/usr/sbin/useradd-g MySQL MySQL #添加mysql组

Compile:

./configure--prefix=/usr/local/mysql/--enable-assembler--with-extra-charsets=complex-- Enable-thread-safe-client--with-big-tables--with-readline--with-ssl--with-embedded-server--enable-local-infile --with-plugins=partition,innobase,myisammrg
Make&&make Install

An error occurred at compile time:

.. /depcomp:line 571:exec:g++: Not Found
MAKE[1]: * * * [MY_NEW.O] Error 127
MAKE[1]: Leaving directory '/root/lnmpsrc/mysql-5.1.62/mysys '
Make: * * * [all-recursive] Error 1

View which package the g++ belongs to on other servers that install g++:

[Root@vps ~]# Find/-name g++
/usr/bin/g++
[Root@vps ~]# rpm-qf/usr/bin/g++
gcc-c++-4.4.6-3.el6.i686

You can see g++ belongs to gcc-c++ package

Install gcc-c++:

[Root@vps ~] #yum Install gcc-c++-y

Recompile:

./configure--prefix=/usr/local/mysql/--enable-assembler--with-extra-charsets=complex-- Enable-thread-safe-client--with-big-tables--with-readline--with-ssl--with-embedded-server--enable-local-infile --with-plugins=partition,innobase,myisammrg
Make&&make Install

Change/usr/local/mysql directory Users and Groups:

Chown-r Mysql.mysql/usr/local/mysql

Create MySQL database, log storage directory

Mkdir/mysql/{data,binlog,relaylog}-P
Chown-r Mysql.mysql/mysql

Set up a data table with MySQL account:

/usr/local/mysql/bin/mysql_install_db--basedir=/usr/local/mysql--datadir=/mysql/data--user=mysql

To create a MySQL configuration file:

Vim/mysql/my.cnf


[Client]

Character-set-server = UTF8
Port = 3306
Socket =/tmp/mysql.sock

[Mysqld]
Character-set-server = UTF8
replicate-ignore-db = MySQL
REPLICATE-IGNORE-DB = Test
Replicate-ignore-db = Information_schema
user = MySQL
Port = 3306
Socket =/tmp/mysql.sock
Basedir =/usr/local/mysql
DataDir =/mysql/data
Log-error =/mysql/mysql_error.log
Pid-file =/mysql/mysql.pid
Open_files_limit = 10240
Back_log = 600
Max_connections = 5000
Max_connect_errors = 6000
Table_cache = 614
external-locking = FALSE
Max_allowed_packet = 32M
Sort_buffer_size = 1M
Join_buffer_size = 1M
Thread_cache_size = 300
#thread_concurrency = 8
Query_cache_size = 512M
Query_cache_limit = 2M
Query_cache_min_res_unit = 2k
Default-storage-engine = MyISAM
Thread_stack = 192K
Transaction_isolation = read-committed
Tmp_table_size = 246M
Max_heap_table_size = 246M
Long_query_time = 3
Log-slave-updates
Log-bin =/mysql/data/binlog
Binlog_cache_size = 4M
Binlog_format = MIXED
Max_binlog_cache_size = 8M
Max_binlog_size = 1G
Relay-log-index =/mysql/relaylog/relaylog
Relay-log-info-file =/mysql/relaylog/relaylog
Relay-log =/mysql/relaylog/relaylog
Expire_logs_days = 30
Key_buffer_size = 256M
Read_buffer_size = 1M
Read_rnd_buffer_size = 16M
Bulk_insert_buffer_size = 64M
Myisam_sort_buffer_size = 128M
Myisam_max_sort_file_size = 10G
Myisam_repair_threads = 1
Myisam_recover

Interactive_timeout = 120
Wait_timeout = 120

Skip-name-resolve
#master-connect-retry = 10
Slave-skip-errors = 1032,1062,126,1114,1146,1048,1396

#master-host = 192.168.1.1
#master-user = Username
#master-password = password
#master-port = 3306

Server-id = 1

Innodb_additional_mem_pool_size = 16M
Innodb_buffer_pool_size = 512M
Innodb_data_file_path = Ibdata1:256m:autoextend
Innodb_file_io_threads = 4
Innodb_thread_concurrency = 8
Innodb_flush_log_at_trx_commit = 2
Innodb_log_buffer_size = 16M
Innodb_log_file_size = 128M
Innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
Innodb_lock_wait_timeout = 120
innodb_file_per_table = 0

#log-slow-queries =/mysql/slow.log
#long_query_time = 10

[Mysqldump]
Quick
Max_allowed_packet = 32M

Managing MySQL Scripts:

Vim/mysql/mysqld


#!/bin/sh
mysql_port=3306
Mysql_username= "Admin" #帐号密码可以自行创建
Mysql_password= "Rootisnosafe"

Function_start_mysql ()
{
printf "Starting mysql...\n"
/bin/sh/usr/local/mysql/bin/mysqld_safe--defaults-file=/mysql/my.cnf 2>&1 > /dev/null &
}

Function_stop_mysql ()
{
printf "stoping mysql...\n"
/usr/local/mysql/bin/mysqladmin-u ${mysql_username}-p${mysql_password}-s/tmp/mysql.sock shutdown
}

Function_restart_mysql ()
{
printf "Restarting mysql...\n"
Function_stop_mysql
Sleep 5
Function_start_mysql
}

Function_kill_mysql ()
{
Kill-9 $ (Ps-ef | grep ' Bin/mysqld_safe ' | grep ${mysql_port} | awk ' {printf $} ')
Kill-9 $ (Ps-ef | grep ' Libexec/mysqld ' | grep ${mysql_port} | awk ' {printf $} ')
}

If ["$" = "start"]; Then
Function_start_mysql
elif ["$" = "Stop"]; Then
Function_stop_mysql
elif ["$" = "restart"]; Then
Function_restart_mysql
elif ["$" = "kill"]; Then
Function_kill_mysql
Else
printf "Usage:/mysql/mysqld {start|stop|restart|kill}\n"
Fi

To give script Execute permissions:

chmod +x/mysql/mysqld

Start MySQL

/mysql/mysqld start

Command line management MySQL:

/usr/local/mysql/bin/mysql-u root-p-s/tmp/mysql.sock
Create a user with root permissions: admin, password is Rootisnosafe
GRANT all privileges in *.* to ' admin ' @ ' localhost ' identified by ' Rootisnosafe ';
GRANT all privileges in *.* to ' admin ' @ ' 127.0.0.1 ' identified by ' Rootisnosafe ';


Install PHP:


First install libevent and Libiconv:
wget https://github.com/downloads/libevent/libevent/libevent-1.4.14b-stable.tar.gz
Tar zxvf libevent-1.4.14b-stable.tar.gz
CD libevent-1.4.14b-stable
./configure&&make&&make Install

wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz
Tar zxvf libiconv-1.14.tar.gz
CD libiconv-1.14
./configure
Make
Make install
Ln-sf/usr/local/lib/libiconv.so.2/usr/lib/libiconv.so.2


Now install PHP:


wget http://cn.php.net/distributions/php-5.4.0.tar.gz
Tar zxvf php-5.4.0.tar.gz
CD php-5.4.0

./configure--prefix=/usr/local/php--with-mysql=/usr/local/mysql--with-mysqli=/usr/local/mysql/bin/mysql_config --with-openssl--enable-fpm--enable-mbstring--with-freetype-dir--with-jpeg-dir--with-png-dir-- WITH-LIBXML-DIR=/USR--enable-xml--with-iconv-dir=/usr/local

The directory for Make zend_extra_libs= '-liconv ' #因为-liconv is not under/usr/local, so it needs to be manually specified when installing
Make install
which
The path to –with-mysql and –with-mysqli is your MySQL specific directory
–ENABLE-FPM start FPM. Others are basic options, easy to understand

CP Php.ini-production/usr/local/php/etc/php.ini

To modify a configuration file:

Vim/usr/local/php/etc/php-fpm.conf


Pm.max_children = 50
Pm.start_servers = 10
Pm.min_spare_servers = 5
Pm.max_spare_servers = 35
Start:
/USR/LOCAL/PHP/SBIN/PHP-FPM &
Check for normal Startup:
NETSTAT-TUNLP|GREP 9000
TCP 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 7826/php-fpm
Add/USR/LOCAL/PHP/SBIN/PHP-FPM & to Rc.local:
Echo '/USR/LOCAL/PHP/SBIN/PHP-FPM & ' >>/etc/rc.local
To configure the Fastcgi_params file:
Vim/etc/nginx/fastcgi_params

To replace the content with:


Fastcgi_param Gateway_interface cgi/1.1;
Fastcgi_param Server_software Nginx;
Fastcgi_param query_string $query _string;
Fastcgi_param Request_method $request _method;
Fastcgi_param Content_Type $content _type;
Fastcgi_param content_length $content _length;
Fastcgi_param script_filename $document _root$fastcgi_script_name;
Fastcgi_param script_name $fastcgi _script_name;
Fastcgi_param Request_uri $request _uri;
Fastcgi_param Document_uri $document _uri;
Fastcgi_param document_root $document _root;
Fastcgi_param server_protocol $server _protocol;
Fastcgi_param remote_addr $remote _addr;
Fastcgi_param Remote_port $remote _port;
Fastcgi_param server_addr $server _addr;
Fastcgi_param server_port $server _port;
Fastcgi_param server_name $server _name;
# PHP only, required if PHP is built with--enable-force-cgi-redirect
Fastcgi_param Redirect_status 200;

Last modified nginx.conf configuration file:


Vim/etc/nginx/nginx.conf
Location ~ \.php$ {
root/www;
Fastcgi_pass 127.0.0.1:9000;
Fastcgi_index index.php;
Fastcgi_param Script_filename/scripts$fastcgi_script_name;
Include Fastcgi_params;
}

Location/{
root/www;
Index index.php index.html index.htm;
}


Save exit.

Start Nginx:


Service Nginxd Start

Edit/www/index.php:


<?php
Phpinfo ();
?>

Access test:


To test the database connection:

Edit:/www/index.php
 
<?php
        $link =mysql_connect (" LocalHost "," admin "," Rootisnosafe ");
        if ($link) echo "OK";
        Else echo "FAIL";

Refreshes the access. The
If the OK Word appears, the connection is normal.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.