How to set up Django, nginx, postgresql and gunicorn

This post is a guideline that I use to set up Django in linux.

Install nginx, virtualenv, pip and postgresql


apt-get install nginx virtualenv python-pip python-dev libpq-dev postgresql postgresql-contrib

Nginx will act as a proxy server and virtualenv will keep different Django’s instances separated.

Create a folder for the web. I will use /opt/DjangoWeb1/ and install there the virtualenv.


mkdir /opt/DjangoWeb1/
cd /opt/DjangoWeb1/
virtualenv venbdjango1

Log with postgres user and launch psql.


su postgres
psql

Create database for Django with postgresql.


CREATE DATABASE djangoweb1;
CREATE USER djangouser1 WITH PASSWORD 'djangopassword1';
ALTER ROLE djangouser1 SET client_encoding TO 'utf8';
ALTER ROLE djangouser1 SET default_transaction_isolation TO 'read committed';
ALTER ROLE djangouser1 SET timezone TO 'UTC';
GRANT ALL PRIVILEGES ON DATABASE djangoweb1 TO djangouser1;

Activate the virtual enviroment and install Django, gunicorn and support for postgresql.


source venvdjango1/bin/activate
pip install Django
pip install psycopg2
pip install gunicorn

Start Django project


django-admin startproject DjangoWeb1

Modify settings in order add database we have just created.


cd DjangoWeb1
nano DjangoWeb1/settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'djangoweb1',
        'USER': 'djangouser1',
        'PASSWORD': 'djangopassword1',
        'HOST': 'localhost',
        'PORT': '',
    }
}

Migrate the database and create admin user


python manage.py makemigrations
python manage.py migrate

Add a service to run gunicorn as a webserver. I use systemd with this set up. Create a file  /etc/systemd/system/djangoweb1.service


[Unit]
Description=gunicorn daemon
After=network.target

[Service]
User=root
Group=www-data
WorkingDirectory=/opt/DjangoWeb1/DjangoWeb1
ExecStart=/opt/DjangoWeb1/venvdjango1/bin/gunicorn --access-logfile - --workers 3 --bind unix:/opt/DjangoWeb1/DjangoWeb1.sock DjangoWeb1.wsgi:application

[Install]
WantedBy=multi-user.target

Enable and activate the service


systemctl enable djangoweb1.service
systemctl start djangoweb1.service

Last step is to redirect web traffic through nginx proxy.


nano /etc/nginx/sites-available/djangoweb1

Set up the config file to point the socket gunicorn is linked to. For static files add in settings.py . Also allow all hosts to connect to the site.


STATIC_ROOT = '/opt/DjangoWeb1/DjangoWeb1/static/'
ALLOWED_HOSTS = ['*']

server {
    listen 80;
    server_name mydomain.com;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /opt/DjangoWeb1/DjangoWeb1/static;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/opt/DjangoWeb1/DjangoWeb1.sock;
    }
}

Enable the new site by linking in sites-enabled.


cd /etc/nginx/sites-available/
ln -s /etc/nginx/sites-available/djangoweb1 /etc/nginx/sites-enabled/
systemctl restart nginx

Now you can visit the webpage

Welcome Django
Django Website

If you want to create a ftp user to handle the files of the web visit the post How to set up proftd

Leave a Reply

Your email address will not be published. Required fields are marked *