+ All Categories
Home > Technology > Simple webapps with nginx, uwsgi emperor and bottle

Simple webapps with nginx, uwsgi emperor and bottle

Date post: 23-Jan-2015
Category:
Upload: jordi-soucheiron
View: 1,824 times
Download: 1 times
Share this document with a friend
Description:
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.
15
Simple webapps with ngnix, uwsgi emperor and bottle Jordi Soucheiron - @jordixou Backend engineer & sysadmin @ DEXMA @ The Barcelona Python Meetup Group 2014.02.20
Transcript
Page 1: Simple webapps with nginx, uwsgi emperor and bottle

Simple webapps with ngnix, uwsgi emperor

and bottle

Jordi Soucheiron - @jordixouBackend engineer & sysadmin @ DEXMA

@ The Barcelona Python Meetup Group 2014.02.20

Page 2: Simple webapps with nginx, uwsgi emperor and bottle

What is nginx

HTTP and reverse proxy server

Event driven

Very fast

Easy to configure

Page 3: Simple webapps with nginx, uwsgi emperor and bottle

What is uWSGI

WSGI compatible server

VERY flexible

Little overhead

Compatible with many frameworks like: bottle, flask, django, …

Page 4: Simple webapps with nginx, uwsgi emperor and bottle

What is bottle

Bottle is a fast, simple and lightweight WSGI web microframework

No additional dependencies

Uses decorators: @route, @get, @post, etc…

Page 5: Simple webapps with nginx, uwsgi emperor and bottle

Bottle Hello World

mkdir –p /opt/uwsgiApps/apps/

cd /opt/uwsgiApps/apps/

virtualenv bottle-hello

cd bottle-hello

pip install bottle

Page 6: Simple webapps with nginx, uwsgi emperor and 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

Page 7: Simple webapps with nginx, uwsgi emperor and bottle

Bottle Hello World

python bottle-hello.py

Try to access http://localhost:8080/

Page 8: Simple webapps with nginx, uwsgi emperor and bottle

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)

Page 9: Simple webapps with nginx, uwsgi emperor and bottle

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

Page 10: Simple webapps with nginx, uwsgi emperor and bottle

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

Page 11: Simple webapps with nginx, uwsgi emperor and bottle

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>

Page 12: Simple webapps with nginx, uwsgi emperor and bottle

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;

}}

Page 13: Simple webapps with nginx, uwsgi emperor and bottle

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

Page 14: Simple webapps with nginx, uwsgi emperor and bottle

Questions

Page 15: Simple webapps with nginx, uwsgi emperor and bottle

Links

http://nginx.org/

http://uwsgi-docs.readthedocs.org/

http://bottlepy.org/


Recommended