Django and Red Hat logos
25 SEP

Originally posted in 2014 on absoluteweb.solutions

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 wsgi.py 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 - http://stackoverflow.com/questions/16854750/issues-installing-mod-wsgi-cannot-find-makefile-in
wget https://github.com/GrahamDumpleton/mod_wsgi/archive/4.4.3.zip
unzip 4.4.3.zip

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
make
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/mod_wsgi.so

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>
  ServerName dev.testapp.com
  ServerAdmin webmaster@example.com
  DocumentRoot /home/zjasek/testapp
  Alias /static/ /home/zjasek/env/py2.7/lib/python2.7/site-packages/suit/static/

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

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

import os, sys
sys.path.append('/home/zjasek/env/py2.7/lib/python2.7/site-packages/')
sys.path.append('/home/zjasek/testapp')
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'


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

sudo /etc/init.d/httpd restart

And that’s it. Going to http://dev.testapp.com/admin/ 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.

References
http://stackoverflow.com/questions/16854750/issues-installing-mod-wsgi-cannot-find-makefile-in
http://www.harelmalka.com/?p=159