Nginx – Directory for PHP 5 FPM socket files is deleted after reboot on Ubuntu Server

nginxPHPphp-fpmsocketUbuntu

I've created a directory to store socket files for PHP 5 FPM: sudo mkdir /var/run/php-fpm. It is owned by root:root as well as core PHP FPM process.

When PHP FPM is starting, it creates socket files for each pool inside of this directory and nginx uses them to communicate.

However, when I reboot my machine (sudo reboot) this directory is gone after reboot and PHP FPM can't start, cause it can't create socket files inside of a missing directory.

What can be the problem? How do I debug it?

I'm using latest Ubuntu Server 14.04.

Best Answer

Current version of Ubuntu is using so-called up-start configs to run services. More info about this can be found here.

All up-start scripts are located in the /etc/init directory. Do not confuse it with old init scripts in /etc/init.d directory.

As @Rhim stated in his answer, /var/run is a temporary mounted filesystem, that is re-created after each reboot, so changes are not persisted in it. So in order to have a custom directory for socket files you will have to create it every time. The best place to add such functionality is an upstart config for PHP FPM located in: /etc/init/php5-fpm.conf.

Here's the modified version of this config:

# php5-fpm - The PHP FastCGI Process Manager

description "The PHP FastCGI Process Manager"
author "Ondřej Surý <ondrej@debian.org>"

start on runlevel [2345]
stop on runlevel [016]

# Precise upstart does not support reload signal, and thus rejects the
# job. We'd rather start the daemon, instead of forcing users to
# reboot https://bugs.launchpad.net/ubuntu/+source/php5/+bug/1272788
#
# reload signal USR2

pre-start script
    mkdir -p /var/run/php-fpm
    /usr/lib/php5/php5-fpm-checkconf
end script

respawn
exec /usr/sbin/php5-fpm --nodaemonize --fpm-config /etc/php5/fpm/php-fpm.conf

pre-start stanza was already used to check PHP FPM configuration file so I've converted it to a script section and added my own command for creating the direcotry for socket files before calling the checkconf.

With this script it should work fine. In order to control state of the service use the service command like this: service php5-fpm start, service php5-fpm restart etc.

I hope it will help someone. Cheers!