Django and Red Hat logos
25 SEP

Originally posted in 2014 on

A Recap

In part 1 of this post, I went through the process I used to install Python 2.7 and Django 1.7 on a Linux Red Hat RHEL 6.6 box. We left off at the point where a Django app was running on the built-in Django server in development mode. Today, I will go through the steps I took to deploy it, using Apache and mod_wsgi.

The Implementation

The first thing that I did, was to install mod_wsgi. This is the part that probably took the longest, because I was running into all kinds of issues. One of the things that I did (or didn’t do) was to leave an old, global install of Django 1.4 and mod_wsgi which worked with the global Python 2.6, instead of just uninstalling it first. Huge mistake. Not that it prevented the virtualized app from working, but it made debugging elusive because while I thought I was running off of my brand-spanking-new Django 1.7, I was in fact running 1.4. Turns out that although the virtual host in Apache was set up correctly, my was running the global system. So lesson learned. Anyway, here is what I did for mod_wsgi:

cd ~/src
sudo yum install httpd-devel # must have this as it installs apxs -

cd mod_wsgi-4.4.3
./configure --with-apxs=/usr/sbin/apxs --with-python=/home/zjasek/.localpython/bin/python # IMPORTANT: --with-apxs & --with-python must be there to compile mod_wsgi for the local, 64 bit system
sudo make install
sudo touch /etc/httpd/conf.d/wsgi.conf
sudo nano wsgi.conf

And in wsgi.conf, add the following:

LoadModule wsgi_module modules/

The last part is not exactly the way I’ve seen others implement it, but it works as expected for me, so I left it alone.

The next action was to create a virtual host in Apache:

WSGIPythonPath /home/zjasek/testapp:/home/zjasek/env/py2.7/lib/python2.7/site-packages
WSGISocketPrefix /var/run/wsgi
<VirtualHost *:80>
  DocumentRoot /home/zjasek/testapp
  Alias /static/ /home/zjasek/env/py2.7/lib/python2.7/site-packages/suit/static/

  <Directory /home/zjasek/testapp >
      Allow from all
  <Directory /home/zjasek/env/py2.7/lib/python2.7/site-packages/suit>
    Allow from all
  WSGIScriptAlias / /home/zjasek/testapp/
  WSGIDaemonProcess testapp python-path=/home/zjasek/testapp:/home/zjasek/env/py2.7/lib/python2.7/site-packages
  WSGIProcessGroup testapp

Now with that in place, I changed my `` file to include the necessary files. So now it looks like so:

import os, sys
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
And one final modification because my app is having a hard time referencing its own resources:

# ROOT_URLCONF = 'testapp.urls'
# WSGI_APPLICATION = 'testapp.wsgi.application'

WSGI_APPLICATION = 'wsgi.application'
Restart Apache ...

sudo /etc/init.d/httpd restart

And that’s it. Going to I see the login page and after logging in, I see the familiar Django admin interface. Yay. Just to make sure all is working ok, I navigated to the admin superuser I created earlier and change my password. All good.

I’m certain that there are more and better ways to accomplish this, but this way worked well for me. It also provided some quality education in the process, so I hope this could be of use to someone else. Until next time, code on.