Getting Django up and running with Hostgator

I thought it would be fun this year to start learning Django, so here’s a bit about how to get it set up using Hostgator (I have a shared hosting account).
I am using Terminal on the MAC and the File Manager on the cPanel.
The first thing you have to do is enable SSH (shell access). To do this, log into your cPanel and go to “Billing”:

Then choose, “View Hosting Packages”:

Then, click the link that says, “Enable Shell Access“.
Next, write down your IP as shown in the cPanel (I blurred mine out for security purposes, but you can see what the section looks like here):

Also, write down your cPanel username and password, you will need this.
Open Terminal and the File Manager in cPanel. I keep both open at the same time to see what I am doing.
In your File Manager, you should see /home/your-username/. Then look for a folder called django. You should have this. If not, in Terminal, type the following to SSH to your account:
ssh -p 2222 your-username-goes-here@your-ip-goes-here
If this works (you might have to wait a moment while it connects to your site), you will be prompted for your password.
Type in your password (you won’t see it at all) then hit enter and wait.
If this is your first time, you will be asked about a key. Just say “yes”.
You might then get some message like, “The authenticity of this host can’t be established” and “RSA key fingerprint is blah blah blah”.
Don’t worry about this, if you like, write down the key in case you need it later. That’s it. You are now connected to your logged in to your account via SSH. Now type the following.
mkdir ~/django
cd ~/django
svn co http://code.djangoproject.com/svn/django/trunk/django
mkdir creates a folder called “django”.
cd changes where you are to inside of the “django” folder.
The last line gets the latest stable version of django and installs it into the “django” folder. You need to have subversion installed for this to work.
I can’t really remember if I had to install subversion on my version of OS X, I think it was already installed, but if not, you’ll have to install it. It’s free.
Then, create a directory for your projects inside of the django folder by typing:
mkdir ~/django/projects
You should now have a folder called “django” with two folders inside:
Now, to test if django is running, you make a test project.
In Terminal, type:
cd projects
This will switch you into the projects folder.
Then type:
django-admin.py startproject newproject
This tells django to create a new project called, “newproject”.
Then type:
cd newproject
chmod +x manage.py
./manage.py startapp newapp

If you go inside of your “newproject” folder you should now see the following:
 
Next, you have to enable fastcgi (or, I suppose, django will run really slowly).
Create a new file called index.fcgi in your public folder:

Then change the permissions of the file to 755:

Next, edit the file and add the following (be sure to change “username” with your username for your account):
#!/usr/local/bin/python
import sys, os, user

# sys.path.insert(0, "/usr/lib/python2.4")
sys.path.insert(0, "/home/your-username-goes-here/django")
sys.path.insert(0, "/home/your-username-goes-here/django/projects")
sys.path.insert(0, "/home/your-username-goes-here/django/projects/newproject")

# Switch to the directory of your project.
os.chdir("/home/your-username-goes-here/django/projects/newproject")

# Set the DJANGO_SETTINGS_MODULE environment variable.
os.environ['DJANGO_SETTINGS_MODULE'] = "newproject.settings"

from django.core.servers.fastcgi import runfastcgi
runfastcgi(method="threaded", daemonize="false")
Next, open the .htaccess that is in the same folder as index.fcgi.
AddHandler fcgid-script .fcgi
Options +FollowSymLinks
RewriteEngine On

RewriteRule (media/.*)$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(admin/.*)$ index.fcgi/$1 [L]
Next (and this is important) check your website. If you are getting a 500 error, open Terminal and log in to your account:
ssh -p 2222 your-username-goes-here@your-ip-goes-here
Then type:
which python
This should give you a message like:
/usr/bin/python
If you are getting something different (for example, user/local/bin/python/), update your script to reflect this:
#!/usr/local/bin/python 
If all goes well, you should see this:

No comments:

Post a Comment