Roshan Book

My Tech Notebook

Getting django up and running on hostgator


This post was originally published at http://redconservatory.com/blog/getting-django-up-and-running-with-hostgator-part-1/

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:

This is your “newapp”. Yay, django is working…sort of

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:

Below is my working code for hostgator

Below are my working code based on Robert code.

1. For curlgen.fcgi – change permission to 755

code is :
#!/usr/bin/python
import sys, os, user

# sys.path.insert(0, “/usr/lib/python2.7”)
sys.path.insert(0, “/home/r5tpl/django/projects”)

# Switch to the directory of your project.
os.chdir(“/home/r5tpl/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”)

For .htaccess code is
AddHandler fcgid-script .fcgi
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ curlgen.fcgi/$1 [QSA,L]

3 responses to “Getting django up and running on hostgator

  1. Oliver Bajusz November 10, 2011 at 1:25 pm

    Hey,

    finally a proper description!

    Just signed up with HG, their support is completely useless, there is noone there who can actually help you, they send you error logs, and every single time u email them back another person replies, and they just can’t be bothered to read all previous conversations, which makes it a never-ending cycle. so if u dont KNOW how u have to set it up u r on your own…

    My problem is that my main website is plain html+php and if I edit the .htaccess and create the fcgi file under public_html, it stops working obviously as it expects a Django project….. (Django definitely works btw, and my permissions are also all correct)

    Basically my project is be under /home/uname/projectname

    I created a directory called projectname under /home/uname/public_html, and put my .htaccess there with the content:

    AddHandler fcgid-script .fcgi
    Options +FollowSymLinks
    RewriteEngine On
    RewriteRule ^(.*)$ index.fcgi/$1 [L]

    and in the same folder my index.fcgi :

    #!/usr/local/bin/python
    import sys, os, user

    # sys.path.insert(0, “/usr/lib/python2.4”)
    sys.path.insert(0, “/home/uname/projectname”)

    # Switch to the directory of your project.
    os.chdir(“/home/uname/projectname”)

    # Set the DJANGO_SETTINGS_MODULE environment variable.
    os.environ[‘DJANGO_SETTINGS_MODULE’] = “projectname.settings”

    from django.core.servers.fastcgi import runfastcgi
    runfastcgi(method=”threaded”, daemonize=”false”)

    Still getting 500 internals, but my main website is still up running, so i keep trying..
    If you have any suggestions I’m looking forward to trying them as I’m running out of ideas and patience

  2. dev November 14, 2011 at 8:43 am

    Hi, Excellent post, I like the way of your writing,
    Thanks manishroshan for the share

  3. Miguel November 20, 2011 at 10:31 pm

    Thanks for the guide; I followed the previous (hopefully identical) version, but as a complete newbie with Django, I don’t know how to continue. I don’t even know how to check if the newproject is there. I understood the .htaccess was supposed to redirect… but I just added your code to the one I’m using (like the reader before, my site already hosts php applications). On the other hand, I’ve read you saying that there should be a new folder within public_html, is that right?

    Any help appreciated.

    I’m trying to set up an instance of https://github.com/sunlightlabs/publicmarkup . But I don’t know how to go about installing a new Django app and making it visible.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: