Categories

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.

1

2

apt-get update

apt-get upgrade

Hostname

Choose here name for your server. Modify SERVERNAME.

1

2

3

echo"SERVERNAME">/etc/hostname

hostname-F/etc/hostname

echo-e"\n127.0.0.1 SERVERNAME.local SERVERNAME\n">>/etc/hosts

Install software and dependencies

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

1

2

3

apt-get-yinstall apache2-mpm-worker apache2-dev

apt-get-yinstall python python-dev python-setuptools

easy_install virtualenv virtualenvwrapper 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.

echo"GRANT ALL PRIVILEGES ON DATABASENAME.* TO 'DATABASEUSER'@'localhost';"|mysql-uroot-p

echo"FLUSH PRIVILEGES;"|mysql-uroot-p

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.

1

2

3

4

5

cd/srv/

mkdir-pmy_project/app my_project/app/conf/apache

mkdir-pmy_project/logs my_project/run/eggs

virtualenv my_project/venv

/srv/my_project/venv/bin/pip install django

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.