You could earn up to $300 by adding new articles!

Get Started Now

Setup Django on Debian 8

Published on: Tue, Jun 2, 2015 at 9:42 pm EST

This tutorial explains how to setup Django on Debian 8 (jessie). I will show how to use both Python 2 and 3 as well as Nginx and PostgreSQL.

Everything done in this tutorial is done as root.

Install Packages

To start out, we need to install some packages.

For Python 2:

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

For Python 3:

apt-get install python3-pip python3-dev virtualenv nginx postgresql postgresql-contrib libpq-dev sudo gcc

Setup Databases

First, we log into the user postgres.

sudo -u postgres -s

Next, we create a new database. The database name can be whatever you want it to be (dbname is used here), but you must be consistent with the rest of the setup.

createdb dbname

Create a user for the new database. Again, this can be whatever you desire it to be, but I used dbuser. This will also ask you to set a password.

createuser -P dbuser

The user must now be given access to the database. Just be sure to use the correct database name and user name.

postgres=# GRANT ALL PRIVILEGES ON DATABASE dbname TO dbuser;
postgres=# \q

Exit to root.


Enable and start PostgreSQL:

systemctl enable postgresql
systemctl start postgresql

Setup the Virtual Environment

Instead of just using the global python files, we will be using a virtual environment. We start by creating a directory to hold it all.

mkdir -p /opt/project

We now create the environment. This is different for python 2 and 3 users.

For Python 2:

virtualenv . -p python

For Python 3:

virtualenv . -p python3

Enter the virtual environment.

source bin/activate

Install Django, gunicorn, and psycopg2.

pip install django
pip install gunicorn
pip install psycopg2

If you need a specific version of Django, change the install command to match the format below. This example installs 1.7.8.

pip install django==1.7.8

We are now done with that for now, so we can deactivate our virtual environment.


Project Upload and Configuration

This is the time we upload our project to the server, and we make sure that all of its settings are correct. You can use any method to do this. FTP, SFTP, git, etc are all ways of doing this. If you are using git to track the project's code, you can just clone it to the server. This git command will clone the project to the server and place it in /opt/project/project/.

git clone

Open the file in any text browser.

First things first, debug mode needs to be off. Look for the DEBUG = True line and change True to False. After this, make sure that you have ALLOWED_HOSTS set to some value.


Look for the DATABASES dictionary, and it should look like this:

    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'dbname',
        'USER': 'dbuser',
        'PASSWORD': 'password you set',
        'HOST': 'localhost',
        'PORT': ''

Last step here is to set a static root. Place the following directly below STATIC_URL.

STATIC_ROOT = '/opt/project/static/'

Exit the file and create the static root directory.

mkdir -p /opt/project/static

Now migrate the database, create a super user, and collect all static files.

cd /opt/project/project
../bin/python makemigrations
../bin/python migrate
../bin/python createsuperuser
../bin/python collectstatic

Configure Gunicorn

Gunicorn is the WSGI server that we will be using. Since Debian 8 comes with systemd, we will take advantage of systemd to start and stop the server.

Create the file /etc/systemd/system/django.service and add the following content.

Description=Django with Gunicorn

ExecStart=/opt/project/bin/gunicorn --workers 3 --bind project.wsgi


Enable and start the service that we have created.

systemctl enable django.service
systemctl start django.service


You may have noticed that we bound the gunicorn server to Now we need a way to access it from outside the server. This is where Nginx comes in.

Create the new file /etc/nginx/sites-available/django and add the following. The part can be set to whatever you need it to be.

server {
        listen 80;
        access_log off;

        location /static/ {
                alias /opt/project/static/;

        location / {

Create a symbolic link to enable this site.

ln -s /etc/nginx/sites-available/django /etc/nginx/sites-enabled/django

Enable and start Nginx.

systemctl enable nginx
systemctl start nginx


Congratulations, you now have a working Django site up on your Debian VPS.

Want to contribute ?

You could earn up to $300 by adding new articles!

Get started in the SSD Cloud!