Django LAMP

Over the past year and a half I have been using WordPress to publish my web site & blog. Prior to WordPress I was using Calabro which is build with Python on TurboGears. WordPress is great, but does not give me all the control I need. I have recently decided to move back into the world of freelance web development and therefore want to do more with my main web site I wanted to build something that showcases my specialties which are Python, Django, JavaScript, semantic markup, and jQuery so this new web site will be built with all of these great tools. I also thought it would be a good opportunity to document the process that I follow when starting a new project.

I like to get integration and deployment tasks figured out early since these tasks contain the most risk. Here is where I usually start:

  1. Setup the the code repository.
  2. Create the initial project structure; so we have something to deploy and iterate over.
  3. Deploy the initial project to the production web server; Apache on a Linode VPS.
  4. Script the the subsequent deployment process for continuous deployment.

Today I am going to go over step number 3; deploying a LAMP server.


The first thing you are going to need is to get a virtual private server (VPS) or dedicated server account at a hosting company such as Linode or SliceHost. I am using Linode running Ubuntu 10.04 LTS (Lucid Lynx).


Once you have your server you will need to open a console and SSH into your new server:

$ ssh root@{your.ip.address}


Now that we are logged in we need to install the prerequisites:

$ apt-get update
$ apt-get upgrade
$ apt-get install libapache2-mod-python python-mysqldb
$ apt-get install python-setuptools
$ apt-get install mysql-server
$ apt-get install subversion


We want to be on the bleeding edge so we are going to install the trunk version of Django. First we checkout django-trunk to /usr/local/lib:

$ cd /usr/local/lib/
$ svn co django-trunk

Then create our symbolic links to point to our new trunk version of Django:

$ ln -s `pwd`/django-trunk/django /usr/lib/python2.6/dist-packages/django
$ ln -s `pwd`/django-trunk/django/bin/ /usr/local/bin

Also, while we are in this directory we will create a symbolic link to the Django admin media:

$ ln -s `pwd`/django-trunk/django/contrib/admin/media /var/www/media
$ cd


We need to install Git and then clone the project repository. To keep things simple for this tutorial we are going to put the web site under the Apache root.

$ apt-get install git-core
$ cd /var/www
$ git clone git://{YourGitHubAccout/your_django_project}.git


Next we need to configure Apache. I will be using modpython as the python host for Django. Since Django has support for WSGI we could use modwsgi, but I am going to stick with modpython for this tutorial since it has a proven track record in large scale production environments. The only draw back to using modpython over modwsgi is that an Apache restart is required after each deployment. Also, to keep this tutorial simple we are going have Django sever up the static meda files. This is not an optimal setup for a high traffic web site, but for the initial setup it works fine.

To configure Apache will open the default vhost and edit it to look like below:

$ nano /etc/apache2/sites-enabled/000-default
<VirtualHost *:80>
        ServerName {your.domain}.com
        DocumentRoot /var/www
        <Location "/">
                SetHandler python-program
                PythonHandler django.core.handlers.modpython
                SetEnv DJANGO_SETTINGS_MODULE {your_django_project}.settings
                PythonOption django.root /{your_top_level_folder_name
                PythonDebug On
                PythonPath "['/var/www/'] + sys.path"

        <Directory /var/www/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all
        ErrorLog /var/log/apache2/error.log

For the changes to take effect we need to restart Apache

$ /etc/init.d/apache2 restart


We need configure the database. Connect to the mysql server using your mysql user name and password.

$ mysql -u root -pXXXX
$ create database {your_db_name};
$ nano /var/www/{your_django_project}/ # update database connection info

Finally, we have Django create the tables.

$ cd /var/www/{your_django_project}
$ ./ syncdb

If everything was done right we can open a browser and point it to http://{your.ip.address}/home/ and we should be running. The final step would be to open your DSN manager (i.e. godaddy) where your domain is registered and point the @ record to {your.ip.address}

About these ads

4 thoughts on “Django LAMP

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s