Zentyal Server offers some features that allow it to be used as a web server of not only static content, but also of different kinds of rich applications. In this post I’ll explain step by step how to publish a django application using its own domain.

As you may know, Django is an open source web framework designed to make easier the development of complex, database-driven websites, with emphasis in high modularity, automatization and the use of widely known design patterns such as MVC. It’s also distributed with very useful plugins that offer implementations of common features such as basic authentication and a CRUD interface.

In this tutorial we’ll make use of the wsgi interface to deploy the application.

To start with, you should have:

  • A Zentyal Server, with Web Server module installed.
  • A Django application to deploy, e.g. exampleapp
  • A domain, e.g. example.com

So, let’s start!

Creating the virtual host

The first step is to create a virtual host in the Zentyal Server: a virtual host is used to give a different configuration for a specific location in a physical server that serves different domains from the same IP.

Now go to your Zentyal Server web interface and click on Web Server, if you have the module installed, you’ll find it in the Infrastucture section. If not, install it!

Once you are in the Web Server configuration screen, click on Add New…, you’ll see that basically you only need to specify the domain of your virtual host to create it. You can also select if you want to enable the virtual host (you do) and if you want to enable SSL support, for that you’d also need to create a certificate first, you can check the Web Server module documentation for more information.

Now just enter your domain in the Name field and click on Add. Save changes and that’s it! If your domain is already configured to resolve to the IP address of your Zentyal Server you can try to visit your new web site. It will currently show only an empty directory, so let’s fill it with your application.

Preparing the Django application

If your application already has a wsgi (Web Server Gateway Interface) file, skip this step, if not, open a text editor and write:

import os
import os.path
import sys

#sys.path.append(os.path.realpath(os.path.dirname(__file__)))
#sys.path.append('/srv/www/example.com')

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

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

Not everything is always needed here, but there are three mandatory lines:

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

Tells the Django wgsi handler which one is the settings module of the application to serve.

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

Instantiates the wsgi handler that will handle the interactions between the web server and the Django application.

The other lines can be used to add the path where your application is installed to the python path: the first one considers that the application is installed in the same directory as the wsgi file itself, the second one locates the application in the directory created by Zentyal for the virtual host. If your application is installed in any of the default paths, you don’t need any of these lines. You can also put any other valid python code there that your application might need.

Save your file where you want, e.g. in /srv/www/example.com/example.wsgi.

Configure your web server

And the two final steps are to check that your web server has all the needed dependencies and to add the last configuration.

The dependencies are the Python and WSGI modules for apache and, of course, Django. To install these dependencies and enable the modules run the commands:

sudo apt-get install libapache2-mod-python libapache2-mod-wsgi python-django
sudo a2enmod python
sudo a2enmod wsgi
sudo /etc/init.d/apache2 reload

Finally, you can add any configuration to your virtual host by adding files to /etc/apache2/sites-available/user-ebox-example.com. For this example let’s map a path in the url to the wsgi handler. To do this you can use the WSGIScriptAlias apache directive and the wsgi file, so open again your favorite editor and type:

WSGIScriptAlias / /srv/www/example.com/django.wsgi

This indicates that when a request asks for root in this virtual host, it will be handled with the indicated file. Change the path if your wsgi file is in another place. After that, save it in /etc/apache2/sites-available/user-ebox-example.com/django, reload apache and your application will be deployed.

Summary

  • Add a virtual host
  • Prepare a wgsi file
  • Install dependencies (libapache2-mod-python, libapache2-mod-wsgi and python-django at least)
  • Add the WSGIScriptAlias directive to your virtual host configuration

As you can see, it’s really easy to deploy complex web applications in your own domain using Zentyal, why not try?

A post by Jaime Soriano