LNMP build, Nginx integration PHP-FPM namely FASTCGI implementation, and Nginx multiple virtual host configuration

Source: Internet
Author: User
Tags chmod fpm install php localhost mysql mcrypt snmp

PHP Programmer One, this environment is a personal note bar, posted out


Environment:

CentOS 5.5

The mirror address will not be posted.

php-5.4.20

Address: http://museum.php.net/php5/php-5.4.20.tar.gz

Mysql-5.. 5.48 (Universal binary format)

Address: http://dev.mysql.com/get/Downloads/MySQL-5.5/mysql-5.5.48-linux2.6-i686.tar.gz

nginx-1.8.1

Address: http://nginx.org/download/nginx-1.8.1.tar.gz


First, compile and install Nginx:


1, solve the dependency relationship


Compiling the installation Nginx requires prior installation of the development Pack group "Development Tools" and "Development Libraries".
At the same time, you also need to install Pcre-devel packages specifically:
# yum-y Install Pcre-devel


2, installation


First, you add the user Nginx to run the Nginx service process:
# groupadd-r Nginx
# Useradd-r-G Nginx nginx


Then start compiling and installing:
#./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/\
--HTTP-UWSGI-TEMP-PATH=/VAR/TMP/NGINX/UWSGI \
--HTTP-SCGI-TEMP-PATH=/VAR/TMP/NGINX/SCGI \
--with-pcre
# Make && make install


Note: If you want to use the Nginx Perl module, you can do this by adding the--with-http_perl_module option for the Configure script, but the module is still in the experimental use phase and may be unexpected in operation, so The way it is implemented is not introduced here. If you want to use nginx CGI functionality, can also be based on fcgi, the implementation of the method, please refer to the documentation on the web.


3. Provide SYSV init script for Nginx:


Create a new file/etc/init.d/nginx, which reads as follows:


#!/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


And then give execution permission to this script:


# chmod +x/etc/init.d/nginx


Add to service Management list and let it start automatically:
# chkconfig--add Nginx
# chkconfig Nginx on


You can then start the service and test:

# service Nginx Start


If you see the Nginx Welcome page, the installation is successful.


Second, the Universal binary format to install MySQL


Note: MySQL installed in universal binary format must be installed under/usr/local/and the directory name must be MySQL


Tar XF mysql-5.5.48-linux2.6-i686.tar.gz-c/usr/local/uncompressed to/usr/local

cd/usr/local/

LN-SV mysql-5.5.48 MySQL [link file without changing the original, and following the official MySQL regulations]

If there are no MySQL users and MySQL groups, you need to create:

Groupadd-r-G 306 MySQL

Useradd-r-G 306 MySQL

To change the owner group of a program:

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

Attention

If MySQL's default data directory is still in the program directory, you must be a MySQL user and MySQL group
So we need to change it back.
The other main operation after the completion of the need to be changed to root, this method is a security practice, to prevent malicious attack on the MySQL process, and gain malicious permission to operate

Drwxr-xr-x 2 root mysql 4096 07:01 bin
-rw-r--r--1 root MySQL 17987 07:01 copying
Drwxr-xr-x 5MySQL MySQL4096 Mar 13 07:20Data
Drwxr-xr-x 2 root mysql 4096 07:01 docs
Drwxr-xr-x 3 root mysql 4096 07:01 include
-rw-r--r--1 root mysql 151708 07:01 install-binary
Drwxr-xr-x 3 root MySQL 4096 07:01 Lib
Drwxr-xr-x 4 root mysql 4096 07:01 man
Drwxr-xr-x root mysql 4096 Mar 07:02 mysql-test
-rw-r--r--1 root MySQL 2496 07:00 README
Drwxr-xr-x 2 root mysql 4096 07:01 scripts
Drwxr-xr-x root mysql 4096 Mar 07:01 share
Drwxr-xr-x 4 root mysql 4096 07:01 sql-bench
Drwxr-xr-x 2 root mysql 4096 07:00 support-files

Switch to program directory

Cd/usr/local/mysql


scripts/mysql_install_db--basedir=/usr/local/mysql--user=mysql

CP Support-files/mysql.server/etc/init.d/mysqld

# Join SYSV


Chkconfig--add mysqld


# Boot Boot


[Root@localhost mysql]# chkconfig mysqld on

# Copy configuration file


[Root@localhost mysql]# CP support-files/my-large.cnf/etc/my.cnf


[Note]: If the data directory specifies a different directory, you need to add a configuration item to the/ETC/MY.CNF


Datadir=/mydata/data (for example) or MySQL can not start, the landlord is the default program directory of data, that is, do not have to modify

6, start the service


[root@localhost etc]# Service mysqld start

7. Add Local Environment variables

Vim/etc/profile.d/mysqld

Content: Export path= $PATH:/usr/local/mysql/bin


To use the MySQL installation to conform to the system usage specification and export its development components to the system, the following steps are also required:


6, the output of the man manual mysql to the man Command search path:


Edit/etc/man.config, add the following line:
Manpath/usr/local/mysql/man


7, output MySQL header file to the System header file path/usr/include:

This can be achieved by simply creating a link:
# Ln-sv/usr/local/mysql/include/usr/include/mysql

8, output MySQL library files to the system library to find the path:

# echo '/usr/local/mysql/lib ' >/etc/ld.so.conf.d/mysql.conf

And then let the system reload into the system library:
# Ldconfig

Log in again:


run successfully.


Ii. Compiling and installing PHP5.4


1, solve the dependency relationship:

