Serve Django Applications With mod_wsgi and Apache on Windows Server

Ahmad Al-Sajid
2 min readMay 21, 2019

Django is a very powerful and handy web development framework. In my current job at Venture Solutions Ltd, we mainly develop products using Django. Django has a simple server to test in development that is very useful to developers, but not usable in production. So, we need a reliable and powerful web server like Apache, Nginx, Gunicorn, etc. Basically, we develop applications in Linux environments ( Ubuntu, to be exact ) and use Gunicorn with Nginx as web servers in productions. But recently, I had to use Windows server instead of Ubuntu server, where Gunicorn with Nginx isn’t friendly. That is why I had to use mod_wsgi with Apache to serve the application. Now we will discuss how we can install and configure Apache to serve our Django application.

Python

We will use python3.6 for our development. This version needs visual c++ 14 installed in the system. You can get the details here. Make sure vc++14 is installed.

Django

Suppose, we have a Django application named my_project located at C:/Users/Administrator/Desktop/. So, the structure would be something like below

my_project
|__ another_app
| |__ migrations
| |__ views.py
| |__ models.py
| ...
|__ my_project
| |__ __init__.py
| |__ settings.py
| |__ urls.py
| |__ wsgi.py

WAMPServer

We will use WAMPServer for Apache. On windows, it has some dependencies on visual c++ redistributables. We can get them from Microsoft site or from here as a zip file (I prefer the second as you can get all the vc++ in one place). Make sure you have installed al the dependencies before installing wamp server and installing it as Administrator. After that, install WAMPServer. The default installation path should be in C:/, i.e. C:\wamp64 . If WAMPServer is correctly installed and all of its services are running, then you will see the wamp server icon turned green.

After installing wamp, create an Environment Variable (system variable) named MOD_WSGI_APACHE_ROOTDIR with the value C:\wamp64\bin\apache\apache<version>\. You will have to restart the command prompt for this change to take effect.

Rewrite wsgi.py

The default wsgi.py file works on Linux based systems. But, on windows, it needs some additional contents. So, we will create another file my_project/my_project/wsgi_windows.py and copy the contents of wsgi.py and modify it here.

mod_wsgi

When wamp is installed and configured correctly, install mod_wsgi using pip by running

pip install mod_wsgi

Now, get the Apache configuration by running

mod_wsgi-express module-config

this will output something like below

Copy these lines and paste to end of C:\wamp64\bin\apache\apache<version>conf\httpd.conf. Then open the vhosts file found at C:\wamp64\bin\apache\apache<version>\conf\extra\httpd_vhosts.conf and configure it like below

Now, go to the wamp server, and restart all services. When the services are restarted, you will see the wamp server icon to be green again. After that, you can browse http://localhost for your Django application.

References:

  1. https://www.codementor.io/aswinmurugesh/deploying-a-django-application-in-windows-with-apache-and-mod_wsgi-uhl2xq09e
  2. http://myembeddedlinux.blogspot.com/2017/11/host-django-app-in-apache-windows.html
  3. http://wampserver.aviatechno.net/?lang=en
  4. https://wiki.python.org/moin/WindowsCompilers

Sign up to discover human stories that deepen your understanding of the world.

Ahmad Al-Sajid
Ahmad Al-Sajid

Written by Ahmad Al-Sajid

Software Engineer, DevOps, Foodie, Biker

Responses (4)

Write a response

Nice tutorial, but it does not work. :S My Apache is in orange, the Apache.log is empty. I have no idea what's going on, but it does not work...

--

The command "mod_wsgi-express module-config" doesn't work because "mod_wsgi-express" is not recognized.

--

Thank you very much. It works for me. You saved me from four days of hard work.

--