How To Switch PHP-FPM Listen On Unix Socket

Switch PHP-FPM Listen On Unix Socket

In this tutorial, we will show you how to switch PHP-FPM listen on Unix Socket. 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 useror access to the root user. We recommend acting as a non-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 where 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

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.

VPS Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “Nginx Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!