Sign up to get extra content & updates via email!

Thanks for signing up!
I appreciate it!

Running Django under a Shared Hosting Environment

python-django
I’ve been developing with Django on my local machine,
and I was wondering if there was a way for me to
deploy it on a shared hosting server,
after playing around with it for a few hours, it actually works!

I’m quite surprised so much is possible without having root access (installing Python, Pip & Django).

You will need SSH access though, but most decent hosts give that nowadays.
The 3 steps you have to go through are:

Python
You download the source code, and do the old ./configure, make , make install.
the only difference is that (because you have no root access) you have to install into your local directory, by using:

./configure -prefix=/home/directory/...

and then

make
make install

You also have to add Python to your Path by writing
PATH=/home/directory/python/bin:$PATH

you should also add this to your /home/directory/.bashrc (the file should already exist)
so that command will run once your session starts.

Pip
just download pip here https://pypi.python.org/pypi/pip
and install via

python setup.py install

Django
try writing

pip install Django 

and if you want some MySQL support:

pip install MySQL-python

for some reason, the installation of django didn’t work for me, so I
simply downloaded django and
copied the folder “django” (ignoring the other folders “docs” “extras” “scripts” and “tests” alltogether) into my
“site-packages” folder (should be located wherever you installed your python)

Worked just fine.

Then I created a Django project via going to /home/directory/djangoproject (new directory):

django-admin.py startproject djangoproject

(notice that this is not inside the www (or public_html) folder!)
Now for the last (and trickiest) part:

Django on Apache
Inside the www folder, I created a subfolder called “djangoproject” (the name doesn’t really matter)
and inside created 2 files:
djangoproject.fcgi
contains:

#!/home/directory/bin/python
import sys, os

sys.path.insert(0, "/home/directory/python/python27/site-packages")
sys.path.insert(13, "/home/directory/djangoproject")

os.environ['DJANGO_SETTINGS_MODULE'] = 'djangoproject.settings'
from django.core.servers.fastcgi import runfastcgi
runfastcgi(method="threaded", daemonize="false")

and .htaccess file that contains:

AddHandler fcgid-script .fcgi
RewriteEngine On

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

After that I accessed the website.com/djangoproject folder, and VoilĂ  !

For more information, I highly recommend this blog post

Update

If you’re having some issues that you can’t figure out with Apache, try running the djangoproject.fcgi file directly via:

./djangoproject.fcgi

this will give you more clues as to what went wrong (python problem, permissions, etc.)

Update 2

Sometimes, changes within the .fcgi file are not taken by apache until you restart it.

Tagged with: , , , ,
Posted in Technology