參考
https://www.oschina.net/translate/serving-flask-with-nginx-on-ubuntu
http://www.linuxidc.com/Linux/2016-07/133064.html
http://www.jianshu.com/p/1278c7ee0cc7
http://developer.51cto.com/art/201010/229615.htm
http://uwsgi-docs.readthedocs.io/en/latest/WSGIquickstart.html
環境說明
- OS: debian 4.9.18-1 (2017-03-30) i686
- Python: python 3.5.3
- venv: pyvenv-3.5
- Nginx version: nginx/1.10.3
- uWSGI: 2.0.14-debian
- Supervisor: 3.3.1-1
項目目錄
aircleaner
├── app
│ ├── api
│ ├── api_1_0
│ ├── main
│ ├── setting
│ ├── static
│ ├── templates
│ └── users
├── migrations
├── tests
├── tools
├── uWSGI
└── venv
├── bin
├── include
├── lib
└── share
開始
1. 安裝所需軟件
全部默認用 root 安裝
$ apt install build-essential python-dev
$ apt install virtualenv
$ apt install nginx
$ apt install uwsgi-plugin-python3
$ apt install supervisor
$ pip install uwsgi
2. 配置 Nginx
進入 /etc/nginx/sites-enabled/
文件夾 特別注意不是 sites-available
,也可以在 sites-available
文件夾下創建配置文件,再軟連接到 site-enabled
文件夾下,在 /etc/nginx/conf.d/
中也軟連接一份
備份默認配置文件 default
$ cd /etc/nginx/sites-enabled
$ mv default ../sites_enabled_default.bak
寫入配置文件:
server {
listen 80;
server_name localhost;
charset utf-8;
client_max_body_size 75M;
location / {
try_files $uri @aircleaner;
}
location @aircleaner{
include uwsgi_params;
uwsgi_pass unix:/root/aircleaner/uWSGI/uwsgi.sock; #指定socket文件
#uwsgi_pass 127.0.0.1:8080; #指定url + 地址
}
location /static {
alias /root/aircleaner/app/static; #指定靜態文件地址
}
}
修改好配置之後測試配置文件
nginx -t
重啓 Nginx
/etc/init.d/nginx restart
3. 配置 uWSGI
在項目文件 uwsgi 文件夾下 新建配置文件 uwsgi.ini (支持 xml, ini, json)
在配置文件中寫下以下內容:
[uwsgi]
#application's base folder
#base = /root/aircleaner
chdir = /root/aircleaner #指定項目的目錄(關鍵)
#plugin
plugin = python35 #調用python35 插件(關鍵)
#misc
master = True
py-autoreload = True
enable-threads = True
#python module to import
app = app
wsgi-file = /root/aircleaner/manage.py #指定到運行的py文件上(關鍵)
virtualenv = /root/aircleaner/venv #指定虛擬環境目錄(關鍵)
#socket file's location
socket = /root/aircleaner/uWSGI/uwsgi.sock #將socket輸出到文件
#socket = 127.0.0.1:8080 #也可以輸出到地址+端口
#permissions for the socket file
chmod-socket = 666 #改變權限
#the variable that holds a flask application inside the module imported at line #6
callable = app #調用 manage.py 中的 app
#location of log files
#logto = /root/aircleaner/uWSGI/%n.log #輸出日誌到文件
查看可用的 uWSGI 插件
update-alternatives --list uwsgi
啓動 uWSGI
uwsgi uwsgi.ini
4. 進程監控
使用 supervisor
supervisord
爲服務端supervisorctl
爲客戶端,用來啓動某一進程
對默認配置文件備份
$ echo_supervisord_config > /etc/supervisor/supervisord.conf.default
修改 /etc/supervisor/supervisord.conf
,在最下面加入監控程序的配置
[program:aircleaner]
command=/usr/bin/uwsgi /root/aircleaner/uWSGI/uwsgi.ini # 跟手動啓動的命令一樣
stopsignal=QUIT
autostart=true
autorestart=true
user=root
stdout_logfile=/root/aircleaner/uWSGI/supervisor.log # 運行日誌
stderr_logfile=/root/aircleaner/uWSGI/supervisor_err.log # 錯誤日誌
重啓 supervisord
$ supervisorctl reload
啓動 aircleaner
進程
$ supervisorctl start aircleaner
排錯
$ supervisorctl status aircleaner
使用以上命令可以返回我們進程的狀態信息
測試:使用 killall 殺死 uwsgi 進程, 然後再查找
$ killall uwsgi
$ ps aux | grep uwsgi
發現 uwsgi
自己重新啓動了,目的達成