Linode, Nginx, uWSGI, Django, systemd, sockets

How to run Django 1.10 on a Linode deploying Fedora 24, using uWSGI and Nginx. Nginx is the webserver and the first contact for any requests from the internet which will be made on port 80. Django sits behind the Nginx server, but to communicate with Nginx, Django needs a gateway interface, this is uWSGI or ‘u WebServer Gateway Interface’. The ‘u’ distinguishes it from the earlier ‘WSGI’.

Having installed a virtualenv and activated it, next install uwsgi with,,

$ sudo pip install uwsgi

Presuming you have Django running and have sorted your static files out, proceed.

Systemd

Fedora 24 uses systemd to run /etc/systemd/system/uwsgi.service

This service can be started using,

$ sudo systemctl start uwsgi.service

To stop the service replace ‘start’ with ‘stop’. Also you could use ‘restart’. Sometimes it is necessary to reload systemd. This can be done with,

$ sudo systemctl daemon-reload

To run the service when the suystem boots use,

$ sudo systemctl enable uswgi.service

uwsgi.service

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

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

[Install]
WantedBy=multi-user.target

This calls the uwsgi binary file in the Django virtual environment at /home/ofey/djangoenv/bin/uwsgi
Also this calls /etc/uwsgi/sites which contains the initialisation file djangoForum.ini

[uwsgi]
project = djangoForum
base = /home/ofey

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

master = true
processes = 2

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

This file could be replaced with a commandline command such as,

$ sudo uwsgi --chdir /home/ofey/djangoForum --socket djangoForum.sock --module crudProject.wsgi --virtualenv /home/ofey/djangoenv --chmod-socket=664

Configuration directives, such as those found in the .ini file and command line options such as those above are managed by the same parser. The ini format differs from the command line only in that you need to remove the double dashes.

But an .ini file will be needed if you wish to run it in the background using systemd.

With the .ini file there is no need to activate the virtual environment or to run the Django server which would normally be done with,

$ python manage.py runserver

Django runs on port 8000 by default and Nginx listens on port 80. Between the two is the websocket which we will run on port 8001. Seems ports can be replaced with sockets. So it probably makes no sense to say “run a socket on port x”

Useful site in the Linode documentation for setting up Django, uWSGI and Nginx on Ubuntu,
https://www.linode.com/docs/websites/nginx/deploy-django-applications-using-uwsgi-and-nginx-on-ubuntu-14-04

This link also breaks down the process quite well,
https://gist.github.com/evildmp/3094281