Feb. 21, 2013, midnight

Setting up Django with Apache and uwsgi on Ubuntu 12.10

I will show a alternative approach to serve Django apps in apache, without using the libapache2-mod-wsgi. The choosen method is setup a uwsgi server as backend and apache as frontend, also if you desire, you can use Nginx instead.

user@host:~$ sudo apt-get install apache2 libapache2-mod-uwsgi

user@host:~$ sudo apt-get install uwsgi uwsgi-plugin-python uwsgi-plugin-cgi

Installing these packages will let you with the following config files and folder structures:

Setup uwsgi server

The file * /etc/default/uwsgi * is where you put global configuration for the uwsgi server, this article dosen't require any change in this file, but you should be aware of it content.

To add a new application, let’s say: voorloopnul, you have to create the file /etc/uwsgi/apps-available/voorloopnul.ini:

socket =
workers = 2
chdir = /srv/apps/voorloopnul
env = DJANGO_SETTINGS_MODULE=voorloopnul.settings
module = django.core.handlers.wsgi:WSGIHandler()

Next, you have to create a symbolic link between the file in apps-available to apps-enabled:

user@host:~$ ln -s /etc/uwsgi/apps-available/voorloopnul.ini /etc/uwsgi/apps-enabled/voorloopnul.ini

Now you can start the service:

user@host:~$ service uwsgi restart

It's a good idea to put the folder /srv/app/voorloopnul in ownership of www-data.

user@host:~$ chown -R www-data.www-data /srv/app/voorloopnul

To reload the application just type:

user@host:~$ touch /tmp/voorloopnul.com

Setup apache

Apache will act as a frontend for uwsgi server, each request to apache will be redirected to be processed by the uwsgi server. The comunication will be done by the tcp socket created on last step. So you have to create the /etc/apache2/sites-available/voorloonul.com file with this content:

<VirtualHost *:80>
    ServerAdmin your@email.com
    ServerName voorloopnul.com
    ServerAlias www.voorloopnul.com

    DocumentRoot /var/www

    <Directory />
        Options FollowSymLinks Indexes
        AllowOverride None

    <Directory /var/www/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride None
        Order allow,deny
        allow from all

    <Location />
        Options FollowSymLinks Indexes
        SetHandler uwsgi-handler

   <Location /static>
        SetHandler none

    Alias /static //srv/apps/voorloopnul/voorloopnul/static/

    ErrorLog ${APACHE_LOG_DIR}/error.log
    LogLevel warn
    CustomLog ${APACHE_LOG_DIR}/voorloopnul.com.access.log combined

The uwsgi server is not designed to process static data(css,img,js), to let this responsability for apache we create a location and define the handler for this content as none, this way any content in /static will be ignored by the uwsgi server.

Now you just have to enable this site and reload the apache webserver:

user@host:~$ a2ensite voorloopnul.com

user@host:~$ apache2 reload

comments powered by Disqus