Not logged in. Login

Deploying Django + Nginx + PostgreSQL

You are free to choose other technologies (like Apache or MySQL), but I can't provide instructions for all possible combinations.

You will need Nginx and PostgreSQL installed, as you did in Exercise4. This should include a config file for /etc/nginx/sites-available/default, and creating a PostgreSQL database with the ubuntu user having all permissions on it.

Django + PostgreSQL

You will need the packages postgresql-server-dev-all, libpython-dev, and python-pip package installed, and execute the command pip install psycopg2 to get the connector library installed.

Set your database to PostgreSQL

'default': {
    'ENGINE': 'django.db.backends.postgresql_psycopg2',
    'NAME': 'mydb',
    'USER': 'ubuntu',

Django + Nginx

Make sure you're forwarding port 80 in your Vagrantfile.

The easiest way to get Django running under Nginx seems to be uwsgi. Not that it's easy; just that it's easiest. [Instructions based on Setting up Django with uWSGI and nginx.]

You'll need to install the pip package uwsgi.

In your Nginx config file, set your uwsgi app as the server root:

    location / {
        proxy_pass  http://localhost:8000;

You will need a uwsgi.ini file in your repo, which will serve as a recipe to start up the uWSGI process:

chdir           = /home/ubuntu/project/mysite
module          = mysite.wsgi

master          = true
processes       = 10
http            = localhost:8000

uid             = ubuntu
gid             = www-data

This runs your WSGI processes as the ubuntu user, not as root or the default web server user: it's best practice for security to use a minimally-privileged user for that process.

Finally, you'll need to start the uwsgi process on system start. You can do that from /etc/rc.local

sleep 10
/usr/local/bin/uwsgi --ini /home/ubuntu/project/mysite/uwsgi.ini --daemonize /var/log/mysite.log

[The sleep here gives a few seconds for the shared directory to get itself mounted before trying to start the server.] You'll probably also want to run the command /etc/rc.local in your recipe so it gets started on the first provisioning.

You can test that your uWSGI process is running by accessing it by HTTP from the VM: curl -i http://localhost:8000

You may or may not want to set DEBUG = False in your This will prevent errors from being shown in the web frontend, so it may not be good while developing.

Static Files

In your, give a location for your static files which is outside your code directory:

STATIC_ROOT = '/home/ubuntu/static'

In your provisioner, run this command to get the static files collected in that directory:

python collectstatic --noinput

See DeployStatic for information on getting static files served properly from that location.

Updated Mon Aug. 30 2021, 07:36 by ggbaker.