イロイロと手こずったので、自分用のメモです。
0. 環境
Ubuntu 16.04 LTS
Nginx 1.10.0
uWSGI 2.0.15
1. パッケージのインストール
$ sudo apt-get install nginx python-pip
$ sudo pip install uwsgi virtualenv
2. uWSGIの設定
2-1. systemd 用の設定ファイルを作成
$ sudo nano /etc/systemd/system/uwsgi.service
--
[Unit]
Description=uWSGI Emperor
After=syslog.target
[Service]
ExecStart=/usr/local/bin/uwsgi --ini /etc/uwsgi/emperor.ini
RuntimeDirectory=uwsgi
Restart=always
KillSignal=SIGQUIT
Type=notify
StandardError=syslog
NotifyAccess=all
[Install]
WantedBy=multi-user.target
Description=uWSGI Emperor
After=syslog.target
[Service]
ExecStart=/usr/local/bin/uwsgi --ini /etc/uwsgi/emperor.ini
RuntimeDirectory=uwsgi
Restart=always
KillSignal=SIGQUIT
Type=notify
StandardError=syslog
NotifyAccess=all
[Install]
WantedBy=multi-user.target
--
2-2. あれこれ、ディレクトリを作る
$ sudo mkdir -p /etc/uwsgi/vassals
$ sudo mkdir -p /var/log/uwsgi
2-3. uWSGI/emperor 用の設定ファイルを作成
$ sudo nano /etc/uwsgi/emperor.ini
--
[uwsgi]
emperor = /etc/uwsgi/vassals
uid = www-data
gid = www-data
logto = /var/log/uwsgi/emperor.log
emperor = /etc/uwsgi/vassals
uid = www-data
gid = www-data
logto = /var/log/uwsgi/emperor.log
--
3. アプリケーションの作成
ディレクトリ構成
~/web
├ venv/
│ └ Flask
└ project1/
├ main.py
$ cd
$ mkdir web
$ cd web
$ virtualenv venv
$ source ./venv/bin/activate
V pip install Flask
$ virtualenv venv
$ source ./venv/bin/activate
V pip install Flask
V mkdir project1
V cd project1
V nano main.py
--
from flask import Flask
app = Flask(__name__)
@app.route("/")
def index():
return "Hello Project1!"
if __name__ == "__main__":
app.run()
app = Flask(__name__)
@app.route("/")
def index():
return "Hello Project1!"
if __name__ == "__main__":
app.run()
--
V deactivate
4. アプリケーション用 uWSGI設定ファイル
$ sudo nano /etc/uwsgi/vassals/project1.ini
--
[uwsgi]
base = /home/<user>/web
#
socket = /tmp/%n.sock
home = %(base)/venv
chdir = %(base)/%n
manage-script-name = true
mount = /%n=main:app
py-autoreload = 1
base = /home/<user>/web
#
socket = /tmp/%n.sock
home = %(base)/venv
chdir = %(base)/%n
manage-script-name = true
mount = /%n=main:app
py-autoreload = 1
--
$ sudo systemctl enable uwsgi
5. Nginxの設定
5-1. デフォルトの設定を削除
$ sudo rm /etc/nginx/sites-enabled/default
5-2. アプリケーション用 Nginx設定ファイルを作成
$ sudo nano /etc/nginx/conf.d/project1.conf
--
server {
listen 80;
server_name project1;
location /project1 {
uwsgi_pass unix:/tmp/project1.sock;
include uwsgi_params;
}
}
--
server {
listen 80;
server_name project1;
location /project1 {
uwsgi_pass unix:/tmp/project1.sock;
include uwsgi_params;
}
}
--
6. 再起動
$ sudo reboot
[参考]
[おまけ]
$ sudo systemctl daemon-reload
$ sudo systemctl enable uwsgi
$ sudo systemctl start uwsgi
$ sudo systemctl enable uwsgi
$ sudo systemctl start uwsgi
$ sudo systemctl stop uwsgi
$ sudo systemctl status uwsgi
$ sudo systemctl status uwsgi