How to configure php-FPM in nginx

Source: Internet
Author: User
Tags fpm ftp php and php error php error log socket tidy unix domain socket

Php-FPM configuration in nginx tutorial

Nginx can directly call FPM to drive php and then discard apache. Let's not talk about the reason.
First download the php5.4 installation package.

According to others

The code is as follows: Copy code
. /Configure -- enable-fastcgi -- prefix =/data1/server/php-cgi -- with-gd -- with-jpeg-dir -- with-png-dir -- with-freetype-dir -- enable-mbstring -- with-mysql -- with-mysqli -- with-pdo-mysql -- enable-sockets -- with-curl -- with-ttf -- with-libxml-dir -- with-config- file-path =/data1/server/php-cgi/etc -- with-zlib -- enable-exif -- enable-ftp -- with-xmlrpc -- enable-zip -- with-iconv-dir -- with-libxml-dir -- with-mcrypt -- with-tidy -- enable-fpm -- enable-force-cgi-redirect

Prompts you that you cannot install a bunch of things such as enable-force-cgi-redirect.

Okay. Remove some things for installation.

The code is as follows: Copy code
. /Configure -- prefix =/data1/server/php-cgi -- with-gd -- with-jpeg-dir -- with-png-dir -- with-freetype-dir -- enable-mbstring -- with-mysql -- with-mysqli -- with-pdo-mysql -- enable-sockets -- with-curl -- with-libxml-dir -- with-config-file-path =/data1/server /php-cgi/etc -- with-zlib -- enable-exif -- enable-ftp -- with-xmlrpc -- enable-zip -- with-iconv-dir -- with-libxml-dir -- -mcrypt -- with-tidy -- enable-fpm

Error
Configure: error: xml2-config not found. Please check your libxml2 installation.

The code is as follows: Copy code

1. Install him
# Apt-get install libxml2-dev

Error
Configure: error: Please reinstall the libcurl distribution-
Easy. h shoshould be in <curl-dir>/include/curl/

2. Install him
Apt-get install libcurl4-gnutls-dev

Error
Configure: error: Invalid Lib. h not found.
3. Install him
Apt-get install libjpeg-dev

Error
If configure fails try-with-vpx-dir = <DIR>
Configure: error: png. h not found.
4. Install him
Apt-get install libpng12-dev

Error
If configure fails try-with-xpm-dir = <DIR>
Configure: error: freetype. h not found.
5. Install him
Apt-get install libfreetype6-dev

Error
Configure: error: mcrypt. h not found. Please reinstall libmcrypt.
6. Install him
Apt-get install libmcrypt-dev

Error
Configure: error: Cannot find libtidy
7. Install him
Apt-get install libtidy-dev

I tested it one by one. During installation, you can install these errors first, and then execute./configure ......

Make
Make test
Make install


Start php-fpm

/Data1/server/php-cgi/sbin/php-fpm

Error
[28-Mar-2012 11:15:01] ERROR: failed to open configuration file '/data1/server/php-cgi/etc/php-fpm.conf': No such file or directory (2)
[28-Mar-2012 11:15:01] ERROR: failed to load configuration file '/data1/server/php-cgi/etc/php-fpm.conf'
[28-Mar-2012 11:15:01] ERROR: FPM initialization failed
Cd/data1/server/php-cgi/etc

Start again
/Data1/server/php-cgi/sbin/php-fpm

ERROR: [pool www] cannot get gid for group 'nobody'
Okay, let's add a group named nobody.

# Groupadd nobody

Start again
/Data1/server/php-cgi/sbin/php-fpm
Succeeded.

Disable php-fpm
Killall php-fpm

I do not know whether there are direct commands to close it.

Cannot get gid for group 'nobody' other solutions.
----------------------------
Or can change it in your php-fpm.conf

; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user's group
; Will be used.
User = www-data
Group = nobody
-----------------------------

Integrate nginx

Final configuration

The code is as follows: Copy code

# Cd/etc/nginx/conf. d
# Vi default. conf

