The setup and administration of a shared-hosting server is of course quite a topic. I don’t want to go into too much detail about that, because it’s beyond the scope of this tutorial. I assume you have a running Virtualmin (Pro) server and the need to enable each user to run Django in the shared environment of your root server.
Fortunately Virtualmin Pro comes with “install scripts” that can be used to automatically install a variety of web applications to each virtual host. Naturally this works best with PHP-based apps, such as phpMyAdmin, phpBB and Wordpress. Interestingly though, it’s also able to setup the Ruby-based Rails (via rubygems) and the Perl-based MovableType. Python-based apps are missing at the moment but the developer is considering full support, including a PyPI GUI interface.
But there is good news for the impatient: An API to extend the install script functionality with Perl files. I tinkered a little to get a script ready for Django: django.pl.
Django install script
Just drop this file in your /etc/webmin/virtualmin/scripts directory and you are able to install either one of the stable versions (0.90, 0.91.1, 0.95.2, 0.96.1) or the last subversion trunk to /home/USERNAME/lib/django. The stable versions can be automatically updated once a new version is released at djangoproject.com (also bulk update on all virtual servers), while the subversion version still needs to be updated manually by running svn update /home/USERNAME/lib/django.
Hook up with mod_*
Now on finding a suitable interface to hook up your Django-based web app with the webserver, the options are: cgi, mod_fastcgi, mod_fcgid and mod_wsgi, to name a few. Luckily Virtualmin (Pro) uses mod_fcgid to run PHP, although the following instructions should also be valid for mod_wsgi or mod_fastcgi with little modification.
Once you have installed Django and uploaded your Django-based project to your virtual host (e.g. to the directory ~/lib/mysite) all you need to get up and running is:
A ~/public_html/.htaccess file containing:
AddHandler fcgid-script .fcgi
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /mysite.fcgi/$1 [QSA,L]
and a ~/public_html/mysite.fcgi file containing:
#!/usr/bin/env python
import sys, os
sys.path.insert(0, os.path.expanduser("~/lib"))
os.environ['DJANGO_SETTINGS_MODULE'] = "mysite.settings"
from django.core.servers.fastcgi import runfastcgi
runfastcgi(method="threaded", daemonize="false")
I strongly encourage you to upload your “mysite” app to the directory lib/mysite in your home directory — putting your settings.py in your public_html directory for example is just plain dangerous.
The .htaccess file instructs the webserver to send all requests to the fastcgi process (running via mysite.fcgi) if there is no filename in the public_html directory matching the request path. Since this also applies to symbolic links you can use them to link to site_media or admin_media directories like this:
ln -s /home/USERNAME/lib/django/contrib/admin/media/ /home/USERNAME/public_html/mediaor
ln -s /home/USERNAME/lib/mysite/media/ /home/USERNAME/public_html/site_media
Once the webserver receives the first request, the Django Fastcgi process gets automatically started (spawned) and waits for new requests. Please have a look in the fastcgi.py for more start options of the fastcgi process.
comments
Trying to get to work with Virtualmin Pro.
I have created a mysite project under the lib directory and added the .htaccess file entries as well. When I go to any url after implementing the rewrite rule I get a server exception error. THis goes away after disabling rewrite.
Any suggestions are greatly appreciated.
Hi Keith,
Is mod_rewrite enabled? On Debian (which I run) you can check by typing in a root shell:
<code>ls /etc/apache2/mods-enabled/rewrite.load</code>
If you get a warning "No such file or directory" (or similar) then you need to enable it by running:
<code>a2enmod rewrite && /etc/init.d/apache2 force-reload</code>
Then renable the rewrite part in the .htaccess file and it should work. Let me know if it doesn't work.
[...] Python based web applications because it includes an official install script for Django. This makes my version of a Django install script slightly unnecessary, even though the official version only supports stable releases (0.96.1) and [...]
One other place that would be nice to have django support is Cpanel. We are moving to a cpanel install in our shop and I would like to help push Cpanel along to get Django support.