In this tutorial, we will show you how to switch PHP-FPM listen on Unix Socket. For those of you who didn’t know, PHP-FPM (FastCGI Process Manager) is an implementation of FastCGI for PHP that allows PHP applications to run efficiently on a web server. One way to communicate with PHP-FPM is through a Unix socket, which is a type of inter-process communication (IPC) mechanism that allows processes to communicate with each other on the same machine.
This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo
‘ to the commands to get root privileges. I will show you the step-by-step switch PHP-FPM listen on Unix Socket
Prerequisites
- A server running one of the following operating systems: CentOS Linux.
- It’s recommended that you use a fresh OS install to prevent any potential issues.
- SSH access to the server (or just open Terminal if you’re on a desktop).
- A
non-root sudo user
or access to theroot user
. We recommend acting as anon-root sudo user
, however, as you can harm your system if you’re not careful when acting as the root.
Switch PHP-FPM Listen On Unix Socket
Step 1. Installing LEMP server.
This guide assumes you have already installed a LEMP stack, you can follow our guide here.
Step 2. Configure PHP-FPM Listen on Unix Socket.
First, edit the file at /etc/php-fpm.d/www.conf
and find this block:
nano /etc/php-fpm.d/www.conf
Add the following file:
; 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 address on ; a specific port; ; 'port' - to listen on a TCP socket to all addresses on a ; specific port; ; '/path/to/unix/socket' - to listen on a unix socket. ; Note: This value is mandatory. ;listen = 127.0.0.1:9000 listen = /var/run/php5-fpm.sock
Next, let’s create a file at /etc/nginx/conf.d/
named php5-fpm.conf
:
upstream php5-fpm-sock { server unix:/var/run/php5-fpm.sock; }
Step 3. Configure Vhost Nginx.
Now we open Nginx virtual host config file(s), In our Nginx server config we’ll also have to change were to pass PHP requests:
#fastcgi_pass 127.0.0.1:9000; fastcgi_pass php5-fpm-sock;
Save and close, then reload PHP-fpm and Nginx services:
sudo systemctl restart php-fpm sudo systemctl restart nginx
Using a Unix socket for the connection between the web server and PHP-FPM can improve performance by avoiding the overhead of network communication. It can also be more secure, as the socket can be protected using file system permissions to limit access to the socket only to the web server and PHP-FPM processes.
Congratulations! You have successfully configured PHP-FPM to listen to Unix Socket. Thanks for using this tutorial to set up PHP-FPM Listen to Unix Socket system. For additional help or useful information, we recommend you check the official PHP-FPM website.