Although I’ve chosen to run my Django application server off Gypsy Hosting, I do have a Dreamhost account, and I have been able to run Django on it with no problems to speak of. The instructions here are based on those at the Dreamhost wiki, but I have modified a few things to suit my preferences. Also, this tutorial is more verbose, explaining things a little further.
Some slight words of caution: some people have reported problems with Django on Dreamhost. It is listed an as “Django-friendly web host” on the Django website, though, and I’ve personally not had any trouble. Also, the Dreamhost/Django setup involves running Django under FastCGI. While this is an officially supported configuration for Django, it’s not the recommended one — Django’s website states that mod_python under Apache is the preferred way. All that having been said, Dreamhost should be a perfectly adequate place for you to install and try out Django.
Prepare your Dreamhost account for Django
We’ll need to configure your Dreamhost account for use with Django. You can do this right alongside your existing website if you have one. Django will exist separately of it, so there should be no conflicts.
Add a subdomain
First, you’ll want to create a new subdomain for your Django application server. This way, you can play with Django all you like without affecting your production site. To create the subdomain:
- Open the Dreamhost administration panel.
- Choose “Manage Domains” under the Domains menu.
- Select “Add New Domain / Sub-Domain”
- Choose a name for your subdomain. I’ve chosen
django.jeffcroft.comand will be using that name in this tutorial.
- Select both “Extra web security” and “Fast CGI Support”
- Click the ‘Fully host this domain now!” button.
Create a MySQL database
We’ll also need to setup a MySQL database to use with Django (sadly, Dreamhost doesn’t support PostgreSQL). Follow these steps:
- In the Dreamhost administration panel, choose “manage MySQL” under the Goodies menu.
- Enter a database name for your new database. I’ve chosen
django_dband will be using that name in the tutorial.
- Enter a hostname for your database server. I’ve chosen
- Enter a username and password for your database.
- Click the “Add new database now!” button.
Create a few directories
We’ll need to create some directores for your Django install. You can do this however you like (shell, FTP, etc.), but it’s probably simplest to do it in the shell, as we’ll need the shell in a moment anyway. For these instructions, we’ll use the shell to create directories.
- SSH into your Dreamhost server (for me,it’s rossmore.dreamhost.com, but yours is likely different).
- Make a new directory for Django-related bits:
- Change into your new directory:
- Create directories for templates and projects:
Download and set up Django
We’ll be using the latest Django by checking it out from its Subversion repository.
- In your ~/django directory (which you should already be in), check out the source:
svn co http://code.djangoproject.com/svn/django/trunk/ django_src
- Edit your ~/.bash_profile file, adding the following lines. This sets your Python path to include your Django directories, and puts Django’s utilities in your system path. You can edit the file however you like.
- Activate your profile changes by “sourcing” the file:
Start a Django project
Django differentiates between “projects” and “applications.” A project can contain multiple apps. We’ll start by creating a project named “myproject.” If you choose a different name, you’ll need to modify the instructions accordingly.
- Move into your Django projects directory:
- Start a new project, using Django’s command line utility. This creates a basic directory structure for the project and adds the necessary configuration files:
django-admin.py startproject myproject
- Ensure that only you can read and write to the settings file for your project. This is very important, as your database password will be in this file:
chmod 600 myproject/settings.py
- Edit the myproject/settings.py file with our configuration parameters. The parts of the file you’ll want to change are as follows:
Enter your name and e-mail address in the ADMINS setting:
Enter your database settings, using the database name, username, password, and host your chose in the Dreamhost administration panel.
mysql as your
DATABASE_ENGINE value throws an error, you will need to change it to
mysql_old. This should be a temporary problem, fixed when Dreamhost upgrades MySQLdb, the Python module that providees connectivity to MySQL.
Edit your timezone:
Tell Django where your template directory is (we created it a while ago):
Set the media root and media url to the media directory we created earlier:
Now, let’s edit your ~/.bash_profile file again, and add this line to the end of it:
- Then, active those changes by “sourcing the file”:
- Move into your project directory:
- Syncronize your Django database with the changes you’ve made in the settings file. This process will also prompt you to create a “superuser” for your project:
Install and configure FastCGI
- Move into the directory for the subdomain you created way back at the begining:
- Download the FastCGI Python script:
- Set the proper permissions on the script:
chmod 755 fcgi.py
- Create a FastCGI Python script for your Django project. Make a new file called
dispatch.fcgiand paste in these lines. Be sure to change the “jcroft” in the path to your username and the “myproject” to the name of your project, if you chose different name.
- Set the proper permissions on dispatch.fcgi:
chmod 755 dispatch.fcgi
Set up mod_rewrite
Apache’s mod_rewrite module will pass all requests to your website through dispatch.fcgi, and thus, into Django’s URL dispatcher.
- Create a file called
.htaccess(nte the leading dot) in your subdomain directory (for me, django.jeffcroft.com). Add the following lines to the file:
- Reset Python to activate all of our changes:
All URLs on our subdomain should now be routed to Django. Feel free to open up your browser and try it out for yourself.
Install Django’s built-in administrative interface
Django’s awesome admin tool is a Django program itself. As such, we’ll need to install it into this project, just like we would any Django app. Note that it’s entirely optional. However, it really is one of the nicest things about Django, so there’s a good chance you’ll want it.
- Link Django’s admin media files into our subdomain directory 9be sure to use your subdomain, instead of django.jeffcroft.com):
ln -s $HOME/django/django_src/django/contrib/admin/media $HOME/django.jeffcroft.com/admin_media
- Set the admin media prefix in django/django_projects/myproject/settings.py:
Add the admin application to your installed applications in django/django_projects/myproject/settings.py:
- Install the project with Django’s manage.py tool. You’ll need to be in your django/django_projects/myproject directory:
python manage.py syncdb
- Uncomment the admin line in the URL configuration file (django/django_projects/myproject/urls.py):
- Reset Python again:
At this point, you should be able to go to http://django.yourdomain.com/admin and log into the Django administrative interface with the superuser username and password you created.
Now, you can get started making Django apps. From here, I highly recommend you head over to the tutorial on the official Django website. It’ll take you through build your first Django application. Note that we’ve already set up your first project, so you can skip the first section and head straight to “Creating Models.”
Update: Several months later, it was discovered that Dreamhost prefers the FastCGI script in this setup be called
dispatch.fcgi. Previously, this article suggested
django.fcgi. The article has been updated to reference
dispatch.fcgi, but some comments blow refer to
django.fcgi. Both filenames reference the same file.