Django deployment installation to Ubuntu 12.04 server

Django is a web framework written in Python and as you may guess everything in Django is also done in Python. Django was developed to simplify the creation of database driven sites and principle of it is “don’t repeat yourself”. Best feature in Django is the fact that it propably is the fastest website framework to create a fully functioning website.

When I developed a website with Django I stumbled upon one step, and that step was the deployment and publishing of Django driven website. There are a good tutorial about deploying Django on the official website and I followed it. Anyway there are alot of things in that tutorial that are not explained and you just need to know.

So here is a full step by step tutorial for deploying Django 1.6 web framework. I have tested this tutorial with Ubuntu 12.04.

Requirements for this tutorial

  • Linux knowledge
  • Fresh Ubuntu 12.04 installation on a server
  • Root access to that server
  • Domain

Environment

In this tutorial the user we are using is root and we will install Django in /srv/ folder. Of course you may use another user and sudo everything, and install in some other directory, but this tutorial does not scale to that. This Django installation is safe and secure and we are going to create a new system user to run mod_WSGI with Apache. Python will be handled by VirtualEnv.

Django 1.6 deployment on Ubuntu 12.04

So we start of with updating the system.

Hostname

Choose here name for your server. Modify SERVERNAME.

Install software and dependencies

Type in the next three lines to install Apache2, Python, VirtualEnv and PIP.

Install and enable mod_WSGI

We will wget the latest mod_wsgi, since the time I checked Ubuntu only offered outdated version. Last commands here will add the mod to Apache2 and disable Apaches default site.

Install and configure MySQL

Here we will install MySQL and create database, database user and give privileges for that user to the database. Be sure to modify DATABASENAME with the database name of your choice, DATABASEUSER with the database username of your choice and of course PASSWORD with the password for that database user.

Install Python environment and Django

First we create couple of folders for Django and then we will create virtual Python environment with VirtualEnv. Finally we get Django from PIP.

Start Django project

Next we’ll start an example Django project. Basically Django is now fully installed but we have to start some project to show all of the complicated steps of deployment. Follow the commands strictly so you will end up in the right directories.

mod_WSGI Configuration File

We need to make a configuration file for mod_WSGI which will be used by Apache2. First create some folders for Django and move into configuration folder.

Open nano to create a configuration file

Paste the following code into the editor and save it.

Create system user

Now we will create a system to run Apaches’ mod_WSGI process. Make sure to edit USERNAME. Last command will set a shell to that user.

Apache Site Configuration

I have created an example site configuration which works on normal network environments. Open nano to the site file with the following command. Remember to edit DOMAIN with your own domain name.

Paste the following to the editor and edit DOMAIN with your own domain name and USER with the username we just created a while ago.

Now we just need to activate that configuration to Apache. Edit DOMAIN again to your own.

MySQL dependency to Python

Next we need to install MySQL-module for our Python environment

Configuration of Django

Move to project folder and edit settings.py file.

There you need to edit your own specific MySQL details.

Also add these lines to the settings file MEDIA_ROOT = '/srv/my_project/app/webapp/site_media/' and TEMPLATE_DIRS = '/srv/my_project/app/templates/',

Set permissions and restart Apache

Now we have to set permissions for the user we made while ago. Edit USERNAME to your own.

We have done everything to deploy Django so, all we need to do in order to get Django working is to restart Apache webserver.

You should see something like this on your domain website.

Hello World Django

Now you have completely installed Django framework on a working Ubuntu server installation. Deployment is done to the part where we need here an actual website.

I hope this tutorial has helped and please leave a comment or contact me from the about page, if you have any questions.

Django apps and actual website

If you are new to Django and want to build fully functional website. I suggest you start from the official tutorial which I think is one the best learning tutorials around web frameworks.

  • Damian T.

    Great tutorial but I think there are some missing lines in /etc/apache2/sites-available/DOMAIN configuration file you posted 😉 Maybe ?

  • http://diypict.com/ Djarum Tujuhenam

    any similar tutorial for deploying Django and Nginx?

    I hope you have one, or create another great write up on Django and Nginx.

    Thank you.