(1)應用環境
mkdir myapp
cd myapp
新建run.py 腳本
from flask import Flask
app = Flask(__name__)
app.route('/')
def index():
return 'hello world':q
if __name__ =='__main__':
app.run()
(2)gunicorn配置
pip install gunicorn
gunicorn -D -w 3 -b 0.0.0.0:8000 run:app
D 表示後臺運行
w 表示有3 個 工做線程(感受有些相似 nginx 的 master-worker 模型)
b 指定ip 和端口
這裏採用本機訪問, 主要是爲了使用nginx 進行代理, 方便管理 ,能夠用0.0.0.0來讓其餘主機訪問,可是記得要看後面的防火牆端口,記得開啓這裏的8000
run表存放 寫着全局變量 app 的那個工程文件
在咱們的這個工程中, 即包含 init.py 的那個文件
(3)supervisor配置
pip install supervisor
- 進到工程目錄,在項目目錄添加supervisor的配置文件,
echo_supervisord_conf > supervisord.conf
vi supervisord.conf
添加信息:
[program:myapp] directory =/clh/myapps command =/clh/myapps/myenv/bin/gunicorn -w 4 -b 0.0.0.0:8000 run:app
|
myapp 本身起的項目名
directory運行項目所在的工程文件
command ,gunicorn 的啓動命令
supervisord -c supervisor.conf
supervisorctl -c supervisor.conf status
supervisord -c supervisor.conf 經過配置文件啓動supervisor,注意它和其餘命令不同 supervisorctl -c supervisor.conf status 察看supervisor的狀態 supervisorctl -c supervisor.conf reload 從新載入 配置文件 supervisorctl -c supervisor.conf start [all]|[appname] 啓動指定/全部 supervisor管理的程序進程 supervisorctl -c supervisor.conf stop [all]|[appname] 關閉指定/全部 supervisor管理的程序進程
|
(4)nginx配置
apt-get install nginx
ps aux|grep nginx
sudo cp /etc/nginx/sites-available/default default.bak
sudo vi /etc/nginx/sites-available/default
Ctrl + O 保存
Ctrl + X 退出
把配置改成
server { listen 80; server_name 115.159.146.108; # 這是HOST機器的外部域名,用地址也行 location / { proxy_pass http://0.0.0.0:8000; # 這裏是指向 gunicorn host 的服務地址 } }
這樣啓動以後80端口就能夠轉發到8000端口了
nginx -t
若出現
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
則配置成功
殺nginx進程
重啓nginx
sudo service nginx restart
在已經運行了Gunicorn的前提下,在瀏覽器訪問115.159.146.108就會出現Hello World!了