Nginx + PHP-FPM domain socket configuration method

Nginx and PHP-FPM are a common pair of web servers and application servers used to host and execute PHP scripts. When configuring Nginx and PHP-FPM, you can use domain sockets to achieve communication between them. This article details how to configure Nginx and PHP-FPM to communicate using domain sockets.

  1. Configure PHP-FPM

First, we need to configure PHP-FPM to listen on a domain socket. Open the PHP-FPM configuration file (usually located at /etc/php-fpm.conf or /etc/php-fpm.d/www.conf) and make the following modifications:

listen = /var/run/php-fpm.sock

Replace the path (/var/run/php-fpm.sock) in the above line with the socket path you wish to use. Make sure that the path is writable on the system and is not occupied by other processes.

After saving and closing the PHP-FPM configuration file, restart the PHP-FPM service to make the configuration take effect.

  1. Configure Nginx

Next, we need to configure Nginx to communicate using PHP-FPM’s domain sockets. Open the Nginx configuration file (usually located at /etc/nginx/nginx.conf or /etc/nginx/conf.d/default.conf) and make the following modifications:

location / {
    ...
    fastcgi_pass unix:/var/run/php-fpm.sock;
    ...
}

In the above configuration, set the parameter of the fastcgi_pass directive to the domain socket path of PHP-FPM (consistent with the path in the PHP-FPM configuration).

After saving and closing the Nginx configuration file, reload or restart the Nginx service to make the configuration take effect.

Guess you like

Origin blog.csdn.net/update7/article/details/133432519