Install and configure php-fpm in nginx

Source: Internet
Author: User
Tags bz2 fpm install php mysql client openssl php file php and php source code


PHP-fpm has been finalized since php 5.4 RC2 and is no longer labeled as EXPERIMENTAL (EXPERIMENTAL stuff) [2-3] By the php team.
Compared with Spawn-FCGI, PHP-FPM has better control over CPU and memory, and the former is easy to crash and must be monitored with crontab, while PHP-FPM has no such troubles.
PHP5.3.3 has already integrated php-fpm and is no longer a third-party package. PHP-FPM provides a better PHP process management method, can effectively control the memory and process, can smoothly load PHP configuration, than spawn-fcgi has more advantages, so it is officially included by PHP. You can enable PHP-fpm with The-enable-FPM parameter in./configure.

Nginx itself cannot process PHP. It is only a web server. After receiving the request, if it is a php request, it is sent to the php interpreter for processing and the result is returned to the client.

Nginx generally sends requests to the fastcgi management process. The fascgi management process selects the cgi sub-process to process the results and returns the results

This document uses php-fpm as an example to describe how to enable nginx to support PHP

I. Compile and install php-fpm

What is PHP-FPM

PHP-FPM is a PHP FastCGI manager that is only used for PHP and can be downloaded at http://php-fpm.org/download.

PHP-FPM is actually a patch of PHP source code, designed to integrate FastCGI process management into the PHP package. You must patch it to your PHP source code before using it after compiling and installing PHP.

The new version of PHP has already integrated php-fpm and is no longer a third-party package. We recommend that you use it. PHP-FPM provides a better PHP process management method, can effectively control the memory and process, can smoothly load PHP configuration, than spawn-fcgi has more advantages, so it is officially included by PHP. In./configure with The-enable-fpm parameter to open the PHP-FPM, other parameters are configured php, the specific meaning of the option can be viewed here.

Preparations before installation
Run in centos

Yum-y install gcc automake autoconf libtool make

Yum-y install gcc-c ++ glibc

Yum-y install libmcrypt-devel mhash-devel libxslt-devel \
Libjpeg-devel libpng-devel freetype-devel libxml2 libxml2-devel \
Zlib-devel glibc-devel glib2 glib2-devel bzip2 bzip2-devel \
Ncurses-devel curl-devel e2fsprogs e2fsprogs-devel \
Krb5 krb5-devel libidn-devel openssl-devel
New version of php-fpm installation (recommended)

Wget http://cn2.php.net/distributions/php-5.4.7.tar.gz
Tar zvxf php-5.4.7.tar.gz
Cd php-5.4.7
./Configure -- prefix =/usr/local/php -- enable-fpm -- with-mcrypt \
-- Enable-mbstring -- disable-pdo -- with-curl -- disable-debug -- disable-rpath \
-- Enable-inline-optimization -- with-bz2 -- with-zlib -- enable-sockets \
-- Enable-sysvsem -- enable-sysvshm -- enable-pcntl -- enable-mbregex \
-- With-mhash -- enable-zip -- with-pcre-regex -- with-mysql -- with-mysqli \
-- With-gd -- with-jpeg-dir

Make all install
Install php-fpm manually in the old version (the old version of the program is no longer available. Here is a demonstration)
Wget http://cn2.php.net/get/php-5.2.17.tar.gz
Wget http://php-fpm.org/downloads/php-5.2.17-fpm-0.5.14.diff.gz
Tar zvxf php-5.2.17.tar.gz
Gzip-cd php-5.2.17-fpm-0.5.14.diff.gz | patch-d php-5.2.17-p1
Cd php-5.2.17
./Configure -- prefix =/usr/local/php-with-config-file-path =/usr/local/php/etc \
-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-zlib-dir-with-libxml-dir =/usr-enable-xml \
-With-mhash-with-mcrypt-enable-pcntl-enable-sockets-with-bz2-with-curl-with-curlwrappers \
-Enable-mbregex-with-gd-enable-gd-native-ttf-enable-zip-enable-soap-with-iconv-enable-bcmath \
-Enable-shmop-enable-sysvsem-enable-inline-optimization-with-ldap-sasl-enable-pdo \
-With-pdo-mysql
Make all install

You can install php-fpm in either of the preceding methods. After installation, the content is stored in the/usr/local/php directory.

 

The above completes the installation of php-fpm.

Below are the settings for php-fpm running users

Cd/usr/local/php
Cp etc/php-fpm.conf.default etc/php-fpm.conf
Vi etc/php-fpm.conf
Modify
User = www-data
Group = www-data

If the www-data user does not exist, add the www-data user first.
Groupadd www-data
Useradd-g www-data

2. Compile and install nginx

Then install nginx according to the http://www.nginx.cn/install

