How to configure Django in Debian with Nginx, uWSGI and Python 3

How to configure Django in Debian with Nginx, uWSGI and Python 3

In this howto, we will create a Django application and serve it with Nginx and uWSGI from our Debian server.

Nginx is the web server and uWSGI is the application server container which will be executing the python code.

Nginx natively supports the WSGI protocol used to comunicate with uWSGI, so there is no more dependencies.

Install the packages:


apt-get install python3-django nginx uwsgi uwsgi-plugin-python3

Change to the directory where the Django project will reside:


cd /home/www

Create the Django project:


django-admin startproject el_project

Configure nginx:


upstream el_project {
server unix:///run/uwsgi/app/el_project/socket;
}
server {
listen 80;
server_name default_server;
location / {
uwsgi_pass el_project;
include /etc/nginx/uwsgi_params;
}
}

Configure uWSGI:


[uwsgi]
project=el_project
plugins=python3
chdir=/home/www/el_project
module=el_project.wsgi:application
env=DJANGO_SETTINGS_MODULE=el_project.settings
master=true
processes=10
vacuum=true
chmod-socket=666

Enable the Nginx and uWSGI configurations:


ln -s /etc/nginx/sites-available/el_project /etc/nginx/sites-enabled/el_project
ln -s /etc/uwsgi/apps-available/el_project.ini /etc/uwsgi/apps-enabled/el_project.ini

Enable and start the services:


systemctl enable nginx
systemctl restart nginx
systemctl enable uwsgi
systemctl restart uwsgi

With this, now go with your web browser to http://ip_of_your_server and you will be greeted by a
DisallowedHost traceback. From here, you can start working with your Django application.