Miracle Ayodele
Apr 17 · 4 min read
Django with Apache and mod_wsgi

Have you ever tried to deploy an app you worked so hard to develop, only to find yourself jump from one tutorial to the other, and yet you couldn’t figure it out?

Well, my hands are raised, I have been in that situation and trust me some experts out there have faced this challenge before, so relax, you are not the first and won’t be the last.

I will be showing you, how to deploy your Django app with Apache and mod_wsgi, and this will be very detailed.

So why am I sharing this:

I decided to share this, because it was hard for me to get a straightforward reference when I wanted to deploy my app. So what I will be showing you has been used several times, I have never had issues, so you should not have issues deploying a Django app too.

So before going into details, you should know this:

The word “mod_wsgi” is an Apache module which can host any Python WSGI application, including Django. Django will work perfectly with any version of Apache as long as it supports mod_wsgi. I believe you know what Apache is, Apache is a web server just like Nginx and can be used with any application writing in any language.

In this tutorial, it is assumed that you already know how to build a django application or you have an existing django application, if you don’t have a django knowledge, you can check this tutorial: Starting a Django Project by Real Python or you check Creating your first Django Project by Django Girls.

Now into details:

First thing First: Installing the Needful

You should have apache and mod_wsgi installed, for you to deploy your Django app, with this guys (apache and mod_wsgi), you must have them on your server and it is very easy:

For apache, just do this: sudo apt-get install apache2, you can also add this

sudo apt-get apache2.2-common apache2-mpm-prefork apache2-utils libexpat1

For mod_wsgi, if you are using python 3, you should install mod_wsgi using

sudo apt-get install libapache2-mod-wsgi-py3

but for python 2, you can install mod_wsgi using

sudo apt-get install libapache2-mod-wsgi

With all necessities installed, lets get to work.

Apache Configuration:

To make Django work with Apache, you must setup the conf properly. I will advise you have your own conf file instead of editing an existing file. So how do you do this:

  • Navigate to the apache conf directory:
cd /etc/apache2/sites-available/
  • Create your file using the command:
sudo nano new_config.conf
  • Include the code below and save

So, let me point out few lines of code that are important for the smooth running of your application;

  • ServerName, simply means the server you are using, this could be a domain or an IP, so if you have a domain or IP you want your app to be connected to that domain or IP, just replace the IP or domain with the 127.0.0.1.
  • Alias /static, this is very important as Apache will be the one responsible for serving your static files to your app. So the /var/www/project_name/static/ is pointing to the static directory in your app, which is supposed to house all static files used in your app (css, js,images, etc). So make sure you point correctly
  • DocumentRoot, you just need to reference to the root of your project. So, this depends on the location of your project.
  • WSGIScriptAlias, this is another important thing you must not miss out. You use this to point to the wsgi file in your project, and this must be referenced correctly.
  • ErrorLog, In order to effectively manage a web server, it is necessary to get feedback about the activity and performance of the server as well as any problems that may be occurring. So make sure you create a directory called logs, so you won’t get errors.

Having understood, the code, you must disable the default virtual host config file i.e 000.default.conf, and enable the newly created virtual host config file and restart as shown below.

sudo a2ensite new_config.confsudo /etc/init.d/apache2 restart

After restart, you should see a message like this:

[ ok ] Restarting apache2 (via systemctl): apache2.service

Once you get the “OK” message, you have more one thing to do, and that is telling apache to import your python project. The WSGIPythonPath line ensures that your project package is available for import on the Python path; in other words, that import my_project works.

This can be done inside the apache2.config as shown below

sudo nano /etc/apache2/apache2.conf

Then at the later part of the code, add your WSGIPythonPath:

WSGIPythonPath /var/www/my_project

Save and exit, you can also restart your apache and check your app on the browser without the port.

You are also free to turn off DEBUG, if you are on production, don’t worry your static files are taken care of.

I hope this help you and has given you more understanding of how Apache and mod_wsgi works fine with Django.

Miracle Ayodele

Written by

I am a tech lover with a solid understanding of Odoo ERP and Django. I love coding in Python and enjoying writing at www.theodooguy.com

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade