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> FLUSH PRIVILEGES;
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 http://code.djangoproject.com/svn/django/trunk/ 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 settings.py file.

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

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

DATABASE_ENGINE = 'mysql'
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/your_domain.com/web/'
MEDIA_URL = '/web/'

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

TEMPLATE_DIRS = (
# Put strings here, like “/home/html/django_templates”.
# Always use forward slashes, even on Windows.
‘/usr/lib/python2.4/site-packages/my_django_project/templates/my_django_project/’,
)

Setup the domain

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

Paste this text inside the file:

<VirtualHost *>
ServerName www.your_domain.com
ServerAlias your_domain.com
# 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 (.*) http://your_domain.com$1 [R=301,L]

DocumentRoot /var/www/your_domain.com

CustomLog /var/log/apache2/your_domain.com/access.log combined
ErrorLog /var/log/apache2/your_domain.com/error.log

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
</Location>
# Necessary for Django’s admin media files
<Location “/media/”>
SetHandler None
</Location>
</VirtualHost>

Symlink your new domain configuration file to the correct directory:

ln -s /etc/apache2/sites-available/your_domain.com /etc/apache2/sites-enabled/your_domain.com

Symlink Django’s admin media files to your domain:

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

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/your_domain.com/*.log {
        weekly
        missingok
        rotate 52
        compress
        delaycompress
        notifempty
        create 640 root adm
        sharedscripts
        postrotate
                if [ -f /var/run/apache2.pid ]; then
                        /etc/init.d/apache2 restart > /dev/null
                fi
        endscript
}

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:

/usr/lib/python2.4/site-packages/django/django/

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/your_domain.com) accordingly.

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

  • http://www.slicehost.com matt

    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.

  • http://infiniteexpanse.net Jim B.

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

  • inerte

    Yes Jim, thanks for the correction. Article updated.

  • http://6brand.com Danger

    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 :-)

  • http://www.joethink.com Joe Murphy

    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 django-admin.py command to work from the command line, so I installed django0.95 from the tarball, which seemed to work better.

    -Joe

  • Pingback: Inércia Sensorial » Lighttpd with Apache

  • http://daybarr.com Day Barr

    Or on Ubuntu simply use

    a2ensite your_domain.com

    a2ensite is a handy script that does the softlinking for you

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