Skip to content

Setting up Django with Ubuntu 12.10, SQLite, Gunicorn, and Nginx on a Digital Ocean droplet

September 1, 2013

Quite the title but I want to be as descriptive as possible.  I have been using the tutorial at and though its worked, I had some troubles.

First thing you will want to do is follow the tutorial to make sure that your domain is properly linked on your droplet. Follow this tutorial and give it some time to ensure that the DNS is working.  Then you can follow this tutorial which will help you secure your root user and get a normal user setup.  And lastly, we want to make sure that all our packages are up to date.  Under your normal user, run the commands:

sudo apt-get update
sudo apt-get upgrade

Setup virtualenv

As I understand it, the virtual environment is used to store and run many of the programs needed to keep your django server afloat. Most importantly, in your virtualenv you can have the correct versions of the programs installed so you don’t accidentally install an update that breaks how your django app works.  Now we will run two commands. The first installs virtualenv and the second sets up a virtualenv that we will use for django and the other programs it needs.

sudo apt-get install python-virtualenv
sudo virtualenv /opt/myenv

Now you have your virtualenv installed and setup. You could install it anywhere but I simple follow the guide I’ve been using for setting up the virtualenv.  You can activate and deactivate the virtualenv very easily.  The first of the following two commands will activate it, while the second command deactivates it.  You can tell that your virtualenv is activate by checking your prompt. If it starts with (myenv), you have it activated.

source /opt/myenv/bin/activate

Install Django and Gunicorn under your virtualenv

We will want to install Django within our virtualenv.  This is where I initially had trouble due to permission issues.  I will touch on this in just a little bit.  First we want to activate our virtualenv, then use pip to install django.  Pip is a tool for installing python packages and it is typically just as easy to use as apt-get.  Run the following commands

source /opt/myenv/bin/activate
pip install django

Now if you got OSError: [Errno 13] Permission denied: you need to change the permisions of the /opt/myenv/ directory so your current user can modify it.  You can use the following command to modify the permissions of the directory and all sub-directories.  Be sure to change user:user to your username “reimus:reimus”

sudo chown -R user:user /opt/myenv/

The command chown changes the ownership of the directory.  The option -R tells the command do run recursively so it changes everything within the directory and all its children.  And user:user is the new ownership.

Gunicorn is the HTTP server used to run your Django apps.  It is intended to be fast and light weight.  We will install it quickly in this step, and work with configuring it later.  Make sure that you still have your virtualenv active.

pip install gunicorn

Install SQLite3 and Nginx

We do not need or want to install SQLite3 under our virtualenv nor Nginx so you can deactivate and use apt-get to install everything that is needed. SQLite will be our database for django.  Nginx is a reverse proxy server which is used to prevent the direct access of files within your server.

sudo apt-get install sqlite3 libsqlite3-dev
sudo apt-get install nginx

I will touch on the configuration of Nginx later.

Setup a Django project

Now that we’ve established the groundwork for Django we will create a Django project, initialize our SQLite database, and configure the project to run under Gunicorn. We need to make sure that our virtualenv is active and change our working directory to the root of that virtualenv then use Django to create our project.

source /opt/myenv/bin/activate
cd /opt/myenv startproject myproject

The third command above will create a new Django project named myproject in your current working directory.  run ls and you should see the new folder in /opt/myenv/.  Now a lot has happened with that command.  There are two essential files you will contantly be interacting with.  ./myproject/ and ./myproject/myproject/  The former will be used to run your Django app and the former contains essential settings that are used to properly run your project.

We will want to modify the file so it knows that we want to use our SQLite database and also that we want to use Gunicorn.  Open the settings fille with a text editor like nano and modify some of the text describted below.

nano ./myproject/myproject/
import os

BASE_DIR = '/absolute/path/to/myproject'    # This points to the directory where is
    'default': {
        'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'mysql', '$
        'NAME': os.path.join(BASE_DIR, 'database.db'),                      # Or path to database file if using sql$
        # The following settings are not used with sqlite3:
        'USER': '',
        'PASSWORD': '',
        'HOST': '',                      # Empty for localhost through domain so$
        'PORT': '',                      # Set to empty string for default.

And now we want to add Gunicorn as an installed app.  Find the INSTALLED_APPS tuple in your file and add in gunicorn like so (Remember to add the comma).

    # Uncomment the next line to enable the admin:
    # 'django.contrib.admin',
    # Uncomment the next line to enable admin documentation:
    # 'django.contrib.admindocs',

Now down the road we will want a place to store our static files, we will have to let Django know where those files are by changing STATIC_ROOT in

STATIC_ROOT = '/opt/myenv/static/'

With the settings file updated we can use Djangos to create our database.db. After running the command, it will ask you if you would like to create a superuser for Django’s auth system. I’ve opted not to for the moment. Change your working directory to /opt/myenv/myproject/ and run the syncdb command.

cd ./myproject
python syncdb

If you run ls you should now see a database.db file.

Confugure Nginx

We are almost done.  We need to setup a configuration file for Nginx.  This configuration file is located in /etc/nginx/sites-available/myproject and we will make a symbolic link in /etc/nginx/sites-enabled/ which will point to the configuration file.

sudo nano /etc/nginx/sites-available/myproject

Now add in this text. Be sure to alter the red text when needed.  Changing the server name is required.

server {
    listen 80;
    access_log /var/log/nginx/example.log;

    location /static/ { 
        alias /opt/myenv/static/; 

    location / {
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

And run these commands to create the symbolic link.

cd /etc/nginx/sites-enabled
sudo ln -s ../sites-available/myproject

Now you can start the Nginx service.  Keep in mind that if you alter the configuration file you must restart Nginx for the changes to take effect. Both commands are given below.

sudo service nginx start
sudo service nginx restart

Starting your Django server

There you have it, the configuration is done.  In order to get your server running you must have yoru virtualenv activated, your working directory should be at your projects root, and you run the run_gunicorn command through  Keep in mind that Nginx needs to be running which was done above.

source /opt/myenv/bin/activate
cd /opt/myenv/myproject
python run_gunicorn --workers=1

Now you should be able open your website in a webbrowser and see Djangos welcome screen.  You can follow this tutorial to use supervisor to run your django app automatically and get it to run in the background.

From → Uncategorized

  1. Thank you for this! I was having trouble getting through some of the other tutorials, but this one worked like a charm!

  2. Raghav permalink

    I followed your tutorial, but could not get everything to work –

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: