Rent my time and knowledge so I can help with your project!


Ricardo Pascal

Usually a Python programmer, sometimes an Angular programmer, a physics dropou planning a triumphal come back, currently working as bioinformatician and team leader in a biotech company in south Brazil.

Enjoy expose/consume data from rest API's and teach/evangelize python newcomers.

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:

  • /etc/apache2/sites-available
  • /etc/uwsgi/apps-available
  • /etc/default/uwsgi

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()
  • line 1 define the file that will control the reload of your application (you must create this file in advance)
  • line 2 tell uwsgi to serve this app through a tcp socket on port 3041.
  • line 3 start the app with two uwsgi workers
  • line 4 should point to the root of your django project,
  • line 5 should point to your project settings

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/

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/ file with this content:

<VirtualHost *:80>

    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}/ 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

user@host:~$ apache2 reload

by Ricardo Pascal on Feb. 21, 2013

comments powered by Disqus