Friday, December 30, 2011

Installing Django on Arvixe web hosting

On their main page, Arvixe web hosting claims that they support Python web hosting on their shared hosting servers. However, this is extremely misleading. Yes, they have a 3+ year old version of Python installed in the cavernous ruins which they call their servers, but it ends there. No documentation. Nothing on cpanel to provide any hints. No support for apache mod-wsgi which has become the de-facto standard for hosting Python on apache (mind you they don't support mod-python either).

Bottom line, there are hundreds of better alternatives for hosting Django/Python. I really liked www.alwaysdata.com which provided an intuitive 1-click easy install for any database, any recent version of python, and any version of Django (including the svn development trunk).

However, if you are a poor soul who still has another year subscription on his Arvixe hosting plan, I guess you have to make do with what you have. So here are the steps that I took to install the development version of Python/Django.

Get shell access

First off, you need to get shell access to the server in order to do the configuration. Simply send an email to Arvixe support and they should fix the problem in a couple minutes.

Once you get shell access, open up a terminal and log into the server

$ ssh username@your.website.com

Install python

We have a bunch of things to do here: install python, install django, optionally install virtualenv, create the cgi script. First off, we need to get a newer version of python installed to work with Django. I like to put my downloaded software in a directory called repos.

$ mkdir repos
$ cd repos
$ wget http://www.python.org/ftp/python/2.7.2/Python-2.7.2.tgz
$ tar -xzf Python-2.7.2.tgz

Next, we need to compile and install Python. Arvixe was nice enough to include a compiler on their server

$ cd Python-2.7.2
$ ./configure
$ make altinstall prefix=~ exec-prefix=~
$ cd ~/bin
$ ln -s python2.7 python
$ cd
$ echo "alias python='~/bin/python'" >> .profile

This installs python in your home directory and tells bash to use your version of python instead of the old one. For more of an explanation, go to http://mail.python.org/pipermail/tutor/2002-March/012903.html

Logout and log back in to see if the installation was successful

$ exit
$ ssh username@your.website.com

Now, when you type "python --version" into the console, you should see that we are using version 2.7.

Install Django

Next, we need to get django. Here, I get the development version from the svn repo, but any version should be fine

$ cd ~/repos
$ svn co http://code.djangoproject.com/svn/django/trunk/
$ cd trunk
$ python setup.py install

This installs django into the lib/ folder in your home directory. Though it is not necessary for this example, you might want to install virtualenv if you plan on hosting several Django apps with different versions of Django. http://pypi.python.org/pypi/virtualenv

Here is another guide which was very helpful to me. Instead of downloading and compiling a new, updated version of python, this person simply installed django using virtualenv. http://joemaller.com/1467/django-via-cgi-on-shared-hosting

Make the cgi application

Ok, now that we are done installing things, we need to tell Apache how to find your web application. For this example, I'm going to assume that we already have a working django application located in the directory:

/home/username/mydjangoapp/

Empty out your public_html directory and carry out the following steps.

$ cd ~/public_html

Your web app most likely uses static files, so we make a static directory to hold them. (alternatively make a symlink if your staticfiles are elsewhere) Also, Arvixe only lets us put cgi scrips in a directory called cgi-bin, so we make that as well.

$ mkdir static cgi-bin

Next, get this cgi script for django and put it in your cgi-bin directory.

$ cd ~/public_html/cgi-bin
$ wget https://code.djangoproject.com/raw-attachment/ticket/2407/django.cgi

Open it up in a text editor and change the following lines to tell it where to find your python installation and django application

1: #!/home/username/bin/python
95: sys.path.append("/home/username/mydjangoapp")
97: os.environ['DJANGO_SETTINGS_MODULE'] = 'mydjangoapp.settings'

Configure Apache

Create a .htaccess file in the ~/public_html/ directory with your favorite editor and add the following contents:

AddHandler cgi-script .cgi
RewriteEngine On
RewriteRule ^/(static.*)$ /$1 [QSA,L,PT]
RewriteRule ^/(cgi-bin.*)$ /$1 [QSA,L,PT]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /cgi-bin/django.cgi/$1 [QSA,L]

The last line tells the server to redirect all requests to the django.cgi script. However, we still want static request to go to the static directory, and requests to the cgi-directory must remain untouched or else we have an infinite loop on our hands.

So that should be it. Visit your your website in a browser and pray that your django web application shows up. If not... well, good luck.

11 comments:

  1. Hello Can you help me get this going on Arvixe if possible?

    http://flask.pocoo.org/docs/0.10/deploying/mod_wsgi/

    ReplyDelete
  2. Arvixe Web Hosting Comparison Review - Choose the Best Web Hosting
    • Free ‘.com’ domain
    • 99.9% uptime guarantee
    • 24/7 technical support
    • Unlimited email
    • 60-day money-back guarantee
    • WordPress integration
    Great reasons to choose Arvixe
    • Over 3 MILLION websites being hosted worldwide.
    • 93% of users say they would recommend Arvixe.

    Arvixe Web Hosting Discount

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. Dedicated hosting consists of single server with no one else hosting on that computer. This allows for the greatest of configuration options. Anyone who has a busy website will find dedicated hosting is the necessary choice. cheap web hosting in bangladesh



    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete