Django on SliceHost Virtual Private Server

I’ve recently moved my Django websites from Dreamhost due to poor FastCGI performance to the 256 plan option from SliceHost. If you sign-up with them, please use “[email protected]” as your referral. I have no idea how much they pay for referrals, but any money is good money 😉

Here’s my guide:

Sign-up to SliceHost

Painless registration gave me the root password in 2 minutes. Write down your IP address and your root password somewhere so you won’t forget it.

Setup the packages

SSH (I’ve used Putty) to your server, authenticate, and execute the commands below. I’ve decided to install PHP because I have domains using it. Also, it’s necessary for phpMyAdmin.

$ apt-get install apache2
$ apt-get install libapache2-mod-python2.4
$ apt-get install mysql-server
$ apt-get install python2.4-mysqldb
$ apt-get install php5
$ apt-get install php5-mysql
$ apache2ctl restart
$ /etc/init.d/apache2 reload

Setup Mysql

$ mysql -u root
mysql> UPDATE mysql.user SET Password=PASSWORD ('your_root_mysql_password') WHERE User = 'root';
mysql> quit

Optional: Install phpMyAdmin

It’s just easier. I’ve decided not to install a FTP server to upload my files. Instead, I’ve used WinSFTP, a sftp client for Microsoft Windows. Download and install it. Open, paste your Slice IP address and browse to the /var/www/ directory, upload phpMyAdmin and follow its install instructions.

Setup Django

$ cd /usr/lib/python2.4/site-packages/
$ svn co django

Setup your Django project

Open WinSFTP again, browse to the /usr/lib/python2.4/site-packages/ directory and upload your Django project.

Edit its file.

vi /usr/lib/python2.4/site-packages/your_django_project/

I’ll show only what you have to change, besides whatever is needed for your project to work (like INSTALLED_APPS):

DATABASE_NAME = 'your_db_name'
DATABASE_USER = 'your_db_user'
DATABASE_PASSWORD = 'your_db_password'

I use a directory for media files called “web” on most of my projects:

MEDIA_ROOT = '/var/www/'
MEDIA_URL = '/web/'

You’ll also have to change the TEMPLATE_DIRS tuple. Just put whatever you use. Here’s mine for reference:

# Put strings here, like “/home/html/django_templates”.
# Always use forward slashes, even on Windows.

Setup the domain

$ mkdir /var/www/
$ mkdir /var/log/apache2/
$ vi /etc/apache2/sites-available/

Paste this text inside the file:

<VirtualHost *>
# The three lines below remove the www from the domain name. I don’t like wwws.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.your_domain\.com [NC]
RewriteRule (.*)$1 [R=301,L]

DocumentRoot /var/www/

CustomLog /var/log/apache2/ combined
ErrorLog /var/log/apache2/

SetHandler python-program
PythonHandler django.core.handlers.modpython
SetEnv DJANGO_SETTINGS_MODULE your_django_project.settings
PythonDebug Off
PythonPath “[‘/usr/lib/python2.4/site-packages/django’] + sys.path”

# My own media directory (as mentioned in the previous section)
<Location “/web/”>
SetHandler None
# Necessary for Django’s admin media files
<Location “/media/”>
SetHandler None

Symlink your new domain configuration file to the correct directory:

ln -s /etc/apache2/sites-available/ /etc/apache2/sites-enabled/

Symlink Django’s admin media files to your domain:

ln -s /usr/lib/python2.4/site-packages/django/django/contrib/admin/media/ /var/www/

Edit Apache’s configuration file to tell it your IP address:

vi /etc/apache2/apache2.conf

Paste this before the # Include the virtual host configurations: line (it’s one before the last):

ServerName your.slice.ip.address

Edit Apache’s log rotation to include your new domain:

vi /etc/logrotate.d/apache2

Paste this at the end:

/var/log/apache2/*.log {
        rotate 52
        create 640 root adm
                if [ -f /var/run/ ]; then
                        /etc/init.d/apache2 restart > /dev/null

DNS Server

I’ve tried to install a DNS Server but not only I thought it’s hard, having a single point of failure is bad. So I’ve signed up with DNS Made Easy and I let them manage this for me. It’s super simple, after joining, add your domain to DNS Made Easy and write down the DNS server addresses. Wait until the domain name is “created” (to me, it varied from 30 minutes to 2 hours), and change the dns servers from your domain name registrar.

Last action

Restart Apache one more time:

$ apache2ctl restart
$ /etc/init.d/apache2 reload

And we’re done! With one caveat: We’ve checked out Django “trunk” directory into your Python’s directory. That means the real django lives a directory below:


Which means that if you ever want to use Django outside Apache’s mod_python, you’ll have to add the /usr/lib/python2.4/site-packages/django directory to your sys.path. Or, you could checkout Django to somewhere else, and move the “real” django directory to site-packages/ (and change the /etc/apache2/sites-available/ accordingly.

If you’re having problems, write something at the comments here and I will try to help you.

  • Awesome post! Let us know if you need anything and enjoy your Slice. I’ll be linking to this on the Slicehost Wiki and Forum. Thanks!

  • Kenzie

    Wondering why you put your domain log files in their own directory? I just put them in the main /var/log/apache directory as domain.tld-access.log and then logrotate handles them without modification.

    Is there any reason to put them in a separate directory other than personal preference?

    Great write-up, thanks.

  • inerte

    Hi Kenzie,

    No particular reason. I guess is personal preference.

  • Tom

    Hi – what linux distribution are you using on SliceHost?

  • inerte

    I am using Ubuntu.

  • Shouldn’t ‘ln -s /etc/apache/sites-available/ /etc/apache/sites-enabled/’ be ‘ln -s /etc/apache2/sites-available/ /etc/apache2/sites-enabled/’?

  • inerte

    Yes Jim, thanks for the correction. Article updated.

  • Ahoy!
    You’re the first referral code for SliceHost that I found when Googling for one so I’m going to add yours as I sign up :-)

  • Hiya,

    Thanks for the write-up (I also used you as my referral) — couple questions / notes:

    Are you running the prefork version of Apache2?
    To get subversion on my slice (to run the Django check-out), I also ran apt-get install subversion
    I couldn’t get django’s command to work from the command line, so I installed django0.95 from the tarball, which seemed to work better.


  • Pingback: Inércia Sensorial » Lighttpd with Apache()

  • Or on Ubuntu simply use


    a2ensite is a handy script that does the softlinking for you

  • Pingback: Inércia Sensorial » Apache + mod_wsgi + Django + lighttpd()