nginx + uwsgi emperor + bottle
DESCRIPTION
How to easily create a nginx, uwsgi emperor and bottle deployment.TRANSCRIPT
Simple webapps withnginx + uWSGI emperor +
bottle
Jordi Soucheiron - @jordixouSoftware developer & sysadmin @ DEXMA
@sudoersbcn 2013.10.1
Package Installation
As root: Install nginx repository:
Install nginx packages:
echo "deb http://ppa.launchpad.net/nginx/stable/ubuntu precise main" > \ /etc/apt/sources.list.d/nginx.list apt-key adv --keyserver keyserver.ubuntu.com --recv-keys C300EE8C
apt-get updateapt-get install nginx python-pip python-dev build-essentialpip install virtualenvpip install uwsgipip install bottle
Now the fun part starts
Configure uWSGI emperor
Create /etc/init/uwsgi.conf:
# uWSGI - manage uWSGI application server description "uWSGI Emperor"
start on (filesystem and net-device-up IFACE=lo)stop on runlevel [!2345]
respawn
env LOGTO=/var/log/uwsgi/uwsgi.logenv BINPATH=/usr/local/bin/uwsgi
exec $BINPATH --emperor /opt/uwsgiApps/conf.d/ --logto $LOGTO
Create a simple test app
mkdir -p /opt/uwsgiApps/conf.d/mkdir -p /opt/uwsgiApps/apps/simpleTestApp/
import jsonimport timefrom datetime import datetimeimport bottle
@bottle.route('/time-utc.json')def utc_time(): ts = { 'unix': str(time.time()), 'str': datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S UTC") } return json.dumps(ts)
if __name__ == '__main__': bottle.run(bottle.default_app(), host='0.0.0.0', port=80, reloader=True)else: application = bottle.default_app()
/opt/uwsgiApps/apps/simpleTestApp/bottleMain.py:
uWSGI app config
/opt/uwsgiApps/conf.d/simpleTestApp.xml:
<uwsgi><master>true</master><processes>1</processes><vaccum>true</vaccum><chmod-socket>666</chmod-socket><socket>/tmp/%n.sock</socket><uid>www-data</uid><gid>www-data</gid><pythonpath>/opt/uwsgiApps/apps/%n</pythonpath><module>bottleMain</module>
</uwsgi>
nginx config file
/etc/nginx/conf.d/simpleTestApp.conf:
config nginx:server { listen 80; server_name your-url.com;
location / { include uwsgi_params; uwsgi_modifier1 30; uwsgi_pass unix://tmp/simpleTestApp.sock; }
error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; }
}
Let’s start it
service uwsgi startservice nginx restart
Testing it
$ curl localhost/time-utc.json
{"unix": "1380577343.16", "str": "2013-09-30 21:42:23 UTC"}
Random stuff
Whenever a uWSGI configuration file is touched the app is restarted
Adding another app is as simple as dumping the files in the right place As soon as we write the uWSGI configuration
file it will try to load the application
It is possible to use other this with microframeworks like Flask
Questions?