wiki:Merengue/install/Nginx-WSGI-Virtualenv

Deploying Merengue with Nginx + uWSGI + Virtualenv

Let's suppose that we already have the following environment:

/home/merengue
|-- django_src (django-1.1 checkout)
|-- merengueproject (merengueproject checkout)
`-- virtualenv (our virtualenv containing all the necessary dependencies already installed)

uWSGI

  1. Now, we will download the uWSGI source code. That's needed because we need to add the uwsgi module to nginx.
    # wget http://projects.unbit.it/downloads/uwsgi-0.9.4.3.tar.gz
    
  2. Decompress the tarball and do the following:
    # tar xzf uwsgi-0.9.4.3.tar.gz
    # cd uwsgi-0.9.4.3
    # make
    

Note: That is if you're using Python2.5. However, if you use another python version, please check the  uwsgi documentation.

  1. At this time, you must to have the uwsgi binary file in the current directory. I moved it at /home/merengue/bin and changed the owner to merengue.

Nginx

We will use Nginx from source files because the nginx packages in the Debian repository are very out-of-dated. So we will download the development version.

Note: I have compiled all the packages as root, but you can do it as your user.

  1. First of all, we need to download the source files.
    # wget http://sysoev.ru/nginx/nginx-0.8.34.tar.gz
    
  2. Now, we are going decompress the tarball and to compile the nginx:
    # tar xzf nginx-0.8.34.tar.gz
    # cd nginx-0.8.34
    # ./configure --add-module=/path/to/uwsgi-0.9.4.3/nginx/
    # make && make install
    

At this time, we will have nginx installed at /usr/local/nginx. I moved this directory into /home/merengue/ and changed the owner to merengue.

  1. The following step will be to configure nginx (/home/merengue/nginx/conf/nginx.conf). I used the following configuration:

Include(source:trunk/merengueproj/conf/nginx/conf/nginx.conf, text/plain)?

Configure the Virtualenv

We need to add our PYTHONPATH and DJANGO_SETTINGS_MODULE into the virtualenv/bin/activate script.

...
export PYTHONPATH="/home/merengue/merengueproject/:/home/merengue/merengueproject/beta_merengue/:/home/merengue/merengueproject/beta_merengue/projapps:/home/merengue/merengueproject/beta_merengue/apps"
export DJANGO_SETTINGS_MODULE="settings"

Starting nginx and uwsgi manually

We will start nginx and uwsgi manually for testing. You must to execute the following commands:

# su - merengue
$ . virtualenv/bin/activate
$ /home/merengue/nginx/sbin/nginx -p /home/merengue/ -c /home/merengue/nginx/conf/nginx.conf

For starting uwsgi manually, execute the following command:

$ /home/merengue/bin/uwsgi -p 2 -C -M 4 -A 4 -m -s /home/merengue/sock/uwsgi.sock -H /home/merengue/virtualenv/ beta_merengue.wsgi --pythonpath /home/merengue/merengueproject/beta_merengue/ --pidfile /home/merengue/pid/beta_merengue.pid

Start and stop scripts

Starting manually this services would be a thorny issue. For making our lives more easy, I made a script for start and stop both services (nginx and uwsgi).

Here is the merengue script called merengue: Include(source:trunk/merengueproj/conf/scripts/merengue, text/plain)?

And here is the nginx control script called nginxctl: Include(source:trunk/merengueproj/conf/scripts/nginxctl, text/plain)?

And... that's all folks!, enjoy it!