/ 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-1.0.2.1
  • dev-python/virtualenv-1.7.1.2-r1
  • 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 127.0.0.1;
#       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 127.0.0.1:443;
#       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" '
    '"$gzip_ratio"';

    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
/home/eshlox/projects
[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 $

# DO NOT MODIFY THIS FILE DIRECTLY! CREATE A COPY AND MODIFY THAT INSTEAD!

# Path (or name) of UNIX/TCP socket to bind to
#
UWSGI_SOCKET=/tmp/eshlox.net.sock

# Enable threads?
#
UWSGI_THREADS=1

# The path to your uWSGI application.
#
UWSGI_PROGRAM=/home/eshlox/projects/eshlox.net/project/project/wsgi.py

# The path to your uWSGI xml config file.
#
UWSGI_XML_CONFIG=

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

# The log file path. If empty logging is disabled
#
UWSGI_LOG_FILE=/var/log/uwsgi/eshlox.net

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

# If you want to run your application from a specific directiory specify
# it here. Leave this blank otherwise.
#
UWSGI_DIR=/home/eshlox/projects/eshlox.net/project/

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

# Additional options you might want to pass to uWSGI
#
UWSGI_EXTRA_OPTIONS="--touch-reload=/home/eshlox/projects/eshlox.net/project/reload"

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 ...