Monday, December 19, 2011

Django+nginx+uwsgi+supervisor

Because I've heard so many people talking about switching from Apache to this combination, I thought I would give it a try. After scouring the Internet and reading some blog posts, I came up with a setup, which I'm documenting here, because there were some gotchas for me.

I use Ubuntu 11.04, so this is the list of required packages:
• python-pip
• python-virtualenv
• postgresql
• postgresql-dev
• python2.7-dev (uwsgi requirement)
• libxml2-dev (uwsgi requirement)
• libpcre3 (nginx requirement)
• libpcre3-dev (nginx requirement)
• nginx

I install nginx with apt-get to have the necessary directory created in /etc/, but I'll replace the binary with the one I get after building the source of the latest version:

wget http://nginx.org/download/nginx-1.0.11.tar.gz
tar -xzf nginx-1.0.11.tar.gz
cd nginx-1.0.11/
./configure --conf-path=/etc/nginx/nginx.conf\
--sbin-path=/usr/sbin\
--without-http-cache
make
make install

The configuration options are so few because this is what I need in development, but for production more should be added.

Next, I just use django-startproject, to create a Django project and start working. My fork of this application does some extra things. First, it assumes very similar development and production environments, so the same stack is also used for development (nginx, uwsgi, supervisor, postgresql). Second, it automates a number of tasks done at the start of each project or on a new deployment. A typical work-flow is like this:
1. Install django-startproject:
    pip install -e git+https://github.com/LucianU/django-startproject#egg=django-startproject

2. Go to the location where you want to create your new project and run:
    django-startproject.py projectname
    At this point, you are prompted for some values and then the setup is done. During the setup, all the placeholders are replaced in the project template with the values corresponding to the current machine and the ones provided at the prompt. Afterwards, a virtual env is created and the packages used for development are installed: Django, psycopg2, supervisor, uwsgi, django-debug-toolbar. I prefer installing uwsgi and supervisor in a virtual env and not globally.

3. Create a database and put your credentials in the project's settings/local.py file.

4. Activate your environment and run django-admin.py syncdb

5. Symlink project/confs/development/nginx.conf into the 'sites-enabled' directory of your nginx installation and restart the server

If there weren't any problems, you should be done and running supervisord should start uwsgi, and you should be able to access the Django greeting page.

django-startproject also offers a script that can be used for deployment. For example, if someone wants to start working on a project created with django-startproject, they just clone the repository, run bin/bootstrap.py dev, and then follow the quickstart from step 3 onwards.

Last, I want to mention some things about nginx's configuration system because this was unclear to me at the start. nginx has a global configuration file (located at /etc/nginx/nginx.conf in case of Ubuntu) and two directories, sites-available and sites-enabled. Generally, in nginx.conf are found settings applying to the server as a whole, like used modules, compression, and so on. On the other hand, the configuration files placed in sites-enabled are specific to the served app, containing the domain name and the location of application and its logs.