uWSGI is the most popular application gateway interface, like CGI, fastcgi. It is high-performance and pluggable. It implements application server interfaces for various languages and platforms: WSGI, PSGI, Rack, Lua WSAPI, CGI, PHP, Go, etc.

We can use the uWSGI emperor to manage application instances, and it's easy to configure massive applications. Let's start from building uWSGI.

1.Install libraries

apt-get install libbz2-dev libxml2-dev libyaml-dev zlib1g-dev libcurl4-openssl-dev libsqlite3-dev libncurses5-dev libedit-dev

Download the lastest source
wget http://projects.unbit.it/downloads/uwsgi-latest.tar.gz

tar zxf uwsgi-latest.tar.gz

2.Configure and build plugins

cd uwsgi-2.0.18

vi buildconf/base.ini

Change plugin directory

plugin_dir = /usr/local/lib/uwsgi

mkdir /usr/local/lib/uwsgi

Build uwsgi core

python uwsgiconfig.py --build core
cp uwsgi /usr/local/bin/

Build gevent plugin
python uwsgiconfig.py --plugin plugins/gevent

Build php plugin
apt-get install libxml2-dev libjpeg-dev libpng12-dev libfreetype6-dev libmcrypt-dev zlib1g-dev curl libcurl3 libcurl4-gnutls-dev libmhash2 libmhash-dev libonig-dev libqdbm-dev libdb-dev libkrb5-dev

Add dotdeb source(debian7)
vi /etc/apt/sources.list.d/dotdeb.list

deb http://packages.dotdeb.org wheezy all
deb-src http://packages.dotdeb.org wheezy all

wget --quiet -O - http://www.dotdeb.org/dotdeb.gpg | apt-key add - apt-get update

Install php7

apt-get install php7.0-dev php7.0-cgi php7.0-curl php7.0-gd php7.0-mcrypt php.0-sqlite3 php7.0-pgsql libphp7.0-embed
python uwsgiconfig.py --plugin plugins/php

Build cgi and psgi plugins

apt-get install perl libperl-dev
python uwsgiconfig.py --plugin plugins/cgi
python uwsgiconfig.py --plugin plugins/psgi

Build python2 plugin

apt-get install python-dev python-pip
python uwsgiconfig.py --plugin plugins/python

Build python3 plugin

apt-get install python3-dev python3-pip
python3 uwsgiconfig.py --plugin plugins/python core python3

Build rack plugin

apt-get install zlib1g-dev libcurl4-openssl-dev libsqlite3-dev imagemagick libmagick-dev libmagickwand-dev
apt-get install ruby ruby-dev
python uwsgiconfig.py --plugin plugins/rack

3.Add startup service

wget --quiet -O /etc/init.d/uwsgi https://mirror.scorpwill.com/init.d/uwsgi chmod +x /etc/init.d/uwsgi

Enable service(debian8)
systemctl enable uwsgi

uwsgi runs as user nginx, install nginx if not.

4.Configure applications

Put configuration files into /etc/uwsgi
mkdir /etc/uwsgi

Configuration for PHP, save as php.ini

[uwsgi]  
plugins = php
socket = /var/tmp/uwsgi_%n.sock
master = true
vaccum = true
lazy-apps = true
disable-logging = true
processes = 4
cheaper = 2
php-allowed-ext = .php
php-allowed-ext = .inc
php-index = index.php
php-set = date.timezone=Asia/Tokyo

Configuration for Django, save as django.ini

[uwsgi]  
plugins = python,gevent
socket = /var/tmp/uwsgi_%n.sock
master = true
vaccum = true
enable-threds = true
disable-logging = true
processes = 4
threads = 2
chdir = /var/www/myproject
module = myproject.wsgi:application

Configuration for Redmine, save as redmine.ini

[uwsgi]  
plugins = rack
socket = /var/tmp/uwsgi_%n.sock
master = true
vaccum = true
lazy-apps = true
disable-logging = true
processes = 2
chdir = /var/www/redmine
rack = config.ru
post-buffering = 4096
buffer-size = 25000
rbrequire = rubygems
rbrequire = bundler/setup
env = PATH=/usr/bin:/usr/local/bin
env = LD_LIBRARY_PATH=/usr/lib:/usr/local/lib
env = BUNDLE_GEMFILE=/var/www/redmine/Gemfile
env = RAILS_ENV=production

Configuration for Trac, save as trac.ini

[uwsgi]  
plugins = python,gevent
socket = /var/tmp/uwsgi_%n.sock
master = true
vaccum = true
enable-threds = true
lazy-apps = true
disable-logging = true
processes = 4
threads = 2
wsgi-file = /var/www/trac/cgi-bin/trac.wsgi

Finally, start uwsgi and reload nginx
service uwsgi start