nginx+uWSGI+bottle

ubuntu 14.04

【install】

sudo apt-get -y update
sudo apt-get -y upgrade
sudo apt-get dist-upgrade -y
sudo apt-get -y install build-essential
sudo apt-get -y install python3-dev
sudo apt-get install python3
wget https://pypi.python.org/packages/source/d/distribute/distribute-0.7.3.zip
sudo apt-get install unzip
unzip distribute-0.7.3.zip
cd distribute-0.7.3
sudo python3 setup.py install
sudo easy_install pip
sudo pip install uwsgi
sudo apt-get install nginx
sudo pip install bottle

セキュリティパッチ自動更新
sudo dpkg-reconfigure --priority=low unattended-upgrades

SSH総当たり攻撃予防
sudo apt-get install -y fail2ban

 
nginxの設定
/etc/nginx/conf.d/app.conf

一旦、http版

server {
    listen       80;
    server_name  mknod.server;
    access_log   /var/log/nginx/mknod.server.log;

    location / {
        include     uwsgi_params;
        uwsgi_pass  unix:///tmp/uwsgi.sock;
    }
}

nginx起動
sudo service nginx start
app.py

#!/usr/bin/env python
# -*- coding:utf-8 -*-

from bottle import route, run, default_app

@route('/')
def hello():
    return 'Hello World! abc'

if __name__ == '__main__':
    run(host='mknod.server', port=1234)
else:
    application = default_app()

通常のbottleアプリとして起動するときは

python3 app.py

動作確認は、

curl http://mknod.server:1234/

uwsgi経由で起動するときは下記のように設定。

uwsgiの設定と起動

mkdir ~/app
cd ~/app
touch uwsgi.ini    

# uwsgi.iniを下記のように編集

sudo uwsgi uwsgi.ini

uwsgi.ini

[uwsgi]
socket       = /tmp/uwsgi.sock
pidfile      = /tmp/uwsgi/mknod.server.pid
daemonize    = /tmp/uwsgi/mknod.server.log
master       = True
file         = app.py

chmod-socket = 666

uwsgiの停止

sudo kill -QUIT `cat /tmp/uwsgi/mknod.server.pid`

 

Leave a Reply