Deploy Django Project Using NGINX and Gunicorn

Meet Django

Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design. Built by experienced developers, it takes care of much of the hassle of Web development, so you can focus on writing your app without needing to reinvent the wheel. It’s free and open source.

In this guide, we will try how to install and configure some components on Ubuntu 14.04 to support and serve Django applications.

Meet NGINX

NGINX accelerates content and application delivery, improves security, facilitates availability and scalability for the busiest web sites on the Internet.

Meet Gunicorn

Gunicorn ‘Green Unicorn’ is a Python WSGI HTTP Server for UNIX. It’s a pre-fork worker model ported from Ruby’s Unicorn project. The Gunicorn server is broadly compatible with various web frameworks, simply implemented, light on server resources, and fairly speedy. This Gunicorn will serve as an interface to our application, translating client requests in HTTP to Python calls that our application can process.

Things we will try to achieve :

  1. Installing Django within a virtual environment.
  2. Start Django project from scratch.
  3. Host this Django project using Nginx and Gunicorn.

Installing Django within a virtual environment:

In-order to have django within virtual environment we need to execute virtualenv command. We can install this with pip.

For Python 2:

sudo pip install virtualenv

Python 3:

sudo pip3 install virtualenv

As now we have our virtualenv ready, we can go ahead and setup projects directory. 

  • mkdir ~/myproject
  • cd ~/myproject

Within this project directory, create a Python virtual environment by typing:

virtualenv myproject_env

By executing this command “myproject_env” named directory will be created. It will be used to install and configure different python environments.

Now to activate this virtual environment, execute following command.

source myproject_env/bin/activate

With your virtual environment activated, We can move forward by installing Django, Gunicorn.

pip install django gunicorn

Components which are installed under virtual environment are isolated from other os global environments.

Start Django project from scratch.

Now with virtual environment been activated, Start django project using django management command.

django-admin.py startproject myproject .

Now open settings.py fiile located under myproject folder.

Edit this settings.py by replacing following code :

ALLOWED_HOSTS = ['your_server_domain_or_IP',]

STATIC_URL = '/static/' STATIC_ROOT = os.path.join(BASE_DIR, 'static/')

Complete remaining basic project setup

cd ~/myproject

./manage.py makemigrations

./manage.py migrate

./manage.py createsuperuser

./manage.py runserver 0.0.0.0:8000
In web browser, enter your_ip_address:8000:

Once you had finished exploring recently created project, hit CNRL + C.

Now let’s test Gunicorn interface.

cd ~/myproject

gunicorn --bind 0.0.0.0:8000 myproject.wsgi:application

This will start gunicorn on same interface on which our earlier development server was working.

Now type this command to deactivate the virtual environment:

deactivate

Now we have to create Gunicorn systemd service file.

This service file will be used to start or stop our application server.

Open the gunicorn systemd service file using this command.

sudo vi /etc/systemd/system/gunicorn.service

Edit this service file with following data.

[Unit]
Description=gunicorn daemon
After=network.target

[Service]
User=vivek
Group=www-data
WorkingDirectory=/home/vivek/myproject
ExecStart=/home/vivek/myproject/myproject_env/bin/gunicorn --workers 3 --bind unix:/home/vivek/myproject/myproject.sock myproject.wsgi:application

[Install]
WantedBy=multi-user.target

Save and close the file.

Start the Gunicorn service using following command.

sudo systemctl start gunicorn

sudo systemctl enable gunicorn

This will create socket file inside your project directory.

Always check that the socket file has www-data group and this group should have group ownership.

Now is the time to configure NGINX

Create new server block in Nginx’s sites-available directory.

sudo vi /etc/nginx/sites-available/myproject

Now edit this server block with following code.

server {
    listen 80;
    server_name server_domain_or_IP;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/vivek/myproject;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/home/vivek/myproject/myproject.sock;
    }
}

Save and close the file.

Now go forward by enabling the file by linking it to the sites-enabled directory:

sudo ln -s /etc/nginx/sites-available/myproject /etc/nginx/sites-enabled

Test your Nginx configuration for syntax errors by typing:

sudo nginx -t

If no errors is found, Restart Nginx by typing:

sudo systemctl restart nginx

In the end, Following points were achieved :

  1. Virtual environment having its own Django project.

  2. Django can now handle client requests thanks to Gunicorn.

  3. Nginx is added to act as a reverse proxy to handle client connections and serve the project.

2 thoughts on “Deploy Django Project Using NGINX and Gunicorn

  1. Hi Vivek, this is nice write. And your website looks awesome. Does this go on Django or Word Press standard build? : ) cheers

    Like

Leave a comment