Setup Django on Debian 8

Updated on June 3, 2015
Setup Django on Debian 8 header image

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.

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

Exit to root.

exit

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.

deactivate

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 http://example.com:project.git

Open the settings.py 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.

ALLOWED_HOSTS = ['*']

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

DATABASES = {
    '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 manage.py makemigrations
../bin/python manage.py migrate
../bin/python manage.py createsuperuser
../bin/python manage.py 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.

[Unit]
Description=Django with Gunicorn
After=network.target

[Service]
User=root
Group=root
WorkingDirectory=/opt/project
ExecStart=/opt/project/bin/gunicorn --workers 3 --bind 127.0.0.1:8000 project.wsgi

[Install]
WantedBy=multi-user.target

Enable and start the service that we have created.

systemctl enable django.service
systemctl start django.service

Nginx

You may have noticed that we bound the gunicorn server to 127.0.0.1. 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 domain.example.com part can be set to whatever you need it to be.

server {
        listen 80;
        server_name domain.example.com;
        access_log off;

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

        location / {
                proxy_pass http://127.0.0.1:8000;
        }
}

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

Conclusion

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