Some Extra Steps Needed to Perform in Ubuntu for the Right Nginx PHP5-FPM UNIX Socket Configuration. Else You’ll Face Random 502, 504 Error. There are important parts of many Free Software has been patched, PHP5-FPM is one of them. We described how to fix the PHP5-FPM restart issue, in a separate GitHub project, but honestly; Ubuntu Nginx PHP5-FPM UNIX socket configuration is more difficult to fix by a new user. It will unfortunately give Nginx related error, WP-Cron related errors etc. We have a small list of HTTP Error Codes.
Errors Related To Wrong Ubuntu Nginx PHP5-FPM UNIX Socket Configuration
Normally, UNIX Socket is preferred over TCP/IP for faster end result. Errors can be like :
connect() to unix:/var/run/php5-fpm.sock failed
upstream timed out (110: Connection timed out)
connect() to unix:/tmp/php5-fpm.sock failed ( : No such file or directory)
---
It is basically not even possible to list the number of different type of errors. You have the everything right, these errors are relatively newer and related to modified materials for increasing security after fix of those shell shock, Heartbleed etc. deb Linux unfortunately throws more errors.
Ubuntu Nginx PHP5-FPM UNIX Socket Configuration : Does Socket Exits?
There are reasons to upgrade PHP 5.5.9 to 5.6. Although, our method will solve your issue with 95% warranty. Run php -v
and you will get this kind of output :
1 2 3 4 5 6 7 8 | PHP 5.5.9-1ubuntu4.6 (cli) (built: Feb 13 2015 19:17:11) Copyright (c) 1997-2014 The PHP Group Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies with XCache v3.1.0, Copyright (c) 2005-2013, by mOo with Zend OPcache v7.0.3, Copyright (c) 1999-2014, by Zend Technologies with XCache Optimizer v3.1.0, Copyright (c) 2005-2013, by mOo with XCache Cacher v3.1.0, Copyright (c) 2005-2013, by mOo with XCache Coverager v3.1.0, Copyright (c) 2005-2013, by mOo |
This is PHP 5.5.9 from Ubuntu’s “natural” repo. Normally, we edit this PHP-FPM pool config file :
1 | nano /etc/php5/fpm/pool.d/www.conf |
and add / make this active socket active :
1 | listen = /var/run/php5-fpm.sock |
instead of TCP/IP port :
1 | listen = 127.0.0.1:9000 |
In the Nginx configuration file, we pass in this way :
1 | fastcgi_pass unix:/var/run/php5-fpm.sock; |
Errors starts to throw on the fronted and in log file. This happens due to two reasons in combination – improper permission, wrong user-group of nginx
and non-existing UNIX socket file. WordPress like software will give you another gift – wp-cron related error. OwnCloud will give APC related error. 95% will be covered if we fix 5 stuffs.
UNIX socket, unfortunately needs to be created. Editing the /etc/php5/fpm/pool.d/www.conf
file to make php5-fpm.sock
active, in practical never works. Various complex errors become apparent with PHP-MySQL fronted Apps and their Plugins/Modules.
Checking the UNIX Socket Configuration
Your /etc/php5/fpm/php-fpm.conf
should look like this :
1 2 3 4 5 6 7 | [global] pid = /var/run/php5-fpm.pid error_log = /var/log/php5-fpm.log emergency_restart_threshold = 5 emergency_restart_interval = 2 events.mechanism = epoll include=/etc/php5/fpm/pool.d/*.conf |
Secondly, you should have a file named php5-fpm.conf
at /etc/nginx/conf.d
containing this :
1 2 3 | upstream php5-fpm-sock { server unix:/var/run/php5-fpm.sock; } |
Usually it never exists. In that case, do these :
1 2 3 4 5 6 | cd /etc/nginx/conf.d nano php5-fpm.conf # copy-paste/write this stuff upstream php5-fpm-sock { server unix:/var/run/php5-fpm.sock; } |
Save it and chown the directory :
1 | sudo chown root:www-data /etc/nginx/conf.d/ -R |
But, nginx
is not of www-data
user group. We should add :
1 2 | sudo useradd -g www-data nginx usermod -a -G www-data nginx |
These solves many things, but wait.
Editing /etc/php5/fpm/pool.d/www.conf
It is a challenge to edit /etc/php5/fpm/pool.d/www.conf
due to hundreds of commented out lines. This is what we are giving you is a fail safe set for chronic patient server :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | [www] user = www-data group = www-data listen = /var/run/php5-fpm.sock listen.owner = www-data listen.group = www-data listen.mode = 0660 listen.allowed_clients = 127.0.0.1 pm = dynamic pm.max_children = 5 pm.start_servers = 2 pm.min_spare_servers = 1 pm.max_spare_servers = 3 pm.max_requests = 500 request_terminate_timeout = 30 chdir = / security.limit_extensions = .php .php3 .php4 .php5 php_flag[display_errors] = off php_admin_value[error_log] = /var/log/fpm-php.www.log php_admin_flag[log_errors] = on php_admin_value[memory_limit] = 128M |
These settings will never make your 4GB instance blazing fast, but it will be stable.
Restart and Reboot the Services
1 2 3 4 5 | # if mysql is on the same server service mysql stop service php5-fpm stop service nginx stop reboot |
After the reboot, reload the configurations and start them :
1 2 3 4 | # if mysql is on the same server service mysql start service php5-fpm start service nginx start |
Check /var/log/nginx for Errors
cd
to :
1 2 | cd /var/log/nginx && ls -al cat error.log |
Copy & Paste the error to your computer and then empty the error.log
file :
1 2 3 4 | echo " " > error.log cat error.log # nothing |
Now, you’ll wait like a real cat and run cat error.log
every 5-6 mins for few times. With a normal website with visitors, if error has to arrive, it should show up. For WordPress, errors are due to wp-cron and for OwnCloud it is related to APC.
Nginx public directory is usually here :
/usr/share/nginx/html/
wp-cron.php
and wp-config.php
will be here :
/usr/share/nginx/html/wp-cron.php
You’ll disable this PHP chron from wp-config.php
file :
1 2 3 4 5 | nano /usr/share/nginx/html/wp-config.php # add this define('DISABLE_WP_CRON', true); # before /* That's all, stop editing! Happy blogging. */ |
and add a real cron :
1 2 | crontab -e */15 * * * * cd /usr/share/nginx/html/; php /usr/share/nginx/html/wp-cron.php > /dev/null 2>&1 |
This is PHP-CLI setup for running that wp-cron.php
every 15 minutes. It possibly has to do something with W3 Total Cache. You can decrease the time to 1 or 2 minute depending on load average and top
.
For OwnCloud, you will need another thing :
1 2 3 4 5 | cd /etc/php5/fpm/conf.d/ nano 20-apcu.ini # edit ;extension=apcu.so apc.enabled=0 |
Obviously, restart the services – service php5-fpm restart && service nginx restart
.