0

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.

Hassan Baig
  • 15,055
  • 27
  • 102
  • 205

1 Answers1

0

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:

Community
  • 1
  • 1
miki725
  • 27,207
  • 17
  • 105
  • 121
  • Miki thanks for the post on supervisor. Regarding nginx, as a reverse proxy with waitress, I asked that question because (i) I read this post claiming waitress is a good enough production-grade standalone server: http://blog.etianen.com/blog/2014/01/19/gunicorn-heroku-django/, and (ii) the only reason someone's ever mentioned using nginx with waitress is because of https support: http://stackoverflow.com/questions/19462959/i-can-not-connect-to-https-waitress-wsgi-server – Hassan Baig Dec 12 '15 at 06:33
  • 1
    Havent myself used waitress however I would imagine nginx will be higher performance serving static files and other things nginx does well like load-balance, etc. Also it has some basic tools for protecting against DDOS, etc attachs since it can more efficiently manage memory. But at the end of the day, I think you should use whatever works for you. I would give nginx a try – miki725 Dec 12 '15 at 15:02
  • Yep, I've taken your advice and gone deep dive into setting up nginx + gunicorn. Everything works there, except when I try to access some DB records, I get an **invalid input syntax for type inet** error. If you can help, take a quick glance at separate ques I just opened up: http://stackoverflow.com/questions/34238623/invalid-input-syntax-for-type-inet-db-error-in-django-app-with-postgres-and-gu – Hassan Baig Dec 12 '15 at 15:24