The paper
In Linux, nginx servers and PHP-FPM can be implemented using both TCP sockets and Unix sockets.
A Unix socket is a terminal that enables data communication between two or more processes running on the same operating system. This method requires the LOCATION of the PHP-FPM PID file to be filled in the nginx configuration file, which is more efficient than the TCP socket.
The advantage of TCP sockets is that they can cross servers and can only be used in this way when nginx and PHP-FPM are not on the same machine.
In Windows, only TCP sockets can be used for communication
Configuration method
tcp socket
For TCP socket communication, you need to fill in the Nginx configuration file with the IP address and port number of phP-FPM.
location ~ \.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;; Fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; } 123456Copy the code
unix socket
In Unix socket communication mode, you need to fill in the Nginx configuration file with the PID file address of phP-Fpm.
location ~ \.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;; fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; } 123456Copy the code
The php-fpm run port number and socket file address are configured in php-fpm.conf. Conf file in the /etc directory of the PHP installation file, such as /opt/ PHP, should be /opt/ PHP /php-fpm.
; The address on which to accept FastCGI requests.
; Valid syntaxes are:
; 'ip.add.re.ss:port' - to listen on a TCP socket to a specific IPv4 address on
; a specific port;
; '[ip:6:addr:ess]:port' - to listen on a TCP socket to a specific IPv6 address on
; a specific port;
; 'port' - to listen on a TCP socket to all IPv4 addresses on a
; specific port;
; '[::]:port' - to listen on a TCP socket to all addresses
; (IPv6 and IPv4-mapped) on a specific port;
; '/path/to/unix/socket'- to listen on a unix socket. ; Note: This value is mandatory. Listen = 127.0.0.1: listen = 9000 / var/run/PHP - FPM. Sock1234567891011121314Copy the code
As you can see from the comments, the PHP-fpm listen directive can handle FastCGI requests in five ways: Ipv4 :port number 2. Ipv6 :port number 3. Port equals to 0.0.0.0:port, which corresponds to all ipv4 ports on the host
The nginx process does not have permission to access the Unix socket file because the socket file is essentially a file created by the root user by default. The following command should be used:
; Set permissions for unix socket, if one is used. In Linux, read/write
; permissions must be set in order to allow connections from a web server. Many
; BSD-derived systems allow connections regardless of permissions.
; Default Values: user and group are set as the running user
; mode is set to 0660
listen.owner = www
listen.group = www
listen.mode = 066012345678Copy the code
You can configure both nginx and PHP-fpm to use WWW users, so that there is no problem with permissions, of course, you can also create different users, and then join the same group, easy to assign permissions.