使用flask來建立LINE-bot,但不到一天就連不上了,都必須重開flask的應用程式
找了資料後發現,必須架設WSGI server(gunicorn)轉發來自使用者的要求給Web應用程式(flask)
架設過程如下:
#install安裝nginx和gunicorn
sudo apt-get install nginx
pip install gunicorn
----------------------------------------
#編輯nginx的設定檔 vi /etc/nginx/sites-available/default
#加入或修改以下資料: #Ref:https://www.gaia.net/tc/news_detail/2/127/nginx-ssl
server {
listen 5001; #要監聽的port
server_name example.org; #網站的網域名稱
ssl on; #啟動ssl,若不使用,則可以把這3行拿掉
ssl_certificate /etc/XXXXX/fullchain.pem; #憑證
ssl_certificate_key /etc/XXXXX/privkey.pem; #private key私鑰
location / {
proxy_pass http://127.0.0.1:5002; # 指向 gunicorn host 的服務位址
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
----------------------------------------
#sudo service nginx restart 重新啟動nginx
sudo service nginx restart
----------------------------------------
#啟動flask_app.py
nohup gunicorn -w 1 -b 127.0.0.1:5002 flask_app:app
-----------------------------------------
#Ref:https://jeffwen0105.com/1759-2/
#Ref:https://www.maxlist.xyz/2020/05/06/flask-wsgi-nginx/