Showing posts with label webdev. Show all posts
Showing posts with label webdev. Show all posts

Monday, April 30, 2012

Deploying Apache with Django on Ubuntu Linux

So, the other day I was trying to start of my Django on a production server on Ubuntu 12.04 LTS.

Prelims: What is Django and the difference between production and development server?

You know what is Django. Otherwise you shouldn't be here. Now, Django comes packed with a development server. This means you can start off with Django without any server installed (e.g. Apache). Django has its own mini-server. However, if you're deploying the site online, you need a production server e.g. Apache. So, our problem statement is to allow Django to use Apache.

Let's Roll

You should have your Django already installed. Now, you need to install mod_wsgi. This will be used as a connector for Django to Apache. Also, install python-mysqldb if you will be using mysql database. 

Open your terminal:
    $ cd  /var/www/
    $ django-admin.py startproject mysite

(Note that if you don't have the permission, then do it as root i.e. using sudo)

Now, you have created your Django project.

Now, open /etc/apache2/apache2.conf. Add the following lines at the end:

WSGIScriptAlias /django /var/www/mysite/mysite/wsgi.py
WSGIPythonPath /var/www/mysite

<Directory /var/www/mysite/mysite>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory> 

Now, restart apache.

Now, open up firefox and fire: http://localhost/django. That's it.

 Now, let me explain a bit. 

/django : this is the alias for your site i.e. how you access it through the client (your browser). If you left it as "/", then you could have accessed your site simply as localhost.
/var/www/mysite/mysite/wsgi.py : this is where your wsgi file resides
/var/www/mysite : this is where you started your project
/var/www/mysite/mysite : this is the directory of the project

You could have started your project elsewhere. Correspondingly, above destinations will change.