Let me first start by saying that the site you are reading right now is NOT hosted at Dreamhost, nor is this blog written with Django. Both of these factors will change once I get around to migrating all of my content, but right now, that simply isn’t going to happen. There is also a really good tutorial that I grabbed most of this information from. Go hear to read the tutorial which details how to configure Django with Dreamhost on a subdomain. This puppy here is for the full blown website, not a subdomain at all.
Lets first start by stating what exactly it is that we are going to be doing here. First I purchased a domain name “www.worthless-stuff.com”. Yes I know that sounds a bit childish, and you would be right, it really is kind of absurd that I would create a site built upon things that are worthless.
Anyways, it came to my attention by the amount of email that I was getting and IM messages that the web is full of products that are just worthless pieces of junk. So now I’m going to dedicate part of my day finding those kinds of products and making some fun of them. This should be lots of fun. So without further ado, lets get to how setting up Django goes on Dreamhost.
I’m going to assume that you already have a Dreamhost account and that you know how to go in and manage your account via the “Dreamhost Web Panel”. So the first this we are going to do is get our account all setup for Django, that is prepare our web space for Django.
Prepare your site for FastCGI
Since we aren’t going to be able to install mod_python, like we can in our development environment, we are going to have to set up FastCGI. This is as easy as logging into your “Dreamhost Web Panel” and go to:
Domains >> Manage Domains >> Edit
From there you should see the FastCGI Support option. Make sure it’s checked and then select “Change fully hosted settings now!”. Good, so now your all done making sure your host is ready for the other changes we are going to make.
Create a MySql DB
Back in the Dreamhost Web Panel, select “Goodies” and then down to “Manage MySQL”. This is where you create your database to be used with Django. So go ahead and fill in the necessary fields to create your database. Since the site we are putting together is going to be called www.worthless-stuff.com, I naturally used the following settings:
- DB Name: worthless
- Host: worthless.worthless-stuff.com
- Pass: HAHA, wouldn’t you like to know
Start the Setup of Django
So from this point on you might as well get pretty use to using your shell. Since I’m on a Mac I will fire up my terminal window and ssh into my account. Usually with Dreamhost it’s easy to use your ftp user account to do this setup. So for me I entered the following to connect to my server.
ssh worthless@worthless-stuff.com
First we are going to make a place for Django to live, so lets do that. I like to place my Django files in vary specific places, it just helps with the organization between the different pieces. At the top level I create a directory called “django”. Since Django is based on MVC, I like to break my templates and project locations out. So I’ll do the following to create my structure.
- mkdir django
- cd django
- mkdir django_templates
- mkdir django_projects
At this point I create another directory that you can store your media related files in. So go ahead and do the follwing:
- mkdir /media
Now that we have all of our locations set up, it’s time to download Django. Now I’m one of those type of people that likes the latest and greatest, and because I spent so many years developing Web Content Management Systems, I really like it when I can check out the latest version from a revision control system. So lets go head and issue the following command, to check out the latest version using subversion. Remember yours is going to be a tad different, depending on your hostname.
Svn co http://code.djangoproject.com/svn/django/trunk/ django_src
This could take a couple of minutes, which after your done, your going to need to change your shell profile to let your environment know how to find the django source and the tools that come along with it. Open up your ~/.bach_profile file and add the following lines to it:
export PATH=$PATH:$HOME/django/django_src/django/bin
export PTYTHONPATH=$PYTHONPATH:$HOME/django/django_src:
$HOME/django/django_projects
Once you’ve made that change, you want to make sure the system picks up your new environment settings. Enter the following command.
source ~/.bash_profile
Creating a Project
Well now you have Django right were you want it, and now it’s time for the real fun to begin. Django does a great job of separating projects from applications, which there can be many applications associated with a particular project. Since just about every tutorial and every books I have read on Django creates the “myproject” project, we are going to do the very same thing. Type the following at the prompt.
cd ~/django/django_projects
django-admin.py startproject myproject
This should have created a myproject folder in your django/django_projects directory. The next thing we want to do is set the permissions on your settings file since this is where we are going to be putting some highly sensitive information.
chmod 600 settings.py
Change your settings.py file. Now we need to start adding some of your configuration settings to the settings.py file. So open up your settings.py file and edit the following sections:
ADMINS = (
(’Worthless Stuff’, ’stuff@worthless-stuff.com’),
)
Now remember that database stuff you created before, this is where that information is going to come in handy. Edit the following section like so, keeping in mind that your settings will be slightly different.
DATABASE_ENGINE = ‘mysql’ # ‘postgresql_psycopg2′, ‘postgresql’, ‘mysql’, ’sqlite3′ or ‘oracle’.
DATABASE_NAME = ‘worthless’ # Or path to database file if using sqlite3.
DATABASE_USER = ‘worthless’ # Not used with sqlite3.
DATABASE_PASSWORD = ’supersecret’ # Not used with sqlite3.
DATABASE_HOST = ‘worhtless.worthless-stuff.com’ # Set to empty string for localhost. Not used with sqlite3.
DATABASE_PORT = ” # Set to empty string for default. Not used with sqlite3.
Now edit the Time Zone. Mine is set for the Los Angeles (Pacific Time)
# Local time zone for this installation. All choices can be found here:
# http://www.postgresql.org/docs/current/static/datetime-keywords.html#DATETIME-TIMEZONE-SET-TABLE
TIME_ZONE = ‘America/Chicago’
Configure the Media Root
# Absolute path to the directory that holds media.
# Example: “/home/media/media.lawrence.com/”
MEDIA_ROOT = ‘/home/worthless/worthless-stuff/media’
Now Configure the Media Url
# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash if there is a path component (optional in other cases).
# Examples: “http://media.lawrence.com”, “http://example.com/media/”
MEDIA_URL = ‘http://www.worthless-stuff.com/media/’
Configure the template directory
TEMPLATE_DIRS = (
# Put strings here, like “/home/html/django_templates”.
# Always use forward slashes, even on Windows.
“/home/worthless/django/django_templates”
)
Thats it for right now, as far as editing the settings file. We still have a lot more work to do, but it doesn’t take very long. Trust me.
Go back into your ~/.bash_profile and add the following.
Export DJANGO_SETTINGS_MODULE=myproject.settings
Remember to source the file: source ~/.bash_profile
Now change to your project directory:
cd ~/django/django_projects/myproject
Now we need to synchronize your Django database with the changes you’ve made in the settings file. Run the following command.
django-admin.py syncdb
If your like me and you start getting a bunch of errors thrown from mysql, it’s a good chance that the server you are on doesn’t have the latest version of MySQLdb on it for python. So here’s what you do, go into your settings.py file and change the database option to say “myslq_old” instead of mysql. Then save the file and rerun the above command. There is a good chance it’s going to work.
Once the installation starts to go smoothly, you will be asked to create a super user. Just remember to jot down the information that you supply. Your going to need it again.
Install and configure FastCGI
Change back to your docroot, for me it’s the following:
cd ~/worthless-stuff
Download the latest version of FastCGI Python script:
wget http://svn.saddi.com/py-lib/trunk/fcgi.py
Change the permissions on the file:
chmod 755 fcgi.py
Now create a FastCGI Python script for the project. Make a new file called dispatch.fcgi and past in the following lines:
#!/usr/bin/env python
import sys
sys.path += ['/home/jcroft/django/django_src']
sys.path += ['/home/jcroft/django/django_projects']
from fcgi import WSGIServer
from django.core.handlers.wsgi import WSGIHandler
import os
os.environ['DJANGO_SETTINGS_MODULE'] = ‘myproject.settings’
WSGIServer(WSGIHandler()).run()
Set the permissions on this file to 755
Create mod_rewrite
So what we are doing here is asking Apache to pass all the requests that come through the web server to dispatch.fcgi, which will then turn around and hand them off to Django’s URL dispatcher. Pretty sneaky huh?
In your main web area, create a file called .htaccess(Remember the dot)
Add the following lines:
RewriteEngine On
RewriteBase /
RewriteRule ^(media/.*)$ - [L]
RewriteRule ^(admin_media/.*)$ - [L]
RewriteRule ^(dispatch\.fcgi/.*)$ - [L]
RewriteRule ^(.*)$ dispatch.fcgi/$1 [L]
Reset Python to bring the changes to life.
pkill python
Get used to running this command, as you’ll use it a lot while making changes to your code.
At this point you should be able to open a browser and try to view your site. If everything is running successfully you should see a Django style web page telling you it worked.
What’s Next?
Well we aren’t done yet, we still need to make sure that the Admin that comes with Django works, so lets set that up now.
Lets create a link, mine would look like so:
ln -s $HOME/django/django_src/django/contrib/admin/media $HOME/worthless-stuff/admin_media
Now we must update our settings.py file to reflect the changes we just made.
ADMIN_MEDIA_PREFIX = ‘/admin_media/’
It would also be a good idea at this point to add the application to our available applications inside the settings.py file (since we already had it opened). Your Installed Apps section should look something like the following:
INSTALLED_APPS = (
‘django.contrib.auth’,
‘django.contrib.contenttypes’,
‘django.contrib.sessions’,
‘django.contrib.sites’,
‘django.contrib.admin’,
)
Inside our project directory myproject, there is a file called urls.py. We need to open this and comment out the admin line.
# Uncomment this for admin:
(r’^admin/’, include(’django.contrib.admin.urls’)),
That should do it for the edits thus far.
Now all we need to do is notify Django that we are using a newly configured app, which means we need to run the following command from inside our myproject directory.
python manage.py syncdb
Run pkill python so that apache reloads, and your off and running. Try going to http://yourhost.com/admin and seeing if it works. Please leave your comments if you have any, and if you need any help let me know.



























0 responses so far ↓
There are no comments yet...Kick things off by filling out the form below.
You must log in to post a comment.