Search code examples
pythondjangoubuntunginxwaitress

Constructing an Upstart file for python-waitress


I have a Django app running gunicorn - I now want to replace it with waitress. Easy enough, I installed waitress via apt-get install python-waitress, and ran it via waitress-serve --port=8080 myproject.wsgi:application.

Now I want to set this up to run via an Upstart file (I'm on Ubuntu). I want it to run on normal runlevels 2, 3, 4, and 5, and tell it to stop when its in any other runlevel (such as when the system is rebooting, shutting down, or in single-user). I also want Upstart to automatically restart the service if it fails. Last but not least, I need waitress to run on port 80, not port 8080.

My questions are: how do I construct my Upstart file to reflect those requirements? And any other inclusions that will be helpful additions? Please advise; this is my main production server.

Secondly, as per waitress itself, other than being able to use it with SSL, are there any advantages of using a reverse proxy (such as Nginx) with waitress? My application is a read heavy Django web app where users congregate and chat with one another.


Solution

  • Instead of writing your own upstart script, I would recommend to look into other well-known and battle tested solutions. One such popular solution is supervisor. It is pure-Python and is pretty easy to install and configure. If you install with apt-get it comes with all the necessary scripts to run it when Ubuntu itself restarts, etc (although then most-likely you will not be installing latest version of supervisor). Once supervisor is installed, all you need to do is add a small configuration for your web-server process. Then when you start supervisor, it will manage your web-server process. Here is a sample supervisor config:

    [program:project_name]
    command=/path/to/virtualenv/bin/waitress-serve --port=8080 myproject.wsgi:application
    directory=/path/to/django/project
    autostart=true
    autorestart=true
    redirect_stderr=True
    user=www
    

    Note that supervisor can manage multiple processes so if you have any other processes which are required to run for your webapp, you can add them to supervisor as well.

    As for advantages of using nginx, there are plenty of other StackOverflow questions on the topic so I would recommend to look at those rather then reciting similar ideas here. Here are a couple I found: