実行環境
- Ubuntu 14.2
- Python 3.4
- uWSGI
- Nginx
手間なのでvirtualenvは利用せずに設定します。
なおサーバー環境はvagrantでまっさらなところから作っていきます。
環境構築
さくっと作ります。
vagrantでVM構築
ご存じのvagrant。既に環境がある方は読み飛ばしてください。
$ vagrant init ubuntu14
....
....
`vagrantup.com` for more information on using Vagrant.
$ vagrant up
....
....
==> default: Checking for guest additions in VM...
==> default: Mounting shared folders...
default: /vagrant => xxx/xxx/xxx/uwsgi
$ vagrant ssh
Python3、uWSGIのインストール
念のため現在のバージョン確認。
$ python -V
Python 2.7.6
Python3のインストール。
$ sudo apt-get install python3
distribute(easy_install) -> pip の順にインストール。
# 最新のdistributeはこちらで確認してください。
# https://pypi.python.org/pypi/distribute
$ wget https://pypi.python.org/packages/source/d/distribute/distribute-0.7.3.zip
# zip解凍のためにunzipインストール
$ sudo apt-get install unzip
# distributeを解凍してインストール
$ unzip distribute-0.7.3.zip
$ cd distribute-0.7.3/
$ sudo python3 setup.py install
# インストールされたeasy_installでもってpipをインストール
$ sudo easy_install pip
これでpipが使えます。
このままuWSGIをインストールしましょう。
#uWSGIインストールに必要なpython3-devをインストール
$ sudo apt-get install python3-dev
#uWSGIをインストール
$ sudo pip install uwsgi
Nginxのインストール
Nginxはさくっと。
$ sudo apt-get intall nginx
$ sudo service nginx start
これで環境構築は完了です。
bottleで簡単なアプリを組む
次にbottleでサンプルアプリを組みます。 まずはbottleをインストール。
$ sudo pip install bottle
次にbottleを利用して簡単なアプリを組みます。
$ cd ~
$ mkdir myapps
$ cd myappps
$ vim index.py
index.pyの中身
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from bottle import route, run, default_app
@route('/')
def hello():
return 'Hello World!'
if __name__ == '__main__':
# コマンドから"python3 index.py"で起動した場合
run(host='localhost', port=3030)
else:
# uWSGIから起動した場合
application = default_app()
bottleのサーバー立ち上げて、wgetでアクセスしてみます。
$ nohup python3 index.py > out.log &
$ wget http://localhost:3030/
$ less index.html
> Hello World!
問題なければプロセスkillしておきましょう。
各種設定
NginxやuWSGIなどの設定を行います。
適宜実環境に置き換えて考えてください。
hosts書き換え
ローカルで確認しやすいようhostsを書き換えます。
$ sudo vim /etc/hosts
127.0.0.1 myapps.com
を追記して閉じます。以下のような感じ。
127.0.0.1 localhost
127.0.0.1 myapps.com # 追記
# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts
pingで念のため確認します。
$ ping myapps.com
PING myapps.com (127.0.0.1) 56(84) bytes of data.
64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.026 ms
64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.053 ms
64 bytes from localhost (127.0.0.1): icmp_seq=3 ttl=64 time=0.036 ms
64 bytes from localhost (127.0.0.1): icmp_seq=4 ttl=64 time=0.066 ms
64 bytes from localhost (127.0.0.1): icmp_seq=5 ttl=64 time=0.105 ms
OKですね。
Nginxの設定
Virtualhostの設定ファイルを作成します。
$ sudo vim /etc/nginx/sites-available/myapps.com
myapps.com
server {
listen 80;
server_name myapps.com;
access_log /var/log/nginx/myapps.com.access.log;
location / {
include uwsgi_params;
uwsgi_pass unix:/var/run/uwsgi/myapps.com.sock;
}
}
sites-enabled
にエイリアス置きます。
$ sudo ln -s /etc/nginx/sites-available/myapps.com /etc/nginx/sites-enabled/
socket置く場所の確保
$ sudo mkdir /var/run/uwsgi/
$ sudo chown www-data:www-data /var/run/uwsgi/
これでNginx周りの設定は完了。
uWSGIの設定
uWSGIの起動時設定ファイルを用意します。
$ cd ~
$ cd myapps
$ vim uwsgi.ini
uwsgi.ini
[uwsgi]
socket = /var/run/uwsgi/myapps.com.sock
pidfile = /var/run/uwsgi/myapps.com.pid
daemonize = /var/log/uwsgi/myapps.com.log
chdir = /home/vagrant/myapps/
master = 1
file = index.py
chmod-socket = 666
uid = www-data
gid = www-data
ログの保存先確保。
$ sudo mkdir /var/log/uwsgi
$ sudo chown www-data:www-data /var/log/uwsgi
起動
以下のとおりです。
$ sudo uwsgi uwsgi.ini
> [uWSGI] getting INI configuration from uwsgi.ini
起動確認。
$ wget http://myapps.com/
$ less index.html
> Hello World!
停止
PIDファイル作ってあるので、それを呼び出してquitするだけです。
sudo kill -QUIT `cat /var/run/uwsgi/myapps.com.pid`
以上です。
参考
- http://d.hatena.ne.jp/m-hiyama/20120312/1331513519
- http://www.ekesete.net/log/?p=6912
- http://manpages.ubuntu.com/manpages/gutsy/ja/man7/hier.7.html
- http://stackoverflow.com/questions/18562952/uwsgi-socket-not-created
- http://qiita.com/5t111111/items/e170fead91261621b054
- http://qiita.com/takashi/items/a9ff20ff5cdafee50124
- http://zafiel.wingall.com/archives/7513
- http://davidx.me/2014/10/17/configure-nginx-with-bottlepy-and-uwsgi/
- http://qiita.com/haburibe/items/d7ccb1f38ed368055809
- http://qiita.com/yasunori/items/64606e63b36b396cf695
- https://community.runabove.com/kb/en/development/how-to-run-bottle-uwsgi-nginx.html
- https://typist.geek.nz/files/2014/06/bottle+uwsgi/slides/#/6
- http://www.tengiz.net/bootlepy-nginx-uwsgi-python3-with-virtualenv-on-debian/
- http://uwsgi-docs.readthedocs.org/en/latest/Configuration.html
- http://uwsgi-docs.readthedocs.org/en/latest/tutorials/Django_and_nginx.html
- http://www.fisproject.jp/2015/01/nginx-uwsgi-flask/