This site runs best with JavaScript enabled.

Setting up Django on a WHM/cPanel VPS (LiquidWeb)


Tutorial for getting Django running on WHM & cPanel

I ordered a new VPS from LiquidWeb for the purpose of using it to host our company's new product that I'm currently writing in Django. I have another VPS with LiquidWeb and I highly recommend them!

I got Django up and running on my other server with some help of LiquidWeb support. This time I'm going to attempt to do it on my own and document the process as I'm sure this won't be the last time I need to set this up. Hopefully, it can help someone else out there looking to do the same.

Python 2.4.3 is installed by default. My original plan was to use Python 2.5.2, but I had problems compiling database bindings with 2.5, so I finally gave up and decided to stick with 2.4.

Install PostgreSQL & Python Database Bindings

I'm quite familiar with MySQL, but knowing that Django creators have a fondness for PostgreSQL, I thought I might attempt to use it for this project. (My development so far has been using SQLIte, but I don't think that it will be adequate for this project). I didn't have PostgreSQL installed, but installing it was pretty straight forward. cPanal has an installer script (thanks LiquidWeb support for the tip):

1/scripts/installpostgres

After installation, I set the password in WHM and now the database options appeared in cPanel:

cPanel Databases

Just follow the wizard to create your database.

Next, I installed psycopg to bind python to PostgreSQL.

1cd /usr/local/src
2wget http://initd.org/pub/software/psycopg/psycopg2-2.0.8.tar.gz
3tar xzvf psycopg2-2.0.8.tar.gz
4cd psycopg2-2.0.8
5python setup.py build
6python setup.py install

Build and Install WSGI

Basically, here are the commands I ran to download and complie wsgi:

1cd /usr/local/src/
2wget http://modwsgi.googlecode.com/files/mod_wsgi-2.3.tar.gz
3gzip -dc mod_wsgi-2.3.tar.gz | tar xf -
4cd mod_wsgi-2.3
5./configure
6make && make install

After compiling the module, I was given the path to where it was located (/usr/lib/httpd/modules/mod_wsgi.so).

I logged in to WHM -> Server Configuration -> Apache Setup -> Include Editor to add the following line to load the module:

Apache Setup

LoadModule wsgi_module /usr/lib/httpd/modules/mod_wsgi.so

AddHandler wsgi-script .wsgi

Add Handler

If it works for you... Lucky you. Unfortunately I got this error:

1Configuration problem detected on line 220 of file
2/usr/local/apache/conf/httpd.conf: : Syntax error on line 1 of
3/usr/local/apache/conf/includes/pre_virtualhost_2.conf: API module structure
4'wsgi_module' in file /usr/lib/httpd/modules/mod_wsgi.so is garbled - expected
5signature 41503232 but saw 41503230 - perhaps this is not an Apache module DSO,
6or was compiled for a different Apache version? ---
7/usr/local/apache/conf/httpd.conf --- 214 215 216 217# SUEXEC is supported 218
8219Include "/usr/local/apache/conf/includes/pre_virtualhost_global.conf" 220
9===> Include "/usr/local/apache/conf/includes/pre_virtualhost_2.conf" <=== 221
10222# DO NOT EDIT. AUTOMATICALLY GENERATED. IF YOU NEED TO MAKE A CHANGE PLEASE
11USE THE INCLUDE FILES. 223NameVirtualHost \* 224 225# Default vhost for unbound
12IPs 226 --- /usr/local/apache/conf/httpd.conf ---

My first thought was that I had two versions of Apache installed, but I noticed my ./configure command said it found Apache 2.2.9. I asked LiquidWeb Support and they said that it was likely because I needed FastCGI installed.

So in WHM again, I went to Software -> Apache Update and reinstalled Apache and added fastcgi support.

That didn't help either.

So I posted to the modwsgi google group and got help from the modwsgi author himself, Graham Dumpleton and was able to install the correct version by specifying the apxs file located in /usr/local/apache/bin/. So back in /usr/local/src/mod_wsgi-2.3, I ran the following commands to rebuild modwsgi:

1make distclean
2./configure --with-apxs=/usr/local/apache/bin/apxs
3make && make install

The output was more concise this time. Then I when back into WHM -> Server Configuration -> Apache Setup -> Include Editor and loaded the module and restarted apache and it all worked this time! Thank you Graham!

I followed the Quick Configuration Guide to test WSGI to make sure it was working and learn a bit more about how it works. I recommend that you do too. Everything worked by the way. Well come back to modwsgi setup in a moment, but first we need to install Django.

Install Django

Download and installation of Django 1.0 was quite simple really - the easiest part of the gig.

1cd /usr/local/src
2wget http://www.djangoproject.com/download/1.0/tarball/
3tar xzvf Django-1.0.tar.gz
4cd Django-1.0
5python setup.py install

Run Baby, Run

Now, we attempt to put it all together!

This set up will need to be run per domain/account. First, lets create our wsgi app that will call the Django application.

I already have my account set up on WHM where I will run my Django app. It is fcabi.net and the username is fcabi, so where every you see this, be mindful to replace it with your specifics.

Create a file in /home/fcabi/public_html with the following python code (I named it django.wsgi):

1import os, sys
2sys.path.append('/home/fcabi/public_html')
3sys.path.append('/home/fcabi/public_html/fcabinet')
4os.environ['DJANGO_SETTINGS_MODULE'] = 'fcabinet.settings'
5
6import django.core.handlers.wsgi
7
8application = django.core.handlers.wsgi.WSGIHandler()

Next I create a file to add my virtual host directives in.

1mkdir -p /usr/local/apache/conf/userdata/std/2/fcabi/fcabi.net/
2vi /usr/local/apache/conf/userdata/std/2/fcabi/fcabi.net/wsgi.conf

I enter the following:

1<IfModule mod_alias.c>
2 Alias /robots.txt /home/fcabi/public_html/fcabinet/media/robots.txt
3 Alias /favicon.ico /home/fcabi/public_html/fcabinet/media/favicon.ico
4 Alias /media /home/fcabi/public_html/fcabinet/media
5 Alias /adminmedia /home/fcabi/public_html/fcabinet/media/adminmedia
6</IfModule>
7
8<IfModule mod_wsgi.c>
9 WSGIScriptAlias / /home/fcabi/public_html/django.wsgi
10 WSGIDaemonProcess fcabi.net threads=15 display-name=%{GROUP}
11 WSGIProcessGroup fcabi.net
12 WSGIApplicationGroup %{GLOBAL}
13</IfModule>

Next I run the cPanel script to add the include file to the main httpd.conf file and make sure the changes stick, then I restart apache.

1/usr/local/cpanel/bin/build_apache_conf

After running this command, I should see the Include to the file I just created:

1<VirtualHost 67.227.189.54:80>
2 ServerName fcabi.net
3 ServerAlias www.fcabi.net
4 DocumentRoot /home/fcabi/public_html
5 ServerAdmin webmaster@fcabi.net
6 UseCanonicalName Off
7 CustomLog /usr/local/apache/domlogs/fcabi.net combined
8 CustomLog /usr/local/apache/domlogs/fcabi.net-bytes_log "%{%s}t %I .\n%{%s}t %O ."
9 ## User fcabi # Needed for Cpanel::ApacheConf
10 <IfModule !mod_disable_suexec.c>
11 SuexecUserGroup fcabi fcabi
12 </IfModule>
13
14 Include "/usr/local/apache/conf/userdata/std/2/fcabi/fcabi.net/*.conf"
15
16</VirtualHost>

It's there, so I restart Apache.

1/usr/sbin/apachectl restart

Notice that I added an alias to adminmedia. To get this to work, I also had to add a symbolic link to to the contrib/admin/media files:

1ln -s /usr/local/lib/python2.4/site-packages/django/contrib/admin/media/ /home/fcabi/public_html/fcabinet/media/adminmedia

Bonus: Wildcard Subdomains

My application makes use of wildcard sub-domains, so I also needed this to work as well. This was fairly easy to implement.

Log in to cPanel and click on Subdomains:

cPanel Subdomains

For the subdomain, enter "*" and click create (Document Root may autopopulate with "/public_html", this is normal).

Now, set up virtual host directives for this subdomain like you did with your original domain.

1mkdir -p /usr/local/apache/conf/userdata/std/2/fcabi/wildcard_safe.fcabi.net/
2vi /usr/local/apache/conf/userdata/std/2/fcabi/wildcard_safe.fcabi.net/wsgi.conf

Enter the following:

1<IfModule mod_alias.c> Alias /robots.txt /home/fcabi/public_html/fcabinet/media/robots.txt Alias /favicon.ico /home/fcabi/public_html/fcabinet/media/favicon.ico Alias /media /home/fcabi/public_html/fcabinet/media </IfModule>`
2
3<IfModule mod_wsgi.c> WSGIScriptAlias / /home/fcabi/public_html/django.wsgi WSGIDaemonProcess django-sub threads=15 display-name=%{GROUP} WSGIProcessGroup django-sub WSGIApplicationGroup %{GLOBAL} </IfModule>
  • Notice the the WSGIDaemonProcess name is must be different.

Rebuild the config file and restart Apache.

1/usr/local/cpanel/bin/build_apache_conf
2/usr/sbin/apachectl restart

PS. If you're using sub-domains, you might find my sub-domain middleware useful.

Discuss on TwitterEdit post on GitHub

Share article
Dustin Davis

Dustin Davis is a software engineer, people manager, hacker, and entreprenuer. He loves to develop systems and automation. He lives with his wife and five kids in Utah.

Join the Newsletter



Dustin Davis