Home > Development, nginx, PHP, PHP-FPM > PHP-FPM and nginx upstart scripts

PHP-FPM and nginx upstart scripts

Upstart is becoming the de-facto standard for everything in Ubuntu, and I do enjoy it's process management and re-spawning capabilities.

(Actually, before PHP-FPM I used to use upstart jobs to su - $user php-cgi -b $port :))

These are VERY simple but effective scripts, and would actually be beefed up to be more intelligent (chaining nginx to start after PHP-FPM for example. However, if you do not need PHP, then that's a useless chain. So I kept it simple. I suppose you could add a short delay to start nginx then...)

Note: make sure PHP-FPM is set to daemonized = yes.

Second note: this works for PHP 5.2.x w/ the PHP-FPM patch, on Ubuntu Lucid Lynx (10.04) - anything else YMMV. I am not using PHP-FPM w/ PHP 5.3 yet since I have no environments that I know will support 5.3 code. When I finally get one, I will look for the same opportunity.

/etc/init/nginx.conf:

description "nginx"

start on (net-device-up and local-filesystems)
stop on runlevel [016]

expect fork
respawn
exec /usr/sbin/nginx

/etc/init/php-fpm.conf:

description "PHP FastCGI Process Manager"

start on (net-device-up and local-filesystems)
stop on runlevel [016]

expect fork
respawn
exec /usr/local/bin/php-cgi --fpm --fpm-config /etc/php-fpm.conf

Once you've done this you can remove all the /etc/init.d/php-fpm, /etc/init.d/nginx and /etc/rc?.d/[K,S]??php-fpm and /etc/rc?.d/[K,S]??nginx symlinks and files. This takes care of all of that.

Feel free to comment and leave better tips, tricks and ideas!

Categories: Development, nginx, PHP, PHP-FPM
  1. mike
    May 22nd, 2010 at 21:34 | #1

    Note: this does not do anything about logrotate signals, graceful reload signals, etc.

  2. Tim
    May 26th, 2010 at 05:53 | #2

    hey thanks, just what i needed to get started with upstart ...

  3. Peter Mescalchin
    July 28th, 2010 at 21:41 | #3

    Thanks Michael, so simple it can't be legal! 🙂

    Works a treat, upstart really is a nice thing - will be good to see this taking over from init.d/ in the future.

    Can confirm these scripts work perfectly with nginx & PHP 5.3.3 (with FPM/APC) under Ubuntu 10.04.

  4. mike
    August 4th, 2010 at 18:59 | #4

    @Peter Mescalchin
    My main concern is that it doesn't factor in the USR1, USR2, etc. signals that nginx, PHP-FPM and other daemons use for log rotation, graceful reloading, etc. Upstart is cool, but right now it has that limitation, at least from what I can tell. I tried starting a thread on the Upstart mailing list, and it went nowhere.

  1. No trackbacks yet.
You must be logged in to post a comment.