Django uWSGI Service

Posted by Bryan in Web Development

uWSGILooking for a command line utility to control a uWSGI Django web application?

I have been using uWSGI with nginx for a good while now and am very satisfied with the stability and performance. However, as most know that us it, there are numerous options for executing and controlling a uWSGI app.

So, I created a simple python-based command line script to control my Django applications.

https://github.com/bryanpieper/django-wsgi-svc/blob/master/django-wsgi-svc.py

On a side note, nginx 0.8+ now supports the uwsgi protocol as part of the base install. You just need to install the uwsgi binary.

Command examples:

To start a Django app named thepiepers located in /home/[user]/webroot/thepiepers/django_wsgi.py.  

django-wsgi-svc.py thepiepers start

The script as written, will load the django_wsgi.py module to start the wsgi application.  It is all customizable via the command line options. It will create a unix socket /home/[user]/tmp/thepiepers_wsgi.sock that you can connect up to nginx.

You can also download the source code and modify the defaults to fit your environment.

# stop thepiepers
django-wsgi-svc.py thepiepers stop

# reload gracefully
django-wsgi-svc.py thepiepers reload

# restart thepiepers
django-wsgi-svc.py thepiepers restart

# check if thepiepers is running
django-wsgi-svc.py thepiepers status

# output thepiepers stats to the uwsgi log
django-wsgi-svc.py thepiepers stats

If you need to start your Django instances on reboot, you can use the crontab. Here is an example crontab:

@reboot django-wsgi-svc.py [djangoapp] start
@reboot django-wsgi-svc.py [djangoapp2] start
@reboot django-wsgi-svc.py [djangoapp3] start
@daily /bin/bash -lc 'python $HOME/webroot/[djangoapp][/manage.py cleanup'

Here are a couple good articles on setting up nginx and uWSGI: