在本文記錄了我在Ubuntu中部署Flask Web站點的過程, 其中包括用戶創建、代碼獲取、Python3環境的安裝、虛擬環境設置、uWSGI啟動程序設置,并將Nginx作為前端反向代理。希望對各位有所幫助。

建立一個Python Web程序專用賬戶
adduser haseovim /etc/sudoers #將haseo用戶加入導sudo用戶清單中sudo usermod -a -G www-data haseo
安裝Python3并配置程序運行環境
1.更新Ubuntu的軟件庫
sudo apt-get updatesudo apt-get -y upgradesudo apt-get install build-essential libssl-dev libffi-dev python3-dev #安裝一些必要的工具包
2.安裝python包管理工具
python3 -Vsudo apt-get install -y python3-pippip3 install virtualenv
配置Python 程序
1.創建程序目錄
mkdir -p /var/www/html/pricing-service
2.修改目錄權限
sudo chown haseo:haseo /var/www/html/pricing-service
3.創建一個SSH Key使得用戶可以同步GitHub的代碼
ssh-keygencat ~/.ssh/id_rsa.pub # 復制公鑰并增加到GitHub(https://github.com/settings/keys)
4.復制GitHub上的代碼
git clone git@xxx .
5.創建log目錄
mkdir log
6.創建虛擬目錄
pip3 install virtualenvpython3 -m virtualenv venv # 在pricing-service目錄下執行./venv/bin/pip install -r requirements.txt./venv/bin/pip install uwsgi
配置uwsgi
1.測試一下python直接運行程序是否可以訪問
vim ~/myproject/wsgi.pyfrom flask import Flaskapp = Flask(__name__)@app.route("/")def hello(): return "<h1 style='color:blue'>Hello There!</h1>"if __name__ == "__main__": app.run(host='0.0.0.0')python wsgi.py2.創建WSGI入口文件
vim ~/myproject/wsgi.pyfrom myproject import appif __name__ == "__main__": app.run()
3.測試uWSGI是否正常運行
uwsgi --socket 0.0.0.0:5000 --protocol=http -w wsgi:app
4.創建uWSGI配置文件
前面測試沒問題之后我們開始創建uWSGI配置文件
vim ~/myproject/wsgi.ini[uwsgi]module = wsgi:appmaster = trueprocesses = 5socket = socket.sockchmod-socket = 660vacuum = truedie-on-term = true
5.創建systemd文件
sudo vim /etc/systemd/system/price_service.service[Unit]Description=uWSGI instance to serve price_serviceAfter=network.target[Service]User=haseoGroup=www-dataWorkingDirectory=/var/www/html/pricing-serviceEnvironment="PATH=/var/www/html/pricing-service/venv/bin"ExecStart=/var/www/html/pricing-service/venv/bin/uwsgi --ini wsgi.ini[Install]WantedBy=multi-user.target
新聞熱點
疑難解答