For Systems Engineers coming in from PHP world, installing and configuring the software stack needed to run python + django applications can be a daunting task specially when dealing with multiple python versions. Or, when the operating system python version is not compatible with what is required by the python web application. Here's how I did it with Ubuntu:

  1. Set locale and timezone
    locale-gen en_US.UTF-8
    echo "Asia/Singapore" >  /etc/timezone
    dpkg-reconfigure --frontend noninteractive tzdata
  2. Update packages
    apt-get update && apt-get upgrade -y
  3. Limits
    ulimit -n 20000
    echo 'fs.file-max = 200000' >> /etc/sysctl.d/20_nginx.conf
  4. Install nginx
    apt-get install nginx -y
     
    cat << 'EOF' > /etc/nginx/nginx.conf
    user  www-data;
    worker_processes  1;
    worker_rlimit_nofile 200000;
     
    error_log  /var/log/nginx/error.log;
    pid        /var/run/nginx.pid;
     
    events {
        worker_connections  1024;
    }
     
    http {
     
        open_file_cache max=200000 inactive=20s; 
        open_file_cache_valid 30s; 
        open_file_cache_min_uses 2;
        open_file_cache_errors on;
        sendfile       on;
        tcp_nopush     on;
        tcp_nodelay    on;
        keepalive_timeout  65;
        reset_timedout_connection on;
        client_body_timeout 10;
        send_timeout 5;
     
        include       /etc/nginx/mime.types;
        default_type  application/octet-stream;
     
        log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                          '$status $body_bytes_sent "$http_referer" '
                          '"$http_user_agent" "$http_x_forwarded_for"';
        access_log /var/log/nginx/access.log main;
     
        server {
            listen       80;
            server_name  localhost;
     
            location / {
      include /etc/nginx/uwsgi_params;
      uwsgi_pass unix://var/run/uwsgi/app/app/app.sock;
            }
     
            error_page  404              /404.html;
            location = /40x.html {
                root   /usr/share/nginx/html;
            }
     
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   /usr/share/nginx/html;
            }
     
            location ~ /\.ht {
                deny  all;
            }
        }
     
    }
    EOF
  5. Install and config virtualenv
    apt-get install python-virtualenv virtualenvwrapper -y
    mkdir /var/webapps
    virtualenv /var/webapps/Env1
     
    # Or, if you have an alternate python version
    virtualenv -p python3 /var/webapps/Env2
     
    cat << 'EOF' >> /root/.bashrc
    export WORKON_HOME=/var/webapps
    source /etc/bash_completion.d/virtualenvwrapper
    EOF
  6. Install and config uWSGI
    apt-get install uwsgi uwsgi-plugin-python -y
     
    cat << EOF > /etc/uwsgi/apps-available/app.ini
    [uwsgi]
    plugin = python
    socket = /var/run/uwsgi/app/app/%n.sock
    stats = 127.0.0.1:3000
    pythonpath = /var/webapps/sample_proj/current
    home = /var/webapps/Env1
    env = DJANGO_SETTINGS_MODULE=helloworld.settings
    module = django.core.handlers.wsgi:WSGIHandler()
    master = true
    processes = 4
    harakiri = 60
    reload-mercy = 8
    max-requests = 2000
    limit-as = 512
    reload-on-as = 256
    reload-on-rss = 192
    no-orphans = true
    vacuum = true
    touch-reload = /var/run/uwsgi/app/%n
    EOF
     
    ln -s /etc/uwsgi/apps-available/app.ini /etc/uwsgi/apps-enabled/
     
    # To restart uwsgi, just do:
    touch /var/run/uwsgi/app/app
  7. Optional. Install python modules
    exit
    sudo su
    workon Env1
    pip install django 
  8. Sample django app
    cd /tmp
    wget https://github.com/django-ve/helloworld/archive/master.zip
    mkdir -p /var/webapps/sample_proj/current
    unzip master.zip && cp -a helloworld-master/* /var/webapps/sample_proj/current/
    cd /var/webapps/sample_proj/current/
    python manage.py syncdb
  9. Folder structure should look like
    (Env1)[email protected]:~# ls -la /var/webapps/sample_proj/current/
    total 68
    drwxr-xr-x 4 root root  4096 Feb 10 19:28 .
    drwxr-xr-x 3 root root  4096 Feb 10 19:07 ..
    drwxr-xr-x 2 root root  4096 Jun 12  2012 docs
    drwxr-xr-x 3 root root  4096 Feb 10 19:59 helloworld
    -rw-r--r-- 1 root root 35840 Feb 10 19:10 helloworld.db
    -rw-r--r-- 1 root root   253 Jun 12  2012 manage.py
    -rw-r--r-- 1 root root  1366 Jun 12  2012 README.rst
    -rw-r--r-- 1 root root  1216 Jun 12  2012 setup.py
  10. Start services
    service uwsgi start && service nginx start
  11. If you need a non-django sample hello world app
    import os
    import sys
     
    sys.path.append('/var/webapps/sample_proj/current')
     
    os.environ['PYTHON_EGG_CACHE'] = '/var/webapps/sample_proj/current/.python-egg'
     
    def application(environ, start_response):
        status = '200 OK'
        output = 'Hello World!'
     
        response_headers = [('Content-type', 'text/plain'),
                        ('Content-Length', str(len(output)))]
        start_response(status, response_headers)
     
        return [output]