Deploying Django Project on Apache Using mod_wsgi

Fellow programmers, Hello. If you are developing  a web application using Django, I am sure you’ll reach that cusp where your application is running smooth like butter and you start facing scalability issues, Best way to address this is to migrate your Django project from single-threaded server to some good HTTP server like apache. Unfortunately, apache by itself won’t support any python applications to run on it, we need to add few nuts and bolts to do that and I’m gonna tell ya how to add those. 

This post flies you over step-by-step instructions on deploying your Django application on apache using mod_wsgi on Ubuntu. Why mod_wsgi is better that mod_python etc is discussed here.

Assumption: You are on Ubuntu Operating System and you have django installed, you’ll be running the following commands as sudo (run sudo -s before proceeding with the setup) and your Django project called ‘myweb’ is sitting in ’/home/foo/’ directory.

Step:1:: Install apache2 and mod_wsgi using following command:

sudo apt-get install apache2 libapache2-mod-wsgi 

Step:2:: Create your wsgi script file say myweb.wsgi (for simplicity, lets create it under /home/foo/myweb/) with the following contents:

Step:3:: Create your apache conf file (lets call it myweb.conf) under ’/etc/apache2/sites-available/’ directory with the following contents:

Step:4:: Tell apache to enable your site using myweb.conf by running the a2ensite command from ’/etc/apache2/sites-available/’ directory:

sudo a2ensite myweb.conf

Step:5:: Restart apache by running the following command:

sudo service apache2 restart

Step:6:: Add a host entry in your ’/etc/hosts’ file to point to correct IP address which will look like this

 *note: If you have your own server, change as needed

Now, you are all set, hit '’ in your web browser and it should take you to your homepage.

Step:7:: (Optional):: Access access permissions to apache to access your resource folders, coz sometimes, your Django application loads up properly but gives you 404 for certain files or templates even if they present.

 sudo chown -R www-data myweb/templates/