Vinay Kumar

Deploying django application using uWSGI

One of my friend asked my help in deploying django project using uwgi. To be honest, deploying django using uwsgi is painstaking and very confusing. I had to go through django documentation and other sources to get the application running.

The below steps would make a good reference for future myself and others.

Start by creating a file named uwsgi.ini . The file can reside in your project directory or location of your choice.

$ cd your-django-project
// Activate your virtual environment if needed before running the below command.
$ pip install https://projects.unbit.it/downloads/uwsgi-lts.tar.gz
[uwsgi]
chdir=/home/ubuntu/project-directory
module=name_of_django_module.wsgi:application
master=True
socket=127.0.0.1:8000 # ip:port the django application should run on.
processes=1 # can be > 1
pidfile=/tmp/project-master.pid # Any filename
vacuum=True
max-requests=5000 # can be > 5000
# Create directory uwgi in /var/log 
daemonize=/var/log/uwsgi/application.log

Finally to start the application

$ uwsgi --ini uwsgi.ini # After entering to the directory where uwsgi.ini file exists.

If you are using nginx to serve django application (running on uwsgi), then it is mandatory to make use of the below lines.

upstream django {
  server 127.0.0.1:8000;
}

location / {
	include /etc/nginx/uwsgi_params;
       	uwsgi_pass django;
        uwsgi_param Host $host;
        uwsgi_param X-Real-IP $remote_addr;
        uwsgi_param X-Forwarded-For $proxy_add_x_forwarded_for;
        uwsgi_param X-Forwarded-Proto $http_x_forwarded_proto;
}

location /static/  {
       alias path-to-django-project-directory/static/;
}

If you make any changes to the application code, then it is mandatory to re-run the above command after running the below command or else you will be sucked into the black-hole of frustration.

$ killall uswgi # or ps ax|grep uwsgi and then sudo kill -9 process-id