3. Modify the nginx configuration file to support php-fpm

After nginx is installed, change the nginx configuration file to nginx. conf.

The following configuration is added to the server segment. Pay attention to the configuration in red; otherwise, No input file specified will occur.

# Pass the PHP scripts to FastCGI server listening on Fig: 9000
#
Location ~ \. Php $ {
Root html;
Fastcgi_pass 127.0.0.1: 9000;
Fastcgi_index index. php;
Fastcgi_param SCRIPT_FILENAME $ document_root $ fastcgi_script_name;
Include fastcgi_params;
}

4. Create a PHP test file

Create a php file

Create the index. Php file in/usr/local/nginx/html and enter the following content:

<? Php
Echo phpinfo ();
?>
5. Start the service

Start php-fpm and nginx

/Usr/local/php/sbin/php-fpm
# Manual patching startup method/usr/local/php/sbin/php-fpm start

Sudo/usr/local/nginx
Php-fpm close restart see the end of the article

6. Browser access

Access http: // your server ip address/index. php, you can see the php information.

 

Possible errors when installing php-fpm:

1. php configure error

Configure: error: XML configuration cocould not be found

Apt-get install libxml2 libxml2-dev (under ubuntu)
Yum-y install libxml2 libxml2-devel (under centos)
2. Please reinstall the BZip2 distribution

Wget http://www.bzip.org/1.0.5/bzip2-1.0.5.tar.gz
Tar-zxvf bzip2-1.0.5.tar.gz
Cd bzip2-1.0.5
Make
Make install
3. There is a line in the php configuration file -- with-mysql =/usr.
Prompt during installation:
Configure: error: Cannot find MySQL header files under yes.
Note that the MySQL client library is not bundled anymore.

This is because the mysql header file is not installed during mysql installation, or the path is incorrectly specified, and php cannot find the mysql header file.
Solution.
(1) Check whether your system has installed the mysql header.
Find/-name mysql. h
If yes. Specify -- with-mysql =/and your normal path.
If not. Please refer to the next step.
(2) redhat installation
Rpm-ivh MySQL-devel-4.1.12-1.i386.rpm
(3) Install ubuntu
Apt-get install libmysqlclient15-dev
(4.) add -- with-mysql =/usr to the configuration option of php in the last step!

4. No input file specified.

Location ~ \. Php $ {
Root html;
Fastcgi_pass 127.0.0.1: 9000;
Fastcgi_index index. php;
Fastcgi_param SCRIPT_FILENAME $ document_root $ fastcgi_script_name;
Include fastcgi_params;
}

5. If the php configure Library is missing, install the library first (under ubuntu)

Sudo apt-get install make bison flex gcc patch autoconf subversion locate
Sudo apt-get install libxml2-dev libbz2-dev libpcre3-dev libssl-dev zlib1g-dev libmcrypt-dev libmhash-dev libmhash2 libcurl4-openssl-dev libpq-dev libpq5 libsyck0-dev

6. mcrypt. h not found. Please reinstall libmcrypt

Apt-get install libmcrypt-dev

Or

Cd/usr/local/src
Wget http://softlayer.dl.sourceforge.net/sourceforge/mcrypt/libmcrypt-2.5.8.tar.gz
Tar-zxvf libmcrypt-2.5.8.tar.gz
Cd/usr/local/src/libmcrypt-2.5.8
./Configure -- prefix =/usr/local
Make
Make install

7. php-fpm 5.4.7 how do I disable restart?

Php-fpm in php 5.4.7 does not support commands such as/usr/local/php/sbin/php-fpm (start | stop | reload) in php-fpm, signal control is required:

The master process can understand the following signals:

INT, TERM immediately terminate QUIT smoothly terminate USR1 re-open the log file USR2 smoothly reload all worker processes and re-load the configuration and binary module

Example:

Disable php-fpm:

Kill-INT 'cat/usr/local/php/var/run/php-fpm.pid'

Php-fpm restart:

Kill-USR2 'cat/usr/local/php/var/run/php-fpm.pid'

View the number of php-fpm processes:

Ps aux | grep-c php-fpm

8. Execute php under the command line and prompt that the command cannot be found

-Bash:/usr/bin/php: No such file or directory

Vi/etc/profile

Add a line of configuration at the bottom of the file
Export PATH =/usr/local/php/bin: $ PATH

Save and exit

Source/etc/profile


Use PHP-FPM to control FastCGI processes of PHP-CGI

/Usr/local/php/sbin/php-fpm {start | stop | quit | restart | reload | logrotate}
-- Start the fastcgi process of php
-- Stop force terminate the fastcgi process of php
-- Quit: smoothly terminate the fastcgi process of php
-- Restart the fastcgi process of php
-- Reload re-smoothly loads php. ini
-- Logrotate re-enable log files

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.