nginx + uwsgi emperor + bottle

11

Click here to load reader

Upload: jordi-soucheiron

Post on 11-May-2015

2.541 views

Category:

Technology


3 download

DESCRIPTION

How to easily create a nginx, uwsgi emperor and bottle deployment.

TRANSCRIPT

Page 1: nginx + uwsgi emperor + bottle

Simple webapps withnginx + uWSGI emperor +

bottle

Jordi Soucheiron - @jordixouSoftware developer & sysadmin @ DEXMA

@sudoersbcn 2013.10.1

Page 2: nginx + uwsgi emperor + bottle

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

Page 3: nginx + uwsgi emperor + bottle

Now the fun part starts

Page 4: nginx + uwsgi emperor + bottle

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

Page 5: nginx + uwsgi emperor + bottle

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:

Page 6: nginx + uwsgi emperor + bottle

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>

Page 7: nginx + uwsgi emperor + bottle

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; }

}

Page 8: nginx + uwsgi emperor + bottle

Let’s start it

service uwsgi startservice nginx restart

Page 9: nginx + uwsgi emperor + bottle

Testing it

$ curl localhost/time-utc.json

{"unix": "1380577343.16", "str": "2013-09-30 21:42:23 UTC"}

Page 10: nginx + uwsgi emperor + bottle

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

Page 11: nginx + uwsgi emperor + bottle

Questions?