/ linux

Nginx, Uwsgi, Virtualenv and Django on Gentoo

In short, I will describe the installation and the configuration of Nginx, Uwsgi, Virtualenv and Django on Gentoo Linux.

Versions of packages (with flags) that will be used:

  • www-servers/nginx-1.2.1
  • www-servers/uwsgi-
  • dev-python/virtualenv-
  • Django 1.4.1

Install packages.

emerge -av nginx uwsgi virtualenv

Edit /etc/nginx/nginx.conf. In http section comment, an index and all servers subsections.

#index index.html;

#server {
#       listen;
#       server_name localhost;

#       access_log /var/log/nginx/localhost.access_log main;
#       error_log /var/log/nginx/localhost.error_log info;

#       root /var/www/localhost/htdocs;

# SSL example
#server {
#       listen;
#       server_name localhost;

#       ssl on;
#       ssl_certificate /etc/ssl/nginx/nginx.pem;
#       ssl_certificate_key /etc/ssl/nginx/nginx.key;

#       access_log /var/log/nginx/localhost.ssl_access_log main;
#       error_log /var/log/nginx/localhost.ssl_error_log info;

#       root /var/www/localhost/htdocs;

Instead this, add include statement for future configurations.

include /etc/nginx/sites-enabled/*;

The entire configuration file with small additions.

[email protected] ~ $ cat /etc/nginx/nginx.conf 
user nginx nginx;
worker_processes 1;

error_log /var/log/nginx/error_log info;

events {
    worker_connections 1024;
    use epoll;

http {
    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    log_format main
    '$remote_addr - $remote_user [$time_local] '
    '"$request" $status $bytes_sent '
    '"$http_referer" "$http_user_agent" '

    client_header_timeout 10m;
    client_body_timeout 10m;
    send_timeout 10m;

    connection_pool_size 256;
    client_header_buffer_size 1k;
    large_client_header_buffers 4 2k;
    request_pool_size 4k;

    gzip on;
    gzip_min_length 1100;
    gzip_buffers 4 8k;
    gzip_types text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;

    output_buffers 1 32k;
    postpone_output 1460;

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;

    keepalive_timeout 75 20;

    ignore_invalid_headers on;

    include /etc/nginx/sites-enabled/*;

Create two directories for configuration files.

mkdir /etc/nginx/sites-available
mkdir /etc/nginx/sites-enabled

At this point, create a virtual environment and install Django. Assume that the project is kept in /home/eshlox/projects/ and is called eshlox.net.

[email protected] ~ $ cd projects/
[email protected] ~/projects $ pwd
[email protected] ~/projects $ virtualenv eshlox.net
New python executable in eshlox.net/bin/python2.7
Also creating executable in eshlox.net/bin/python
Installing setuptools............done.
Installing pip...............done.
[email protected] ~/projects $ cd eshlox.net/
[email protected] ~/projects/eshlox.net $ source bin/activate
(eshlox.net)[email protected] ~/projects/eshlox.net $ pip install django
Downloading/unpacking django
  Downloading Django-1.4.1.tar.gz (7.7Mb): 7.7Mb downloaded
  Running setup.py egg_info for package django
Installing collected packages: django
  Running setup.py install for django
    changing mode of build/scripts-2.7/django-admin.py from 644 to 755
    changing mode of /home/eshlox/projects/eshlox.net/bin/django-admin.py to 755
Successfully installed django
Cleaning up...
(eshlox.net)[email protected] ~/projects/eshlox.net $ django-admin.py startproject project
(eshlox.net)[email protected] ~/projects/eshlox.net $ ls
bin  include  lib  lib64  project
(eshlox.net)[email protected] ~/projects/eshlox.net $ ls project/
manage.py  project
(eshlox.net)[email protected] ~/projects/eshlox.net $ ls project/project/
__init__.py  settings.py  urls.py  wsgi.py

Let us return to the Nginx configuration. Create and edit /etc/nginx/sites-available/eshlox.net.
Sample configuration.

server {
    listen  80;
    server_name eshlox.net www.eshlox.net;
    access_log /var/log/nginx/eshlox.net_access.log;
    error_log /var/log/nginx/eshlox.net_error.log;

    location / { 
        uwsgi_pass unix:///tmp/eshlox.net.sock;
        include     uwsgi_params;

    location /media/  {
        alias /home/eshlox/projects/eshlox.net/project/project/media/;

    location  /static/ {
        alias  /home/eshlox/projects/eshlox.net/project/project/static/;

After that, make symlink to activate new project. Only configuration files stored in sites-enabled directory are used.

[email protected] ~ $ cd /etc/nginx/sites-available/
[email protected] /etc/nginx/sites-available $ ln -s eshlox.net ../sites-enabled/eshlox.net

Time for Uwsgi.

Add uwsgi user.

useradd -r -M -G uwsgi uwsgi

Create configuration file.

eshlox ~ # cd /etc/conf.d/
eshlox conf.d # cp uwsgi uwsgi.eshlox.net

Sample configuration file.

# Distributed under the terms of the GNU General Public License v2
# $Header: /var/cvsroot/gentoo-x86/www-servers/uwsgi/files/uwsgi.confd,v 1.1 2011/05/31 19:49:07 maksbotan Exp $


# Path (or name) of UNIX/TCP socket to bind to

# Enable threads?

# The path to your uWSGI application.

# The path to your uWSGI xml config file.

# The number of child processes to spawn. The default is 1.

# The log file path. If empty logging is disabled

# If you want to run your application inside a chroot then specify the
# directory here. Leave this blank otherwise.

# If you want to run your application from a specific directiory specify
# it here. Leave this blank otherwise.

# The user and group to run your application as. If you do not specify these,
# the application will be run as root:root.

# Additional options you might want to pass to uWSGI

Again, you must create symlink to activate new uwsgi configuration.

eshlox ~ # cd /etc/init.d/
eshlox init.d # ln -s uwsgi uwsgi.eshlox.net

That's all. Now just run the applications.

eshlox ~ # /etc/init.d/nginx start
 * Checking nginx configuration ...                                    [ ok ]
 * Starting nginx ...                                                  [ ok ]
eshlox ~ # /etc/init.d/uwsgi.eshlox.net start
 * Starting uWSGI application eshlox.net ...