Creating a Python "Hello World" application with Django for ASO Shared Hosting
Posted by ASO Admin on 25 April 2014 03:11 PM
Preparing the Python Environment
The first thing we need to do is set up a local Python environment. For this we'll be using VirtualEnv, which is commonly used to isolate Python environments. This tutorial will require SSH access to your account, which is covered <here>. Also, in several places we refer to your account's home directory, and use "%USER%" (without the quotes) to refer to your account username. Make sure to replace every occurrence of "%USER%" with your actual account username.
First, log in to your account via SSH. If you're already logged in, let's ensure we're in the base of your home directory by entering:
Next we'll initialize our local python environment under the "pyenv" directory, relative to your home directory (/home/%USER%/pyenv):
virtualenv --python=python2.6 pyenvsource pyenv/bin/activate
After that last command, our prompt should now look a little different indicating we've entered our python environment. Now let's install Django:
pip install django
Creating your first Django application
Just in case you're revisiting this tutorial, please ensure we're still in or change to your account's home directory:
Now that our environment has been set up to support Django, we can create our first Django based project. For this tutorial we're going to create a standard Hello World application. First, let's create our django project:
~/pyenv/bin/django-admin.py startproject whyhellothere
And now change to our application's directory:
Now we need to finish creating our application directory layout:
mkdir publicmkdir tmp
Now, let's create a view for our application:
Inside this file, place these contents to create our first view:
from django.http import HttpResponse
Next, we need to connect our view to a URL:
At the top of this file, directly below the line "from django.conf.urls import patterns, include, url", add the following:
from whyhellothere.views import hello
Then, later in the file, you'll see " url(r'^admin/', include(admin.site.urls)),". Directly above this line, add:
Save and then close the file. Next we need to set up our static assets to serve them properly, open the following file:
At the bottom of the file, you should find a line that looks like "STATIC_URL = '/static/'", we need to change this to:
STATIC_URL = '/'
And directly below that line, add:
STATIC_ROOT = '/home/%USER%/whyhellothere/public/'
Save the file and close it. Now you'll need to "collect" the static assets from your new Django app. Run the following command:
~/pyenv/bin/python manage.py collectstatic
It should prompt a yes or no question about replacing files in the directory we gave above for the STATIC_ROOT setting. Enter 'yes'.
Setting up your new Django app for Passenger
We're almost done! Next, we need to create a file named "passenger_wsgi.py". This is the entry file to our application:
Inside this file, place the below contents substituting %USER% for your own user on the server:
import sys, os
Save and close the file. The final step is to connect our application to our domain's document root. Please be mindful that this involves replacing the directory with a symlink, so the document root must be empty to avoid file loss! If you have other domains or content hosted on your account, and you're setting this up for your primary domain, please contact support in regards to changing your document root first! If this is the only domain on your account and no other content is hosted here, it's safe to proceed as is, and we're assuming "public_html" is the document root in question. Adjust the directory for a different document root!
First, remove the old directory:
rm -fr /home/%USER%/public_html
Replace it with a symlink to our application's public folder:
ln -s /home/%USER%/whyhellothere/public /home/%USER%/public_html
Save and close the file. Now try visiting your website, you should see "Hello World".