前言
又有段時間沒寫博客了,最近一直在寫外包項目,都沒啥空余時間。這幾天花了不少時間做項目部署,也看了不少教程,這里就記錄下整個過程,也方便以后要做類似部署的時候不用再查來查去了。
flask + uWSGI
看到網上的教程都是清一色的使用 virtualenv 來創建虛擬環境,但我更傾向於使用 anaconda 來管理虛擬環境,關於 Ubuntu 中 anaconda 的安裝,可以參考這篇博客,安裝完成后,建議在 anaconda 中添加清華源
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
# 以上兩條是Anaconda官方庫的鏡像
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
# 以上是Anaconda第三方庫 Conda Forge的鏡像
# for linux
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
# for legacy win-64
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/peterjc123/
以上兩條是Pytorch的Anaconda第三方鏡像
conda config --set show_channel_urls yes
可以使用 conda info
命令來查看是否添加成功。
接着,我們創建一個虛擬環境
conda create -n "環境名稱" python=python版本
然后通過命令 source activate 環境名稱
進入虛擬環境
接着開始安裝 flask 與 uWSGI
conda install flask
conda install uwsgi
這里我就借用一下別人博客里清一色的 Demo, 創建文件 myproject.py
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "<h1 style='color:blue'>Hello There!</h1>"
manage.py
from myproject import app
if __name__ == "__main__":
app.run()
輸入 python manage.py
運行,然后瀏覽器輸入 http://localhost:5000/ 即可看到結果
nginx
sudo apt-get update
sudo apt-get install nginx
接着,在 Nginx 的 sites-available
目錄中創建一個新的服務器塊配置文件
sudo nano /etc/nginx/sites-available/myproject
這個是我最開始使用的
server {
listen 80;
server_name server_domain_or_IP;
location / {
include uwsgi_params; # 導入uwsgi配置
uwsgi_pass 127.0.0.1:8000; # 轉發端口,需要和uwsgi配置當中的監聽端口一致
}
}
完成后,保存文件,然后輸入 sudo ln -s /etc/nginx/sites-available/myproject /etc/nginx/sites-enabled
將配置文件鏈接到 sites-enabled
目錄
然后測試一下是否有語法問題
sudo nginx -t
若無問題,則重啟 nginx
sudo systemctl restart nginx
配置 uWSGI
最后開始 uWSGI 相關文件的配置,看了網上的教程,感覺都很麻煩,通過同學了解到一個十分簡單的方法,在我們之前創建的 manage.py
所在目錄下,創建 uwsgi.ini
和 uwsgi.pid
uwsgi.ini
[uwsgi]
module = manage:app
master = true
chdir = .
socket = 127.0.0.1:8000
chmod-socket = 660
vacuum = true
wsgi-file = manage.py # flask程序的啟動文件
pidfile=./uwsgi.pid
limit-as = 512
http-timeout = 300
socket-timeout = 300
harakiri = 300
uwsgi.pid
20830
接着,通過以下命令,即可啟動 uwsgi
注:需在創建的虛擬環境中輸入
nohup uwsgi uwsgi.ini > wsgi.log 2>&1 &
結束命令
uwsgi --stop uwsgi.pid
重啟命令
uwsgi --reload uwsgi.pid
然后,我們到本地瀏覽器中輸入 http://server_domain_or_IP
即可看到結果
這里順便再講講 nginx 配置 https
首先要先申請 SSL,申請成功后會用申請的域名為包名發送給你,打開后有
將 Nginx 目錄下的兩個文件上傳到 /etc/nginx
目錄下
然后將之前的 nginx 服務的配置文件改為
server {
listen 443;
server_name xxx; # 你的域名
ssl on;
ssl_certificate /xxx.crt;# 改成你的證書的名字
ssl_certificate_key /xxx.key;# 你的證書的名字
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
location / {
proxy_request_buffering off;
proxy_buffering off;
proxy_http_version 1.1;
proxy_set_header Connection "";
include uwsgi_params; # 導入uwsgi配置
uwsgi_pass 127.0.0.1:8000; # 轉發端口,需要和uwsgi配置當中的監聽端口一致
}
}
server {
listen 80;
server_name xxx; # 你的域名
rewrite ^(.*)$ https://$host$1 permanent;#把http的域名請求轉成https
client_max_body_size 75M;
}
剩下的步驟與上面 nginx 相同。這樣,便可通過 https 進行訪問了。
小節
這還是我頭一次做環境部署,花了不少時間,在部署成功的那一刻,真的是十分開心。一開始是准備在 Windows 服務器上進行項目部署,搗鼓了兩天沒弄出來,就直接放棄然后轉用 linux 服務器,半天多的時間就搞定了。然后今天早上搞 mysql 的遠程連接也搞了一早上,服務器中防火牆、mysql權限什么全都做了,就是連不上,最后發現是因為安全組 3306 端口沒打開,白白多花幾個小時。。。還是太菜了