Deploying Django with Apache and mod_wsgi

by Shahid Ayoub

Deploying Django with Apache and mod_wsgi is a tried and tested way to get Django into production. mod_wsgi is an Apache module which can host any Python WSGI application, including Django. Django will work with any version of Apache which supports mod_wsgi.

After months of work, effort, and dedication put forth by developers on their local machines, an application is deployed on a server for production. This blog will simplify the work of developers while deploying their Django projects on production server. First of all, we have to make sure that Apache web server is installed on the production server.

For deploying Django projects on Apache web server, follow the step by step procedure outlined below:

Step-1: If Apache web server is not already installed, install it on production server by using the following command:

             sudo apt-get install apache2

Apache installed on Ubuntu and Debian will keep its configuration files within “/etc/apache2” folder. Some of these configuration files which we use while deploying is discussed briefly below:

  • apache2.conf: is the main Apache2 configuration file. This contains settings that are global to Apache2.
  • ports.conf: houses directives that determine which TCP ports Apache2 is listening on.
  • sites-available: this directory has configuration files for Apache2 Virtual Hosts. Virtual Hosts allow Apache2 to be configured for multiple sites that have separate configurations.
  • sites-enabled: sites-enabled contain symlinks to /etc/apache2/sites-available directory. Similarly, when a configuration file in sites-available is symlinked, the site configured by it will be active once Apache2 is restarted.

Step-2: Getting application files on the server. We can do this by cloning the application from github or by transferring files using SFTP.

Step-3: Setup virtual environment and install requirements for the project. 

Step-4: Create *.wsgi file (e.g. xyz.wsgi) in the project folder .

Step-5: Set the path to project root and virtual environment in *.wsgi file. Contents within *.wsgi file will look like this:

             import os

             import sys

             import site

             path = '/path/to/project root'   #path to project root

             os.environ['DJANGO_SETTINGS_MODULE'] = 'project_name.settings'

             site.addsitedir('/path to /virtual environment/lib/python2.7/site-packages') 

             import django.core.handlers.wsgi

             application = django.core.handlers.wsgi.WSGIHandler()

Step-6: Enabling TCP port for the application in ports.conf file. The code snippet where the port is to be enabled in ports.conf file will look like this:

             NameVirtualHost *:80

             Listen 81  # This line determines the tcp port available for listening

Step-7: Adding *.conf (or any other name ) file to available sites on the server with all details of project root and static root paths. It should also specify the path to *.wsgi file. Content within the *.conf file will look like this:

<VirtualHost *:81>

          DocumentRoot /path/to/project root

          Alias /static/ /path/to/static folder/

          <Directory /path/to/project root>

                      Order allow,deny

                      Allow from all


          WSGIScriptAlias / /path/to/wsgi/file/*.wsgi

          ErrorLog ${APACHE_LOG_DIR}/error.log


Note: We have to define the same port here which is available in ports.conf file.

Step-8: We can enable the site and it will be symlinked to sites-enabled by using following command:

              sudo a2ensite site_name.conf

Step-9: Restart Apache web server after finishing the above mentioned steps using following command:

              sudo service apache2 reload

We are good to go, we can start using the application on the production server. Just type the IP address of server along with port number on your browser (Example

For any query drop a comment here and I will get back to you.  

Leave a Reply

Your email address will not be published. Required fields are marked *

Tools & Practices

Tools and Technologies we use at Applied

Contact us now

Popular Posts