Another Mike

  • Home
  • Archive

uWSGI init script

5/6/2011

This site (and accompanying backend for NameDropper ) went down about 4am PST this morning due to a Linode outage .

The downtime is not really a big deal for me, but I had forgotten to add a init script for uWSGI. My site would have come back fine after the reboot if I had this setup. I know better. Always test your init scripts!

Here's a quick, minimal configuration and init script for uWSGI. The process for setting up other system start scripts is very similar.

I'm using the uWSGI XML configuration. To use the daemon mode, here are a few XML configuration options to get you started. Save this wherever you like (usually in /etc/ somewhere).

 <uwsgi>
  <uid>uwsgi</uid>
  <gid>uwsgi</gid>
  <master/>
  <pidfile>/var/run/uwsgi/uwsgi.pid</pidfile>
  <daemonize>/var/log/uwsgi.log</daemonize>
</uwsgi>

You will need to create /var/run/uwsgi and allow the daemon user to write to the directory with chown uwsgi.uwsgi /var/run/uwsgi. Restrict permissions on the directory to just that user with chmod 700 /var/run/uwsgi.

Next, create the init script below as /etc/init.d/uwsgi and make it executable by running sudo chmod a+x /etc/init.d/uwsgi. Then to make sure it starts with the system, run update-rc.d uwsgi defaults. (These directions are for Debian-based systems. For Redhat, see chkconfig.)

#! /bin/bash
# /etc/init.d/uwsgi

daemon=/path/to/uwsgi
pid=/path/to/uwsgi.pid
args="-x /path/to/uwsgi.xml"

# Carry out specific functions when asked to by the system
case "$1" in
    start)
        echo "Starting uwsgi"
        start-stop-daemon -p $pid --start --exec $daemon -- $args
        ;;
    stop)
        echo "Stopping script uwsgi"
        start-stop-daemon --signal INT -p $pid --stop $daemon -- $args
        ;;
    reload)
        echo "Reloading conf"
        kill -HUP $(cat $pid)
        ;;
    *)
        echo "Usage: /etc/init.d/uwsgi {start|stop|reload}"
        exit 1
    ;;
esac

exit 0