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 deployement 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.4 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
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.4 deployement on Ubuntu 12.04
So we start of with updating the system.
Choose here name for your server. Modify SERVERNAME.
1 2 3
Install software and dependencies
Type in the next three lines to install Apache2, Python, VirtualEnv and PIP.
1 2 3
Install and enable mod_WSGI
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.
1 2 3 4 5 6 7 8 9
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.
1 2 3 4 5
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
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 deployement. Follow the commands strictly so you will end up in the right directories.
1 2 3
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.
1 2 3
Open nano to create a configuration file
Paste the following code into the editor and save it.
1 2 3 4 5 6 7 8 9 10 11 12
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
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.
1 2 3
There you need to edit your own specific MySQL details. Also do these edits
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.
Now you have completely installed Django framework on a working Ubuntu server installation. Deployement 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.