Wiring uWSGI to work with Django and nginx on Ubuntu 16.04

21,648

The first modification needed is to the /etc/uwsgi/sites/firstsite.ini file. The only change needed is replacing the permissions from 664 to 666. The script would look like this:

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

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

master = true
processes = 5

socket = %(base)/%(project)/%(project).sock
chmod-socket = 666
vacuum = true

Secondly, as we're using systemd rather than upstart, the following file is not needed and can be removed: /etc/init/uwsgi.conf

Third, we create the following systemd script at /etc/systemd/system/uwsgi.service:

[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

Refresh the state of the systemd init system with this new uWSGI service on board

sudo systemctl daemon-reload

In order to start the script you'll need to run the following:

sudo systemctl start uwsgi

In order to start uWSGI on reboot, you will also need:

sudo systemctl enable uwsgi

You can use the following to check its status:

systemctl status uwsgi

Some further details can be found here.

Share:
21,648

Related videos on Youtube

Vlad Schnakovszki
Author by

Vlad Schnakovszki

Updated on September 18, 2022

Comments

  • Vlad Schnakovszki
    Vlad Schnakovszki over 1 year

    I am trying to follow this tutorial to setup uWSGI with Django and nginx on Ubuntu16.04.

    It all works fine up until the very last step (oh the irony...) where I try to execute this command:

    sudo service uwsgi start
    

    If fails with the following error:

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

    Others seem to get a similar error:

    Failed to start uwsgi.service: Unit uwsgi.service failed to load: No such file or directory.

    The issue appears to be related to the version of Ubuntu. While that tutorial is aimed at Ubuntu 14.04, it seems it will not work for newer versions because in version 15 Ubuntu switched from the upstart init daemon to the systemd init daemon.

    How can I use systemd to launch uWSGI so that it works with nginx and Django?

  • tlng05
    tlng05 almost 8 years
    How do you make systemd use a python virtualenv? When I run systemctl status uwsgi I get ImportError: No module named site.
  • Rakib
    Rakib over 7 years
    epic..... i was struggling for so long with this.... thank you for the straight-forward easy step-by-step solution you have written here. +1
  • jozi
    jozi over 7 years
    I used it a few times ,very helpful answer