Configure Nginx and PHP-FPM to use sockets

     
27th June, 2014 by

The following guide describes how to configire Nginx and PHP-FPM to use Sockets on Centos 6. This guide assumes you have already installed a LEMP stack as shown in Install LEMP on Centos

Step 1 – Configure PHP-FPM

Edit the PHP-FPM configuration file and set the required values

vi /etc/php-fpm.d/www.conf

listen = /var/run/php-main.socket
listen.owner = nginx
listen.group = nginx
listen.mode = 0660

Alternatively the following sed commands will set the variables for you

sed -i s'/listen = 127.0.0.1:9000/listen = \/var\/run\/php-main.socket/' /etc/php-fpm.d/www.conf
sed -i s'/;listen.owner = nobody/listen.owner = nginx/' /etc/php-fpm.d/www.conf
sed -i s'/;listen.group = nobody/listen.group = nginx/' /etc/php-fpm.d/www.conf
sed -i s'/;listen.mode = 0660/listen.mode = 0660/' /etc/php-fpm.d/www.conf

Step 2 – Configure Nginx config

Edit the PHP-FPM configuration file and set the required values

vi /etc/nginx/conf.d/default.conf

fastcgi_pass unix:/var/run/php-main.socket;

Alternatively the following sed commands will set the variables for you

sed -i s'/fastcgi_pass   127.0.0.1:9000;/fastcgi_pass unix:\/var\/run\/php-main.socket;/' /etc/nginx/conf.d/default.conf

Step 3 – Restart the Services

All that’s left is to restart the services and test.

service php-fpm restart
service nginx restart

Delivery Manager

Daniel has been an evangelist for internet applications and infrastructure for over 15 years. A graduate of the university of Wollongong Computer Science department, he has delivered projects in complex applications ranging from Flight Center's corporate bookings engine to Macquarie Bank's equities trading platform.

When not at ServerMule Daniel can be found with his surfboard under his arm heading to the beach for a surf or encouraging his young son and daughter to get out there amongst the waves.

Read More from