Deploying a Django App

Un article de alwaysdata.

Python is running under FastCGI. To use Django on our servers, you can look at Django on a shared-hosting provider with Apache. If you want a brief description of what to do, read on.

Preamble

Choose the version of Django you want to use by going to Admin › Environment › Python.

To create your new Django project execute the following via SSH in your root directory (not in your "www"):

django-admin.py startproject mysite
cd mysite/
./manage.py startapp myfirstapp

You can also look at the Django Documentation

Step 1

Now you should have a directory called mysite. Create a directory called public below that (so it will be mysite/public). Create a file django.fcgi with the following in it in that public directory:

#!/usr/bin/python
import os, sys

_PROJECT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, _PROJECT_DIR)
sys.path.insert(0, os.path.dirname(_PROJECT_DIR))

_PROJECT_NAME = _PROJECT_DIR.split('/')[-1]
os.environ['DJANGO_SETTINGS_MODULE'] = "%s.settings" % _PROJECT_NAME

from django.core.servers.fastcgi import runfastcgi
runfastcgi(method="threaded", daemonize="false")
  • Log in via SSH and execute chmod +x mysite/public/django.fcgi to make the file executable.
  • This file needs to have Unix line returns, so execute dos2unix mysite/public/django.fcgi via SSH.

Step 2

In the same directory as the django.fcgi file, add this in a .htaccess file:

AddHandler fcgid-script .fcgi
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ django.fcgi/$1 [QSA,L]
  • This file also needs to have Unix line returns, so you can execute dos2unix mysite/public/.htaccess via SSH.

Step 3

If you are using the Django administration interface, via SSH execute the following command replacing 1.1 with the Django version number you are using. Make sure you are still in the same directory as the .htaccess and django.fcgi files.

ln -s /usr/local/alwaysdata/python/django/1.1/django/contrib/admin/media/ media

For example, if you're using Django 1.2.4, execute this:

ln -s /usr/local/alwaysdata/python/django/1.2.4/django/contrib/admin/media/ media

Step 4

Now you will need to prepare the database. For mysql:

From AlwaysData's administration page, go to Admin › Databases › MySQL to create a MySQL database.

Use settings similar to those below in your mysite/settings.py file:

DATABASE_ENGINE = 'mysql'
DATABASE_NAME = 'myaccount_mybase'
DATABASE_USER = 'myaccount'
DATABASE_PASSWORD = 'mypassword'
DATABASE_HOST = 'mysql.alwaysdata.com'

You can also use PostgreSQL or SqlLite for the database.

Step 5

Currently you will not be able to access your site since all your files are in the mysite directory instead of www. To fix this problem, go to Admin › Domains, and type in /mysite/public/. This will make that the new web root for your site.

Now if you want to upload a file to your website, you will need to put it in mysite/public instead of www. For example, you could put your files in:

/home/myaccount/mysite/public/site_media

Then in mysite/settings.py file, change the variables to be as follows:

MEDIA_ROOT = '/home/myaccount/myproject/public/site_media/'
MEDIA_URL = '/site_media/'

In any templates you have, you can modify them to link to image/css/javascript files in the following way:

<img src="/site_media/images/myimage.gif" alt="my image" />

Step 6

When you're all done, your file structure should resemble the chart below.

mysite/
    __init__.py
    manage.py
    public/
        django.fcgi
        .htaccess
        media/
        site_media/
    settings.py
    urls.py
    myapp/
        views.py
        models.py

After certian changes, you will need to restart FastCGI. You can do this by going to the Admin › Advanced › Processes page and clicking the "Kill" button on the process.

Troubleshooting

If things aren't working out so well, you'll probably end up with either an "Unhandled Exception" or a "500 Internal Server Error."

To diagnose the problem, log into SSH and execute the following command:

tail -n 50 ~/admin/log/error.log

If you see something like the following error, you will need to run another command.

[Mon Dec 06 05:19:07 2010] [error] [client 217.128.57.136] Premature end of script headers: django.fcgi

Log into SSH and execute the file that is causing the problem. It should be something like this:

/home/myaccount/mysite/public/django.fcgi

If you get a bash: permission denied: /home/myaccount/mysite/public/django.fcgi error, run the following command:

chmod +x ~/mysite/public/django.fcgi

If the error message is something like import: unable to open X server `' @ import.c/ImportImageCommand/367., open the django.fcgi file and make sure that the first line is #!/usr/bin/python. Add this if it isn't already there.

If you are running into a contstant "Unhandled Exception" error, remember that you might need to kill the FastCGI process on the Admin › Advanced › Processes page. Refresh the page, and it might be fixed.

If you are still running into problems, you can go to the forums and ask a question or open a support ticket.