Deploying Django project with Nginx and Uwsgi

Sorry for a small pause, this time I will try to show you how to deploy a django project with Nginx and Uwsgi.

Speaking you have debian, follow these steps:

we first install virtualenv:

pip install virtualenv

then we open a new virtual environment for our new django project. Virtualenv is a cool stuff, you won’t have dependency hell with it, just your own virtualenv where you can install and run any modules you want without messing up with the modules that are installed systemwide or etc..

lets now create and activate our own virtualenv:

virtualenv project_env
cd project_env
source bin/activate

and now we will install our wonderful django framework making sure that our virtualenv is activated. It is not important that we are inside the virtualenv folder, main thing virtualenv is activated:

pip install Django
django-admin.py startproject mysite

now we install uwsgi into our virtualenv:

pip install uwsgi

Now, we need to install Nginx, here we go out of our virtualenv by doing:

deactivate

and install nginx:
sudo apt-get install nginx

Configure now Nginx:

first, you need to uwsgi_params file which is availablehere: https://github.com/nginx/nginx/blob/master/conf/uwsgi_params

Just copy this into your project directory where nginx will refer to. I will you this in a while.

Now create a mysite_nginx.conf file and put this content into it:

upstream mysite {
    server unix:///var/www/mysite/mysite.sock;
}

server {
    listen      80;
    server_name .mysite.com;
    charset     utf-8;
    client_max_body_size 75M;

    location /media  {
        alias /var/www/mysite/mysite/media;
    }

    location /static {
        alias /var/www/mysite/mysite/static;
    }

    location / {
        uwsgi_pass  mysite;
        include     /etc/nginx/uwsgi_params;
        proxy_read_timeout 150;
    }

    location /nginx_status {
        stub_status on;
        access_log   off;
        # allow 127.0.0.1;
        # deny all;
    }
}

Now we simlink this file to nginx sites-enabled folder:

sudo ln -s ~/path/to/your/mysite/mysite_nginx.conf /etc/nginx/sites-enabled/

and collect all django static files into static folder:
python manage.py collectstatic

and restart the nginx:
sudo /etc/init.d/nginx restart

Now we create a ini file and put this content into it:

[uwsgi]
socket = /var/www/mysite/mysite.sock
chdir = /var/www/mysite
module = mysite.wsgi:application
virtualenv = /var/www/virtualenvs/project_env/
buffer-size = 8192
processes = 60
env = DJANGO_SETTINGS_MODULE=mysite.settings
enable-threads = True
single-interpreter = True
uid = www-data
gid = www-data
vacuum = True
disable-logging = true
logger = file:/var/www/mysite/log/uwsgi_err.log

with this, we have all our uwsgi configurations in one single file – easy to handle and configure.

As you noticed, we installed uwsgi only in our virtualenv, now we need to install it systemwide (look at deactivate with which we leave the virtualenv).

deactivate
sudo pip install uwsgi

and create vassals folder

mkdir /etc/uwsgi
mkdir /etc/uwsgi/vassals
cd /etc/uwsgi/vassals/

and simlink our ini file to this vassals folder:

ln -s /var/www/mysite/conf/uwsgi.ini mysite.ini

We are almost ready. Now we open the rc.local file and put this command into it before “exit 0”

/usr/local/bin/uwsgi --emperor /etc/uwsgi/vassals --uid www-data --gid www-data --daemonize /var/log/uwsgi/emperor.log

and save the file. With this, the uwsgi runs as daemon and restarts automatically if system reboots etc etc.. nice stuff

aaand last step: give this command into your console:

/usr/local/bin/uwsgi --emperor /etc/uwsgi/vassals --uid www-data --gid www-data --daemonize /var/log/uwsgi/emperor.log

and restart nginx:
/etc/init.d/nginx restart

Hope, you didnot get any errors while deploying and that this post will help someone! have fun!