How to deploy a django project with Apache

In this guide, we will talk about how to deploy a Django project using Apache and its module, mod_wsgi. This two have a main role in the deploying process.
We will handle the client requests using Apache, which will be the one of delegating the requests that need logic to the Django app through mod_wsgi.

Setting a virtual environment

First, we will install pip
$ sudo easy_install pip
Next, we need to install the virtualenv command to create these environments.
$ sudo pip install virtualenv
When virtualenv gets installed we can create a Python virtual environment by typing:
$ virtualenv exampleve
Then, to be able to use it, we will have to activate it:
$ source exampleve/bin/activate
Once we activate our virtual environment we are ready to add our Django project. All the dependencies we install will be located in our "exampleve" folder.

Setting static files directives

Once our Django project is created, we need to specify the directory for both static and media files on the settings project and then generate them. First, we are going to specify the directory where the files are going to be generated. We will do that by editing the file from our project as follows:
import os

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
MEDIA_URL = '/media/'
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
Next, we are going to collect our static files by typing the following command:
$ python collectstatic
A folder with the static files should show up on our project

Configuring mod_wsgi

Once we have finished developing Django project we are ready to deploy it. First, we need to install the apache module mod_wsgi. If you are on MAC, be sure that XCode is updated. If you are on Linux (in this guide we show how to do it specifically in Debian distributions), be sure that apache is installed. You can install it by typing the following command:
sudo apt-get update & sudo apt-get install apache2
Then we are going to install mod_wsgi. We will use pip for that:
$ sudo pip install mod_wsgi
Once it gets installed we will need to include it into apache’s modules by manually editing the httpd.conf file located in /etc/apache2/httpd.conf.
To do that we are going to run:
mod_wsgi-express module-location
Take the path that outputs and modify Apache configuration (httpd.conf) so it has a LoadModule line for wsgi_module with that path. Here is an example of the line we have to include:
LoadModule wsgi_module /Library/Python/2.7/site-packages/mod_wsgi/server/
Then we can check if mod_wsgi is successfully loaded by looking into Apache's loaded modules:
$ apachectl -t -D DUMP_MODULES | grep wsgi
wsgi_module should show up on the output.
Once we get mod_wsgi installed and activated, we have to take a look at our Apache server’s httpd.conf file. At the end of the file, we can see that it imports all the .conf files from “/etc/apache2/other/”. To take advantage of this, we will create a new file there, which can be called django.conf for example, with the following configuration:
WSGIScriptAlias / /path/to/
WSGIDaemonProcess example python-home=/path/to/virtualenviroment/ python-path=/path/to/project/
WSGIProcessGroup example

        Require all granted

   Alias "/static/" "/Users/andres/Desktop/example/static/"

     Require all granted
     Options +Indexes
  • WSGIScriptAlias indicates the path to the directory to our file on our Django project
  • WSGIDaemonProcess indicates the path to our virtual environment and the path to our project. Both grouped under the WSGIProcessGroup name
Finally, we just have to restart the apache server one more time to implement the changes made and that’s it!
$ sudo apachectl restart


Everything should be set now. Go to see your Django project working.
Mountain View
If you want a sample project you can check the link below where you have the default Django project.


  1. I'm completly lost, where do i run this "mod_wsgi-express module-location"

  2. Attend The Best Python Training in Bangalore From ExcelR. Practical Python Training in Bangalore Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Best Python Training in Bangalore.


Post a Comment