Date post: | 23-Jan-2015 |
Category: |
Technology |
Upload: | jordi-soucheiron |
View: | 1,824 times |
Download: | 1 times |
Simple webapps with ngnix, uwsgi emperor
and bottle
Jordi Soucheiron - @jordixouBackend engineer & sysadmin @ DEXMA
@ The Barcelona Python Meetup Group 2014.02.20
What is nginx
HTTP and reverse proxy server
Event driven
Very fast
Easy to configure
What is uWSGI
WSGI compatible server
VERY flexible
Little overhead
Compatible with many frameworks like: bottle, flask, django, …
What is bottle
Bottle is a fast, simple and lightweight WSGI web microframework
No additional dependencies
Uses decorators: @route, @get, @post, etc…
Bottle Hello World
mkdir –p /opt/uwsgiApps/apps/
cd /opt/uwsgiApps/apps/
virtualenv bottle-hello
cd bottle-hello
pip install bottle
Bottle Hello World
vi bottle-hello.py:
from bottle import route, run
@route('/hello')def hello(): return "Hello World!"
if __name__ == '__main__': run(port=8080, debug=True)else: application = app
Bottle Hello World
python bottle-hello.py
Try to access http://localhost:8080/
Bottle Hello World v2
Add this (after the initial imports) to bottle-hello.py:
python bottle-hello.py
Try to access http://localhost:8080/hello/yourname
from bottle import template@route('/hello/<name>')def greet(name='Stranger'): return template('Hello {{name}}, how are you?', name=name)
uWSGI installation
pip install uwsgi
vi /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
uWSGI emperor
One of many uWSGI configuration options
One master process
Many independent child processes
Each application has a config file
Touch or modify the config file to restart the application
uWSGI example config
vi /opt/uwsgiapps/conf.d/bottle-hello.xml:
<uwsgi><master>true</master><processes>1</processes><vaccum>true</vaccum><chmod-socket>600</chmod-socket><socket>/tmp/%n.sock</socket><uid>www-data</uid><gid>www-data</gid><pythonpath>/opt/uwsgiApps/apps/%n/src/</pythonpath><module>scatterapp</module>
</uwsgi>
nginx configuration
vi /etc/nginx/conf.d/subdomain1.domain.com.conf:
server {listen 80;server_name subdomain1.domain.com.conf;
location / {include uwsgi_params;uwsgi_pass unix://tmp/bottle-example.py;
}}
System overview
nginx acts as a reverse proxy
nginx redirects the requests based on the domain name or any other parameter (ip address, url path, cookies, etc)
uWSGI starts and stops the applications
bottle is used to program the application
Questions
Links
http://nginx.org/
http://uwsgi-docs.readthedocs.org/
http://bottlepy.org/