Please configure the Yum source (which can be the local system CD) and execute the following command:


# yum-y Groupinstall "X Software Development"

If you want to allow compiled PHP to support MCrypt, Mhash extensions, and libevent, here are a few RPM packages to install:
libmcrypt-2.5.8-4.el5.centos.i386.rpm
libmcrypt-devel-2.5.8-4.el5.centos.i386.rpm
mhash-0.9.9-1.el5.centos.i386.rpm
mhash-devel-0.9.9-1.el5.centos.i386.rpm
mcrypt-2.6.8-1.el5.i386.rpm


It is best to install the RPM package above using the upgrade method, which is as follows:
# RPM-UVH

In addition, can also be installed according to the needs of libevent, the system will generally bring libevent, but the version is somewhat low. So you can upgrade the installation, which contains the following two RPM packages.
libevent-2.0.17-2.i386.rpm
libevent-devel-2.0.17-2.i386.rpm

Description: Libevent is an asynchronous event notification library file whose APIs provide a mechanism for performing callback functions when an event occurs on a file description or at a time-out, and it is primarily used to replace the event loop mechanism on events-driven network servers. For now, Libevent supports the event epoll of/dev/poll, Kqueue, select, poll, ports, and Solaris.


2. Compile and install php-5.4

# Tar XF php-5.4.20.tar.gz
# CD php-5.4.20
#./configure \
--prefix=/usr/local/php \
--with-mysql=/usr/local/mysql \
--WITH-OPENSSL \
--ENABLE-FPM \
--enable-sockets \
--ENABLE-SYSVSHM \
--with-mysqli=/usr/local/mysql/bin/mysql_config \
--enable-mbstring \
--with-freetype-dir \
--with-jpeg-dir \
--with-png-dir \
--with-zlib-dir \
--WITH-LIBXML-DIR=/USR \
--enable-xml \
--with-mhash \
--with-mcrypt \
--WITH-CONFIG-FILE-PATH=/ETC \
--WITH-CONFIG-FILE-SCAN-DIR=/ETC/PHP.D \
--WITH-BZ2 \
--with-curl

Note: If you install the MCrypt related two RPM packages in the previous 1th step, this./configure command can also bring--with-mcrypt options to allow PHP to support mycrpt extensions. The--WITH-SNMP option is used to implement SNMP extensions for PHP, but this feature requires that NET-SNMP-related packages be installed in advance.

# make
# Make Test
# Make Intall


To provide the configuration file for PHP:
# CP Php.ini-production/etc/php.ini

Provide the SYSV init script for php-fpm and add it to the list of services:
# CP SAPI/FPM/INIT.D.PHP-FPM/ETC/INIT.D/PHP-FPM
# chmod +X/ETC/INIT.D/PHP-FPM
# chkconfig--add php-fpm
# Chkconfig PHP-FPM on


To provide a configuration file for php-fpm:
# cp/usr/local/php/etc/php-fpm.conf.default/usr/local/php/etc/php-fpm.conf

To edit a PHP-FPM profile:
# vim/usr/local/php/etc/php-fpm.conf
Configure FPM options for the values you need, and enable the PID file (the last line below):
Pm.max_children = 150
Pm.start_servers = 8
Pm.min_spare_servers = 5
Pm.max_spare_servers = 10
#pid =/usr/local/php/var/run/php-fpm.pid (do not edit this to default)

The next step is to start the PHP-FPM:
# service PHP-FPM Start

Use the following command to check for positive (if the command output has several PHP-FPM processes to indicate that the startup succeeded):
# PS aux | grep php-fpm

3. Integration of Nginx and PHP5


1, edit/etc/nginx/nginx.conf, enable the following options:


Location ~ \.php$ {
root HTML;
Fastcgi_pass 127.0.0.1:9000;
Fastcgi_index index.php;
Fastcgi_param Script_filename/scripts$fastcgi_script_name;
Include Fastcgi_params;
}


2, edit the/etc/nginx/fastcgi_params, change its contents to the following content:
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;


and add the PHP-formatted home page in the supported home page format, similar to the following:
Location/{
root HTML;
Index index.php index.html index.htm;
}

Then reload the Nginx profile:
# service Nginx Reload


3, in/usr/html New index.php test page, to test whether PHP can work correctly:
# vim/usr/local/html/index.php
<?php
Phpinfo ();
?>
The test page can then be accessed through the browser.


Four, configure the nginx of the virtual host, and all support PHP


① Backup of the main configuration file

Cp/etc/nginx/nginx.conf/etc/nginx/nginx.conf.bak

Cd/etc/nginx

② create additional configuration file directory

mkdir Vohost

③ Modify Configuration Items

Vim nginx.conf


Add a row to read the default configuration file

For example, define a www_test_com.conf under vhost/as a new domain based www.test.com virtual host

Vim vhost/www_test_com.conf

The contents are as follows:

server{
Listen 80;
server_name www.test.com;
CharSet Utf-8;
Index index.php index.html index.htm;
AutoIndex on;
Location ~ \.php$ {
root/var/site/www.test.com;
Fastcgi_pass 127.0.0.1:9000;
Fastcgi_index index.php;
Fastcgi_param Script_filename/www$fastcgi_script_name;
Include Fastcgi_params;
}
}

SERVER_NAME: Website name

Root: Site root directory


If you are installing LNMP into a virtual machine, don't forget to ifconfig it, and then add the IP and domain name (www.test.com) to the Hosts file, and check that the Linux firewall is in the off state, OK ...


The first time, the format is very messy ...

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.