前言:
在本地開發了一個flask項目,如何將其部署到互聯網上?
目前有2種比較常見的方法:
- Ningx+uwsgi
- Ningx+Gunicorn
對於第一種,因爲服務器上對應uwsgi版本的不同,或者環境的差異,作者花了很長時間都沒調試成功。
使用第一種,可能會遇到的坑
- 使用python3但是安裝了python2版本的uwsgi
- uwsgi默認鏈接服務器上的python2.7
- uwsgi安裝後不能讀入
轉念一想,uwsgi這麼坑,有沒有可以替代它的。於是發現了Ningx+Gunicorn來部署Flask項目。可以說相當方便了。
之後會考慮上傳到git實現自動部署,本地修改push到git上自動更新。
具體步驟
1.你需要擁有一個服務器,作者是通過SecureCRT遠程連接上服務器的。
2.服務器上是默認有python2.7 和 python3.5的。但是沒有pip
所以我們需要安裝python3.5的pip
sudo apt-get install python3.5-dev python3.5-pip python3.5-virtualenv
上面如果安裝不成功,百度如何安裝python3.5 pip
安裝好後使用:
pip3 install virtualenv
3.構建項目獨立環境(這個步驟理解爲將這個項目所需的一切(python)相關基礎內容,都放到一個單獨的容器中去,這樣改變這個容器中的
python相關配置,不會影響到服務器。)
將項目傳入服務器:
假設目錄結構如下
/root/flaskweb/app.py
- 主要使用Ningx+Gunicorn配置flaskweb那麼app.py需要做如下配置:
if __name__ == '__main__':
from werkzeug.contrib.fixers import ProxyFix
app.wsgi_app = ProxyFix(app.wsgi_app)
app.run()
進入項目目錄:
cd /root/flaskweb
創建環境:(作者用的是anaconda的python,如你用的是自帶python3.5 這裏路徑改爲你python3.5的路徑)
不清楚路徑可以用命令 whereis python3.5
virtualenv -p /root/anaconda3/bin/python3.6 venv
進入環境:
. venv/bin/activate
安裝所需的python模塊:
venv/bin/pip install flask
venv/bin/pip install pymysql
venv/bin/pip install gunicorn
測試Gunicorn:
gunicorn -w 4 -b 127.0.0.1:5000 app:app
其中
- -w 4 表示4個進程
- -b 127.0.0.1:5000 表示flask應用使用5000端口,開放5000用於ningx連接
- app:app 前者代表啓動程序文件名, 後者爲實例化對象命名即 app = Flask(__name__)
如果程序啓動了,沒有報錯信息代表配置成功。ctrl+c退出。
4.nginx
1.安裝
sudo apt-get install nginx
2.配置參數:
命令行輸入 如果報錯 查詢下你的nginx安裝目錄
echo "server {
listen 8080; #你想服務器的端口
server_name 你的服務器地址; #例如 198.198.22.22
location / {
proxy_pass http://127.0.0.1:5000; #這個是Gunicorn與Ningx通信的端口。和Gunicorn的配置相同
access_log /root/flaskweb/access.log;
error_log /root/flaskweb/error.log;
}
}" > /etc/nginx/conf.d/default.conf
啓動服務:
建議從新鏈接一下服務器,按如此步驟運行:
cd /root/flaskweb
. venv/bin/activate
gunicorn -w 4 -b 127.0.0.1:5000 app:app
啓動後 開啓另一個服務器鏈接,輸入:
sudo service nginx start
這樣你便可以在互聯網上 ‘你的服務器地址’:8080 訪問到你的flask項目了。
後記
nginx命令
安裝: sudo apt-get install nginx
啓動:sudo service nginx start
重啓:sudo service nginx restart
停止:sudo service nginx stop
測試:sudo service nginx configtest
查看端口使用情況:
使用命令:ps -aux | grep 端口
使用命令:netstat -apn
更新ubuntu官方源文件:
首先得進入 源文件的目錄下。--百度
如果要換成163.或者阿里巴巴的,將echo "xx" xx內容替換成163源信息即可。
echo "#deb cdrom:[Ubuntu 16.04.1 LTS _Xenial Xerus_ - Release amd64 (20160719)]/ xenial main restricted
# See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to
# newer versions of the distribution.
deb http://cn.archive.ubuntu.com/ubuntu/ xenial main restricted
# deb-src http://cn.archive.ubuntu.com/ubuntu/ xenial main restricted
## Major bug fix updates produced after the final release of the
## distribution.
deb http://cn.archive.ubuntu.com/ubuntu/ xenial-updates main restricted
# deb-src http://cn.archive.ubuntu.com/ubuntu/ xenial-updates main restricted
## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team, and may not be under a free licence. Please satisfy yourself as to
## your rights to use the software. Also, please note that software in
## universe WILL NOT receive any review or updates from the Ubuntu security
## team.
deb http://cn.archive.ubuntu.com/ubuntu/ xenial universe
# deb-src http://cn.archive.ubuntu.com/ubuntu/ xenial universe
deb http://cn.archive.ubuntu.com/ubuntu/ xenial-updates universe
# deb-src http://cn.archive.ubuntu.com/ubuntu/ xenial-updates universe" > sources.list