Django webservice on Nginx with UWSGI

Supposing, you have got a Django project you want to start on a separate server. There are many ways to do it. At this article, I will tell about running the simplest Django project with minimal Django and Nginx configurations using UWSGI.

Step 0. Installing Django and Nginx.

Supposing, you have already got installed Python. In order to install Django run this command in terminal:

$ sudo pip install django

Installing Nginx:

$ sudo apt-get install nginx

Installing UWSGI:

$ sudo apt-get install uwsgi
$ sudo pip install uwsgi

Step 1. Creating a project.

Let's suppose, the name of our project is djangoexample1. In order to create it, run this:

$ django-admin startproject djangoexample1

After that the folder djangoexample1 with contained files will be created. To check it, run:

$ python runserver

After that open your browser and follow The pleasant title "It worked! Congratulations on your first Django-powered page." should be appeared there.

Upload your project to your server. Futher actions will be done on the server.

If everything worked fine, going to the step 2.

Step 2. Configuring Nginx.

Going to the server via SSH. Do not forget to install Django there globally or locally in a virtual environment.

All Nginx configuration files are stored in /etc/nginx/. In order to add a new configuration, create a new file in the folder /etc/nginx/sites-available:

$ sudo nano /etc/nginx/sites-available/djangoexample1

Put these lines there and save:

upstream djangoexample1 {
    server unix:///tmp/djangoexample1.sock;

server {
    listen      8081;
    server_name djangoexample1;
    charset     utf-8;

    location / {
        uwsgi_pass  djangoexample1;
        include     /home/alexander/websites/djangoexample1/uwsgi_params;

Pay attention, the specified port is 8081. This means this is the port that will be used to access your project from the outside. /tmp/djangoexample1.sock is the a to a socket for working with UWSGI. The file /home/alexander/websites/djangoexample1/uwsgi_params contains UWSGI parameters (your exact path to the file will be different), it should look like this:

uwsgi_param  QUERY_STRING       $query_string;
uwsgi_param  REQUEST_METHOD     $request_method;
uwsgi_param  CONTENT_TYPE       $content_type;
uwsgi_param  CONTENT_LENGTH     $content_length;

uwsgi_param  REQUEST_URI        $request_uri;
uwsgi_param  PATH_INFO          $document_uri;
uwsgi_param  DOCUMENT_ROOT      $document_root;
uwsgi_param  SERVER_PROTOCOL    $server_protocol;
uwsgi_param  REQUEST_SCHEME     $scheme;
uwsgi_param  HTTPS              $https if_not_empty;

uwsgi_param  REMOTE_ADDR        $remote_addr;
uwsgi_param  REMOTE_PORT        $remote_port;
uwsgi_param  SERVER_PORT        $server_port;
uwsgi_param  SERVER_NAME        $server_name;

Add this file to your project.

In order to enable the created Nginx configuration, it is necessary to create a link in the folder /etc/nginx/sites-enabled:

$ sudo ln -s /etc/nginx/sites-available/djangoexample1 /etc/nginx/sites-enabled/djangoexample1

Restarting Nginx:

$ sudo service nginx restart

Step 3. Installing and running UWSGI.

Running UWSGI daemon:

$ uwsgi --socket /tmp/djangoexample1.sock --wsgi-file djangoexample1/ --chmod-socket=666 --daemonize djangoexample1.log

If everything is done correctly, following http://:8081 in your browser will lead to the title "It worked! Congratulations on your first Django-powered page.".