Nginx web server, uwsgi and Django

23 Nov 2020 | by Derek

In this setup, we install an Nginx web server, uwsgi and Django.

In case this needs updated, always reference,

Step 1: Install Packages

Run the following command to install the packages:

sudo apt-get install python3-pip nginx
sudo pip3 install uwsgi virtualenv

Step 2: Configure Nginx

Create sites-available file

sudo vi /etc/nginx/sites-available/projectname
# projectname web config

# the upstream component nginx needs to connect to
upstream django {
    server unix:///path/to/project/projectname.sock; # for a file socket

# configuration of the server
server {
    # the port your site will be served on
    listen      80;
    # the domain name it will serve for
    server_name; # substitute your machine's IP address or FQDN
    charset     utf-8;

    # max upload size
    client_max_body_size 75M;   # adjust to taste

    # Django media
    location /media  {
        alias /path/to/projectname/media;  # your Django project's media files - amend as required

    location /static {
        alias /path/to/projectname/static; # your Django project's static files - amend as required

    # Finally, send all non-media requests to the Django server.
    location / {
        uwsgi_pass  django;
        include     /path/to/projectname/uwsgi_params; # the uwsgi_params file you installed
sudo ln -s /etc/nginx/sites-available/projectname /etc/nginx/sites-enabled

Step 3: Configure uwsgi

Copy the uwsgi_params file to your project

sudo cp /etc/nginx/uwsgi_params /path/to/projectname/

Add the file /path/to/projectname/projectname_uwsgi

# projectname_uwsgi.ini file

# Django-related settings
# the base directory (full path)
chdir           = /path/to/projectname
# Django's wsgi file
module          = projectname.wsgi
# the virtualenv (full path)
home            = /path/to/virtualenv

# process-related settings
# master
master          = true
# maximum number of worker processes
processes       = 10
# the socket (use the full path to be safe
socket          = /path/to/projectname/projectname.sock
# ... with appropriate permissions - may be needed
chmod-socket    = 664
# clear environment on exit
vacuum          = true

Enable Emporer mode

sudo mkdir -p /etc/uwsgi/vassals
sudo ln -s /path/to/projectname/projectname_uwsgi.ini /etc/uwsgi/vassals/

Enable autostart

sudo vi /etc/rc.local

Add the line below before 'exit 0'

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