Location ~ \. Php $ {

Root/usr/share/nginx/html;
Fastcgi_pass 127.0.0.1: 9000;
Fastcgi_index index. php;
Include fastcgi. conf;

# Vi/etc/nginx/fastcgi. conf

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 was built with-enable-force-cgi-redirect
Fastcgi_param REDIRECT_STATUS 200;

/Etc/init. d/nginx stop
/Etc/init. d/nginx start

Success.

Create a configuration file for php and copy one from the installation directory.
Cp php. ini-development/data1/server/php-cgi/etc/php. ini

Supplement: php-fpm outputs php error logs

Nginx is a web server. Therefore, nginx access logs only record access pages and do not contain php error logs.

Nginx sends php requests to the php-fpm fastcgi process for processing. The default php-fpm will only output the error message of php-fpm, the php errorlog cannot be found in the php-fpm errors log.

The reason is that in the php-fpm configuration file php-fpm.conf, the default is to close the error output of the worker process, directly redirect them to/dev/null, therefore, we cannot see php error logs in nginx error log and php-fpm errorlog.

Debugging is very painful. Solution:

1. Add if the configuration in the php-fpm.conf is not modified
Catch_workers_output = yes
Error_log = log/error_log

2. Modify the configuration in php. ini. If no configuration exists, add
Log_errors = On
Error_log = "/usr/local/lnmp/php/var/log/error_log"
Error_reporting = E_ALL &~ E_NOTICE

3. Restart php-fpm,
When PHP executes an error, the error log is in "/usr/local/lnmp/php/var/log/error_log".

Note:

1. The php_admin_value [error_log] parameter in the php-fpm.conf will overwrite the error_log parameter in php. ini
Make sure that the final error_log file you see in phpinfo () has the write permission and does not set the php_admin_value [error_log] parameter. Otherwise, the error log will be output to the php-fpm error log.


2. The php. ini location cannot be found. Use php phpinfo () to view the result.


3. How to modify PHP error logs not to be output to the page or screen
Modify php. ini
Display_errors = off // no error message is displayed (not output to the page or screen)
Log_errors = on // record error information (saved to the log file)
Error_reporting = E_ALL // capture all error messages
Error_log = // Set the log file name

Modify the above configurations in the program
Ini_set ("display_errors", 0)
Ini_set ("error_reporting", E_ALL); // The value seems to be a PHP constant.
Ini_set ("error_log", "<log file name> ")
Ini_set ("log_errors", 1 );

4. How to output php error logs to nginx error logs
In PHP 5.3.8 and earlier versions, PHP running through FastCGI will first be written to the PHP errorlog when an error occurs during user access.
If PHP errorlog cannot be written, the error content will be returned to the FastCGI interface, and then nginx records the error returned to nginx errorlog after receiving the FastCGI error.
In PHP 5.3.9 and later versions, if an error occurs, PHP only attempts to write it to the PHP errorlog. If it fails, it will not return to FastCGI, the error log is output to the php-fpm error log.
If you want to output the php error log to the nginx error log, you must use a version earlier than php5.3.8, and the php error_log in the configuration file cannot be written to the php worker process.


Php-fpm memory configuration problems


The biggest problem in the nginx php-fpm configuration process is internal leakage: the server load is not large, but the memory usage increases rapidly. Soon the memory is consumed and then the swap partition is started, and the system will soon crash!
One day after google finally found something useful. In fact, according to the official introduction, there is no memory leakage in php-cgi. After each request is complete, php-cgi recycles the memory, but it will not be released to the operating system, which will cause a large amount of memory to be occupied by php-cgi.
The official solution is to reduce the value of PHP_FCGI_MAX_REQUESTS, I use php-fpm, the corresponding php-fpm.conf is max_requests, the value indicates how many requests will be sent to restart the thread, we need to reduce this value appropriately to allow php-fpm to automatically release the memory, instead of the 51200 value mentioned on the internet. In fact, there is another max_children value associated with it, this is how many processes will be created for each php-fpm, so the actual memory consumption is max_children * max_requests * memory usage for each request. Based on this, we can estimate the memory usage, you don't have to write a script to kill it.
The following is actually the process of restarting the script, which is not very serious, but we should be careful not to say that it is good to restart all the time, because the restart will lead to soaring cpu usage and huge system load, therefore, it is important to balance the above data.
Mar 08 16:13:33. 113138 [NOTICE] fpm_got_signal (), line 48: received SIGCHLD
Mar 08 16:13:33. 113202 [WARNING] fpm_children_bury (), line 215: child 23051 (pool default) exited on signal 11 SIGSEGV after 747.428492 seconds from start
Mar 08 16:13:33. 113622 [NOTICE] fpm_children_make (), line 352: child 24511 (pool default) started


Configuration Guide Communication Guide

TCP configuration method

TCP communication configuration is simple. You can do it in three steps.

Step 1: edit/etc/nginx/conf. d/your site configuration file (if the default configuration file is used, modify/etc/nginx/sites-available/default)

Change the fastcgi_pass parameter to 127.0.0.1: 9000, as shown in the following figure:

The code is as follows: Copy code
Location ~ \. Php $ {
Index. php index.html index.htm;
Include/etc/nginx/fastcgi_params;
Fastcgi_pass 127.0.0.1: 9000;
Fastcgi_index index. php;
Include fastcgi_params;
 }

Step 2: Edit the php-fpm configuration file/etc/php5/fpm/pool. d/www. conf.

Modify the listen parameter to 127.0.0.1: 9000, as shown in the following code:

Listen = 127.0.0.1: 9000

Step 3: restart php-fpm and nginx

Unix socket configuration

In fact, unix socket should be strictly called unix domain socket, which is a widely used method of * nix system process communication (IPC. as the unique identifier (descriptor) of the socket, two processes that need to communicate can reference the same socket descriptor file to establish a channel for communication.

Five steps are required for configuration

Step 1: Determine the storage location of your socket descriptor file.

It can be placed anywhere in the system. If you want a faster communication speed, you can put it under/dev/shm. This directory is called tmpfs, which is an area that RAM can directly use. Therefore, the read/write speed is always fast.

If the file location is determined, you need to modify the file permission. To grant nginx and php-fpm the read and write permissions to the file, you can:

The code is as follows: Copy code
Sudo touch/dev/sh/fpm-cgi.sock
Sudo chown www-data: www-data/dev/shm/fpm-cgi.sock.
Sudo chmod 666/dev/shm/fpm-cgi.sock

Step 2: modify the php-fpm configuration file/etc/php5/fpm/pool. d/www. conf.

Change the listen parameter to/dev/shm/fpm-cgi.sock, as shown in the following code:


Listen =/dev/shm/fpm-cgi.sock
Change the listen. backlog parameter to-1. The memory pressure is infinite. The default value is 128. If the concurrency is high, an error is returned.

; Set listen (2) backlog. A value of '-1' means unlimited.
; Default Value: 128 (-1 on FreeBSD and OpenBSD)
Listen. backlog =-1

Step 3: modify the nginx site configuration file

Change the fastcgi_pass parameter to unix:/dev/shm/fpm-cgi.sock, like this:

The code is as follows: Copy code
Location ~ \. Php $ {
Index. php index.html index.htm;
Include/etc/nginx/fastcgi_params;
Fastcgi_pass unix:/dev/shm/fpm-cgi.sock;
Fastcgi_index index. php;
Include fastcgi_params;
}

Step 4: modify/etc/sysctl. conf file to increase the number of concurrent connections at the kernel level (I am not particularly familiar with this system-level configuration file. Refer to this blog: Php-fpm TcpSocket vs UnixSocket)

Sudo echo 'Net. core. somaxconn = 2048 '>/etc/sysctl. conf
Sudo sysctl-p
Step 5: Restart the nginx and php-fpm services (it is best to restart php-fpm and then nginx)

Analysis and summary of the two communication modes

In principle, the unix socket method must be faster than the tcp method and consume less resources, because the sockets communicate between processes of nginx and php-fpm, tcp needs to go through the local loopback driver, and requests temporary ports and tcp-related resources.

Of course, in principle, unix socket may not seem so stable. When the number of concurrent connections breaks out, a large amount of long cache will be generated. Without support for connection-oriented protocols, large data packets may cause direct errors and will not return exceptions. The connection-oriented protocol such as TCP can ensure the correctness and integrity of communication.

Of course, the above mainly involves theoretical analysis that is not fully understood and subjective speculation. The specific difference is that we need to talk about it through test data. In the future, we will conduct tests in this area. My theoretical analysis on testing data from other blogs on the internet is almost correct. As for the method you choose, I can only say that "you and the bear's paw cannot have both sides". With superb O & M and configuration skills, let's make a balance between performance and stability.

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.