August 25, 2012

November 25, 2019

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.

eshlox@ping ~ $ 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@eshlox ~ $ cd projects/
eshlox@eshlox ~/projects $ pwd
eshlox@eshlox ~/projects $ virtualenv
New python executable in
Also creating executable in
Installing setuptools............done.
Installing pip...............done.
eshlox@eshlox ~/projects $ cd
eshlox@eshlox ~/projects/ $ source bin/activate
( ~/projects/ $ pip install django
Downloading/unpacking django
Downloading Django-1.4.1.tar.gz (7.7Mb): 7.7Mb downloaded
Running egg_info for package django
Installing collected packages: django
Running install for django
changing mode of build/scripts-2.7/ from 644 to 755
changing mode of /home/eshlox/projects/ to 755
Successfully installed django
Cleaning up...
( ~/projects/ $ startproject project
( ~/projects/ $ ls
bin include lib lib64 project
( ~/projects/ $ ls project/ project
( ~/projects/ $ ls project/project/

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

server {
listen 80;
access_log /var/log/nginx/eshlox.net_access.log;
error_log /var/log/nginx/eshlox.net_error.log;
location / {
uwsgi_pass unix:///tmp/;
include uwsgi_params;
location /media/ {
alias /home/eshlox/projects/;
location /static/ {
alias /home/eshlox/projects/;

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

eshlox@eshlox ~ $ cd /etc/nginx/sites-available/
eshlox@eshlox /etc/nginx/sites-available $ ln -s ../sites-enabled/
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

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

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/ start
* Starting uWSGI application ...
© 2020 Przemysław Kołodziejczyk