Simple webapps with nginx, uwsgi emperor and bottle

3,285 views

Published on

Bottle is a small microframework that lets you build simple python webapps in a few minutes. This talk will explain how to build simple webapp from scratch and configure your system to deploy many other apps concurrently with a rock solid and scalable setup.

Published in: Technology
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,285
On SlideShare
0
From Embeds
0
Number of Embeds
1,439
Actions
Shares
0
Downloads
20
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide

Simple webapps with nginx, uwsgi emperor and bottle

  1. 1. What is nginx  HTTP and reverse proxy server  Event driven  Very fast  Easy to configure
  2. 2. What is uWSGI  WSGI compatible server  VERY flexible  Little overhead  Compatible with many frameworks like: bottle, flask, django, …
  3. 3. What is bottle  Bottle is a fast, simple and lightweight WSGI web microframework  No additional dependencies  Uses decorators: @route, @get, @post, etc…
  4. 4. Bottle Hello World  mkdir –p /opt/uwsgiApps/apps/  cd /opt/uwsgiApps/apps/  virtualenv bottle-hello  cd bottle-hello  pip install bottle
  5. 5. 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
  6. 6. Bottle Hello World  python bottle-hello.py  Try to access http://localhost:8080/
  7. 7. Bottle Hello World v2  Add this (after the initial imports) to bottle-hello.py: from bottle import template @route('/hello/<name>') def greet(name='Stranger'): return template('Hello {{name}}, how are you?', name=name)  python bottle-hello.py  Try to access http://localhost:8080/hello/yourname
  8. 8. 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.log env BINPATH=/usr/local/bin/uwsgi exec $BINPATH --emperor /opt/uwsgiApps/conf.d/ --logto $LOGTO
  9. 9. 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
  10. 10. 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>
  11. 11. 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; } }
  12. 12. 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
  13. 13. Questions
  14. 14. Links  http://nginx.org/  http://uwsgi-docs.readthedocs.org/  http://bottlepy.org/

×