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

建立一個(gè)Python Web程序?qū)S觅~戶
adduser haseovim /etc/sudoers #將haseo用戶加入導(dǎo)sudo用戶清單中sudo usermod -a -G www-data haseo
安裝Python3并配置程序運(yùn)行環(huán)境
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.創(chuàng)建程序目錄
mkdir -p /var/www/html/pricing-service
2.修改目錄權(quán)限
sudo chown haseo:haseo /var/www/html/pricing-service
3.創(chuàng)建一個(gè)SSH Key使得用戶可以同步GitHub的代碼
ssh-keygencat ~/.ssh/id_rsa.pub # 復(fù)制公鑰并增加到GitHub(https://github.com/settings/keys)
4.復(fù)制GitHub上的代碼
git clone git@xxx .
5.創(chuàng)建log目錄
mkdir log
6.創(chuàng)建虛擬目錄
pip3 install virtualenvpython3 -m virtualenv venv # 在pricing-service目錄下執(zhí)行./venv/bin/pip install -r requirements.txt./venv/bin/pip install uwsgi
配置uwsgi
1.測(cè)試一下python直接運(yùn)行程序是否可以訪問
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.創(chuàng)建WSGI入口文件
vim ~/myproject/wsgi.pyfrom myproject import appif __name__ == "__main__": app.run()
3.測(cè)試uWSGI是否正常運(yùn)行
uwsgi --socket 0.0.0.0:5000 --protocol=http -w wsgi:app
4.創(chuàng)建uWSGI配置文件
前面測(cè)試沒問題之后我們開始創(chuàng)建uWSGI配置文件
vim ~/myproject/wsgi.ini[uwsgi]module = wsgi:appmaster = trueprocesses = 5socket = socket.sockchmod-socket = 660vacuum = truedie-on-term = true
5.創(chuàng)建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
6.啟動(dòng)并啟用wsgi服務(wù)
sudo systemctl start price_servicesudo systemctl enable price_service
配置Nginx
1.安裝nginx
apt-get install nginx
2.Nginx狀態(tài)查看及進(jìn)程管理
systemctl status nginxip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's///.*$//' #獲取服務(wù)器的IP地址sudo systemctl start nginxsudo systemctl reload nginxsudo systemctl disable nginx # 精致nginx在系統(tǒng)啟動(dòng)的時(shí)候啟動(dòng)sudo systemctl enable nginx3.配置Nginx站點(diǎn)
vim /etc/nginx/sites-available/defaultserver { listen 8080; #監(jiān)聽IP real_ip_header X-Forwarded-For; set_real_ip_from 127.0.0.1; # 告訴Python程序是誰發(fā)送的請(qǐng)求 server_name localhost; location / { # 用戶訪問的根目錄比如 http://www.bihell.com/ include uwsgi_params; # Flask程序需要uwsgi解析 uwsgi_pass unix:/var/www/html/pricing-service/socket.sock; #uwsgi通過這個(gè)文件傳遞信息 uwsgi_modifier1 30; } # 404錯(cuò)誤頁面配置,下同 error_page 404 /404.html; location = /404.html { root /usr/share/nginx/html; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; }}4.軟鏈接導(dǎo)nginx sites-enabled 目錄
sudo ln -s /etc/nginx/sites-available/default /etc/nginx/sites-enabled
5.測(cè)試配置
sudo nginx -t
6.重新啟動(dòng)nginx大功告成
sudo systemctl restart nginx
總結(jié)
以上所述是小編給大家介紹的Python Web程序部署到Ubuntu服務(wù)器上的方法,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)VEVB武林網(wǎng)網(wǎng)站的支持!
新聞熱點(diǎn)
疑難解答
圖片精選