Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is django using fcgi on a hostgator server behaving this way?

I am trying to deploy a simple static site on hostgator. I followed this tutorial and originally had in my .htaccess (where abc.com is the website):

AddHandler fcgid-script .fcgi
Options +FollowSymLinks
RewriteEngine On

RewriteRule (media/.*)$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(admin/.*)$ index.fcgi/$1 [L]
RewriteCond %{HTTP_HOST} ^abc\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.abc\.com$
RewriteRule ^/?$ "http\:\/\/abc\.com\/index\.fcgi" [R=301,L]

but this did not work and would only load the "home.html" from my template directory for some reason. When I tried to follow any links I would get a hostgator 404 error page. Then after doing some research and talking to the hostgator support people a few times I changed it to the much simpler:

AddHandler fcgid-script .fcgi

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.fcgi/$1 [QSA,L]

My index.fcgi file in the public_html directory looks like:

#!/usr/local/bin/python
import sys, os, user
from django.core.servers.fastcgi import runfastcgi

sys.path.insert(0, "/usr/lib/python2.6")
sys.path.insert(0, "/home/joeshmo/django")
sys.path.insert(0, "/home/joeshmo/django/Projects")
sys.path.insert(0, "/home/joeshmo/django/Projects/PersonalWebsite")

# Switch to the directory of your project.
os.chdir("home/joeshmo/django/Projects/PersonalWebsite/")

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

runfastcgi(method="threaded", daemonize="false")

This worked, but now I am very curious what is going on here. Despite doing some research on my own I am confused what fcgi does and, more importantly, how it is telling django to load the proper html files from my template directory as the hostgator tech person I talked to assured me that html files could ONLY ever be accessed from the public_html folder. I am familiar with the general concept of a url dispatcher, regexp, views, ect but I am a self-taught web developer so please bear that in mind.

like image 380
ohhh Avatar asked Nov 01 '22 08:11

ohhh


1 Answers

It sounds like you may be used to things like PHP projects where there is a 1:1 correspondence between URLs and the path on the server. That's not the case in a Django project: you pass the request path you want to a script (usually a WSGI script but in your case a FCGI) that knows to pass the path on to the Django code. Once the path gets to the Django code it looks for a matching path pattern in the urls.py files and serves that back if it finds one. The path is question is sent in the $1 variable in your .htaccess file, which is why you only ever saw the home template when you were passing the literal string 'home.html' instead of the actual request path.

Is everything else working properly? I was once compelled to host a Django project on HostGator via FCGI so I know the pain of getting it set up. I may be able to dredge up some terrible memories if there are other issues, but I'd strongly suggest looking at Django-friendly host if you think you may continue with Django (I am happy with WebFaction who provides an installer to make getting Django set up via WSGI much easier).

like image 119
Tom Avatar answered Nov 15 '22 05:11

Tom