Setting up Django on a WHM/cPanel VPS (LiquidWeb)
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):
Â
# /scripts/installpostgres
Â
Â
After installation, I set the password in WHM and now the database options appeared in cPanel:
Just follow the wizard to create your database.
Next, I installed psycopg to bind python to PostgreSQL.
Â
# cd /usr/local/src
# wget http://initd.org/pub/software/psycopg/psycopg2-2.0.8.tar.gz
# tar xzvf psycopg2-2.0.8.tar.gz
# cd psycopg2-2.0.8
# python setup.py build
# python setup.py install
Â
Â
Build and Install WSGI
Basically, here are the commands I ran to download and complie wsgi:
cd /usr/local/src/
wget http://modwsgi.googlecode.com/files/mod_wsgi-2.3.tar.gz
gzip -dc mod_wsgi-2.3.tar.gz | tar xf -
cd mod_wsgi-2.3
./configure
make && 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:
![]()
LoadModule wsgi_module /usr/lib/httpd/modules/mod_wsgi.soAddHandler wsgi-script .wsgi
If it works for you… Lucky you. Unfortunately I got this error:
Configuration problem detected on line 220 of file /usr/local/apache/conf/httpd.conf: : Syntax error on line 1 of /usr/local/apache/conf/includes/pre_virtualhost_2.conf: API module structure ‘wsgi_module’ in file /usr/lib/httpd/modules/mod_wsgi.so is garbled – expected signature 41503232 but saw 41503230 – perhaps this is not an Apache module DSO, or was compiled for a different Apache version? — /usr/local/apache/conf/httpd.conf — 214 215 216 217# SUEXEC is supported 218 219Include “/usr/local/apache/conf/includes/pre_virtualhost_global.conf” 220 ===> Include “/usr/local/apache/conf/includes/pre_virtualhost_2.conf” <=== 221 222# DO NOT EDIT. AUTOMATICALLY GENERATED. IF YOU NEED TO MAKE A CHANGE PLEASE USE THE INCLUDE FILES. 223NameVirtualHost * 224 225# Default vhost for unbound IPs 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:
# make distclean
# ./configure --with-apxs=/usr/local/apache/bin/apxs
# make && 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.
cd /usr/local/src
wget http://www.djangoproject.com/download/1.0/tarball/
tar xzvf Django-1.0.tar.gz
cd Django-1.0
python 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:
Â
import os, sys
sys.path.append('/home/fcabi/public_html')
sys.path.append('/home/fcabi/public_html/fcabinet')
os.environ['DJANGO_SETTINGS_MODULE'] = 'fcabinet.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
Â
Â
Next I create a file to add my virtual host directives in.
Â
# mkdir -p /usr/local/apache/conf/userdata/std/2/fcabi/fcabi.net/
# vi /usr/local/apache/conf/userdata/std/2/fcabi/fcabi.net/wsgi.conf
Â
Â
I enter the following:
Â
<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
Alias /adminmedia /home/fcabi/public_html/fcabinet/media/adminmedia
</IfModule>
<IfModule mod_wsgi.c>
WSGIScriptAlias / /home/fcabi/public_html/django.wsgi
WSGIDaemonProcess django threads=15 display-name=%{GROUP}
WSGIProcessGroup django
WSGIApplicationGroup %{GLOBAL}
</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.
Â
# /usr/local/cpanel/bin/build_apache_conf
Â
Â
After running this command, I should see the Include to the file I just created:
Â
<VirtualHost 67.227.189.54:80>
   ServerName fcabi.net
   ServerAlias www.fcabi.net
   DocumentRoot /home/fcabi/public_html
   ServerAdmin webmaster@fcabi.net
   UseCanonicalName Off
   CustomLog /usr/local/apache/domlogs/fcabi.net combined
   CustomLog /usr/local/apache/domlogs/fcabi.net-bytes_log "%{%s}t %I .\n%{%s}t %O ."
   ## User fcabi # Needed for Cpanel::ApacheConf
   <IfModule !mod_disable_suexec.c>
       SuexecUserGroup fcabi fcabi
   </IfModule>
Include “/usr/local/apache/conf/userdata/std/2/fcabi/fcabi.net/*.conf”
</VirtualHost>
Â
Â
It’s there, so I restart apache.
Â
# /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:
# ln -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:
![]()
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.
Â
# mkdir -p /usr/local/apache/conf/userdata/std/2/fcabi/wildcard_safe.fcabi.net/
# vi /usr/local/apache/conf/userdata/std/2/fcabi/wildcard_safe.fcabi.net/wsgi.conf
Â
Â
Enter the following:
Â
<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>
<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.
# /usr/local/cpanel/bin/build_apache_conf
# /usr/sbin/apachectl restart
PS. If you’re using sub-domains, you might find my sub-domain middleware useful.
Related posts:
- Django cron on Webfaction James Bennett addresses one of the most frequently asked questions...
- Basic Authentication on mod_wsgi I’m currently in the process of creating an iPhone app...
- Webfaction Review I was looking at my web stats and noticed I...
- One Click Django Project Publishing I was reading The Joel Test and I got to...
- Django & Djson… er, JSON I must admin this is my first attempt at even...
Related posts brought to you by Yet Another Related Posts Plugin.
September 25, 2008
Tags: cpanel, django, python, whm Posted in: Programming & Internet




2 Responses
GOOD! today i’ll try to install it!
Thanks
mod_wsgi with Apache 2 on CentOS 4 « Locomotion - May 13, 2009
[...] It could be a confusion between multiple Apache versions on your system when you compiled mod_wsgi. Check out the fix at Dustin Davis’ (Django) tutorial. [...]
Comments are closed for this entry.