How to Configure uWSGI with Django and Nginx on Ubuntu 16.04 Using systemd


3 views

When following tutorials for Ubuntu 14.04, you might encounter this error when trying to start uWSGI:

sudo service uwsgi start
Failed to start uwsgi.service: Unit uwsgi.service not found.

This occurs because Ubuntu 16.04 uses systemd instead of upstart as its init system. The old service management commands no longer work.

First, create a service file for uWSGI:

sudo nano /etc/systemd/system/uwsgi.service

Add this configuration (adjust paths for your Django project):

[Unit]
Description=uWSGI Emperor service
After=syslog.target

[Service]
ExecStart=/usr/local/bin/uwsgi --emperor /etc/uwsgi/sites
Restart=always
KillSignal=SIGQUIT
Type=notify
NotifyAccess=all

[Install]
WantedBy=multi-user.target

Create a directory for your uWSGI configs:

sudo mkdir -p /etc/uwsgi/sites

Then create a configuration file for your Django project:

sudo nano /etc/uwsgi/sites/myproject.ini

Example configuration:

[uwsgi]
project = myproject
base = /home/user

chdir = %(base)/%(project)
home = %(base)/%(project)/venv
module = %(project).wsgi:application

master = true
processes = 5

socket = /run/uwsgi/%(project).sock
chown-socket = www-data:www-data
chmod-socket = 660
vacuum = true

Create an Nginx server block for your Django application:

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

Example configuration:

server {
    listen 80;
    server_name mydomain.com;

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

    location / {
        include uwsgi_params;
        uwsgi_pass unix:/run/uwsgi/myproject.sock;
    }
}

Create the socket directory and set permissions:

sudo mkdir -p /run/uwsgi
sudo chown -R www-data:www-data /run/uwsgi

Then enable and start the services:

sudo systemctl enable uwsgi
sudo systemctl start uwsgi
sudo systemctl restart nginx

Check service status with:

sudo systemctl status uwsgi
journalctl -u uwsgi -f

If you make changes to the service file, remember to reload systemd:

sudo systemctl daemon-reload

Ubuntu's switch from Upstart to Systemd in version 15.04 created compatibility issues for many service configurations. When following older tutorials written for Ubuntu 14.04, you'll encounter the "Unit not found" error precisely because the init system changed.

First, create a service file for uWSGI in the Systemd directory:

sudo nano /etc/systemd/system/uwsgi.service

Here's a working configuration template for Django projects:

[Unit]
Description=uWSGI Emperor Service
After=syslog.target

[Service]
ExecStart=/usr/local/bin/uwsgi --emperor /etc/uwsgi/sites
Restart=always
KillSignal=SIGQUIT
Type=notify
StandardError=syslog
NotifyAccess=all

[Install]
WantedBy=multi-user.target

Create a directory for your uWSGI configs and add your Django project configuration:

sudo mkdir -p /etc/uwsgi/sites
sudo nano /etc/uwsgi/sites/myproject.ini

Example configuration for a Django project:

[uwsgi]
project = myproject
base = /home/user

chdir = %(base)/%(project)
home = %(base)/venv
module = %(project).wsgi:application

master = true
processes = 5

socket = /run/uwsgi/%(project).sock
chown-socket = www-data:www-data
chmod-socket = 660
vacuum = true

Configure Nginx to communicate with uWSGI through the socket:

server {
    listen 80;
    server_name yourdomain.com;

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

    location / {
        include uwsgi_params;
        uwsgi_pass unix:/run/uwsgi/myproject.sock;
    }
}

After creating the configurations, enable and start the service:

sudo systemctl daemon-reload
sudo systemctl start uwsgi
sudo systemctl enable uwsgi

To check the service status:

systemctl status uwsgi

If you encounter permission errors with the socket file:

sudo chown www-data:www-data /run/uwsgi/
sudo chmod 775 /run/uwsgi/

For logging uWSGI output to help with debugging:

journalctl -u uwsgi -f