Yawd website

dec20

How to deploy a django application on a linux (Debian/Ubuntu) production server running ISPConfig 3

Here's a quick tutorial to have your django application "up n' running" in a few minutes! This guide applies to Ubuntu/Debian servers running ISPConfig 3 (w. Apache 2) for domain management.

Upload files

Start by uploading/copying the files to the desired location on the server. I normally prefer to have them under the /usr/share/django directory

> mkdir /usr/share/django
> cp -R /path/to/myproject/source/ /usr/share/django/myproject
> ls /usr/share/django/myproject/
media src static templates

Normally, in my own projects, myproject's contains the directories listed above. The media folder is the root of my media files - normally data that the user/administrator can modify. It is of no surprise that the src directory contains the source code. All my project-wide static files that are not located in an application's source code are placed under the static directory (as of Django 1.3, the staticfiles app was introduced to allow for improved static files management). Finally, all my project-wide templates are under the template directory.

Configure & collect static files

Open your django settings file (normally under /usr/share/django/myproject/src/myproject/settings.py) and edit/add the following path settings:

in settings.py:
PROJECT_ROOT = '/usr/share/django/myproject'

...

MEDIA_ROOT = os.path.join(PROJECT_ROOT, 'media')
MEDIA_URL = 'http://static.myproject.com/media/'

STATIC_ROOT = os.path.join(PROJECT_ROOT,'collected')
STATIC_URL = 'http://static.myproject.com/static/'

STATICFILES_DIRS = (
    os.path.join(PROJECT_ROOT, 'static'),
)

TEMPLATE_DIRS = (
    os.path.join(PROJECT_ROOT, 'templates'),
)

ADMIN_MEDIA_PREFIX = 'http://static.myproject.com/admin-media/'

....

Now it's about to collect our static files. As you can suspect from the code above, we must create a collected directory where all our static files will be stored.

> cd /usr/share/django/myproject/src/myproject
> python manage.py collectstatic

The wsgi apache module

To run our application we will use the mod_wsgi apache module, so make sure it is properly installed on your system.

> sudo apt-get install libapache2-mod-wsgi

Now we need to create a wsgi handler for our project

> mkdir /usr/share/django/myproject/apache/ >cd /usr/share/django/myproject/apache/ > vi django.wsgi

Place the following code in the wsgi file:

in django.wsgi:
import os
import sys

path = '/usr/share/django/myproject/src/'
if path not in sys.path:
    sys.path.append(path)
path += 'myproject'
if path not in sys.path:
    sys.path.append(path)

os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

Configure the web server

Log in to your ISPConfig 3 account. Visit the Sites menu link and click on 'Add new website'. We will setup the 'static' suddomain at first.

Screenshot of the ISP Config 3 - Add website pageFigure 1. Screenshot of the ISPConfig 3 - Add website page. Configure the 'static' subdomain.

Configure your domain as shown in figure 1. Make sure 'PHP' and 'CGI' are both disabled and 'auto-subdomain' is set to 'none'. Now go to the 'Options' tab and insert the following in the 'Apache directives' field:

Alias /media /usr/share/django/myproject/media
<Directory /usr/share/django/myproject/media>
     Options All -Indexes
     Order allow,deny
     Allow from all
</Directory>
Alias /static /usr/share/django/myproject/collected
<Directory /usr/share/django/myproject/collected>
     Options All -Indexes
     Order allow,deny
     Allow from all
</Directory>
Alias /admin-media /usr/lib/python2.5/site-packages/django/contrib/admin/media
<Directory /usr/lib/python2.5/site-packages/django/contrib/admin/media/>
     Options All -Indexes
     Order allow,deny
     Allow from all
</Directory>

Make sure the path to your django installation is correct for the admin media files to be served and save your configuration. The next step is to configure our main domain as shown in Figure 2. This time check that 'CGI' is enabled and 'auto-subdomain' is set to 'www'.

Screenshot of the ISP Config 3 - Add website pageFigure 1. Screenshot of the ISPConfig 3 - Add website page. Configure the domain.

Visit the 'Options' tab and insert the following in the 'Apache directives' field:

WSGIScriptAlias / /usr/share/django/myproject/apache/django.wsgi
<Directory /usr/share/django/myproject/apache/>
     Order deny,allow
     Allow from all
</Directory>

Save and exit. If everything went ok, you should now be able to visit http://www.myproject.com and see your application live!

Comments powered by Disqus