Knowledgebase: cPanel
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:

cd ~

Next we'll initialize our local python environment under the "pyenv" directory, relative to your home directory (/home/%USER%/pyenv):

virtualenv --python=python2.6 pyenv
source 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:

cd ~

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:

cd whyhellothere

Now we need to finish creating our application directory layout:

mkdir public
mkdir tmp

Now, let's create a view for our application:

vi whyhellothere/views.py

Inside this file, place these contents to create our first view:

from django.http import HttpResponse
def hello(request):
    return HttpResponse("Hello World")

Next, we need to connect our view to a URL:

vi whyhellothere/urls.py

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:

url(r'^$', hello),

Save and then close the file. Next we need to set up our static assets to serve them properly, open the following file:

vi whyhellothere/settings.py

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:

vi "passenger_wsgi.py"

Inside this file, place the below contents substituting %USER% for your own user on the server:

import sys, os
virt_binary = "/home/%USER%/pyenv/bin/python"
if sys.executable != virt_binary: os.execl(virt_binary, virt_binary, *sys.argv)
sys.path.append(os.getcwd())

os.environ['DJANGO_SETTINGS_MODULE'] = "whyhellothere.settings"
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

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".

(3 vote(s))
This article was helpful
This article was not helpful

Help Desk Software by Kayako fusion
ERROR: This domain name (kb.asmallorange.com), does not match the domain name in the license key file help.asmallorange.com.

For assistance with your license, please contact the Kayako support team: https://support.kayako.com