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

建立一個Python Web程序?qū)S觅~戶
adduser haseo vim /etc/sudoers #將haseo用戶加入導(dǎo)sudo用戶清單中 sudo usermod -a -G www-data haseo
安裝Python3并配置程序運(yùn)行環(huán)境
1.更新Ubuntu的軟件庫
sudo apt-get update sudo apt-get -y upgrade sudo apt-get install build-essential libssl-dev libffi-dev python3-dev #安裝一些必要的工具包
2.安裝python包管理工具
python3 -V sudo apt-get install -y python3-pip pip3 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)建一個SSH Key使得用戶可以同步GitHub的代碼
ssh-keygen cat ~/.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 virtualenv python3 -m virtualenv venv # 在pricing-service目錄下執(zhí)行 ./venv/bin/pip install -r requirements.txt ./venv/bin/pip install uwsgi
配置uwsgi
1.測試一下python直接運(yùn)行程序是否可以訪問
vim ~/myproject/wsgi.py
from flask import Flask
app = 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.py
2.創(chuàng)建WSGI入口文件
vim ~/myproject/wsgi.py from myproject import app if __name__ == "__main__": app.run()
3.測試uWSGI是否正常運(yùn)行
uwsgi --socket 0.0.0.0:5000 --protocol=http -w wsgi:app
4.創(chuàng)建uWSGI配置文件
前面測試沒問題之后我們開始創(chuàng)建uWSGI配置文件
vim ~/myproject/wsgi.ini [uwsgi] module = wsgi:app master = true processes = 5 socket = socket.sock chmod-socket = 660 vacuum = true die-on-term = true
5.創(chuàng)建systemd文件
sudo vim /etc/systemd/system/price_service.service [Unit] Description=uWSGI instance to serve price_service After=network.target [Service] User=haseo Group=www-data WorkingDirectory=/var/www/html/pricing-service Environment="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.啟動并啟用wsgi服務(wù)
sudo systemctl start price_service sudo systemctl enable price_service
配置Nginx
1.安裝nginx
apt-get install nginx
2.Nginx狀態(tài)查看及進(jìn)程管理
systemctl status nginx
ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//' #獲取服務(wù)器的IP地址
sudo systemctl start nginx
sudo systemctl reload nginx
sudo systemctl disable nginx # 精致nginx在系統(tǒng)啟動的時候啟動
sudo systemctl enable nginx
3.配置Nginx站點(diǎn)
vim /etc/nginx/sites-available/default
server {
listen 8080; #監(jiān)聽IP
real_ip_header X-Forwarded-For;
set_real_ip_from 127.0.0.1; # 告訴Python程序是誰發(fā)送的請求
server_name localhost;
location / { # 用戶訪問的根目錄比如 http://www.bihell.com/
include uwsgi_params; # Flask程序需要uwsgi解析
uwsgi_pass unix:/var/www/html/pricing-service/socket.sock; #uwsgi通過這個文件傳遞信息
uwsgi_modifier1 30;
}
# 404錯誤頁面配置,下同
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.測試配置
sudo nginx -t
6.重新啟動nginx大功告成
sudo systemctl restart nginx
總結(jié)
以上所述是小編給大家介紹的Python Web程序部署到Ubuntu服務(wù)器上的方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
Python使用unicodedata實(shí)現(xiàn)字符串標(biāo)準(zhǔn)化
這篇文章主要來和大家聊一聊 Python 的一個內(nèi)置模塊:unicodedata,它是專門用來處理 unicode 字符串的,下面就一起來看看它的用法吧2023-06-06
一文帶你解密Python迭代器的實(shí)現(xiàn)原理
這篇文章主要為大家詳細(xì)介紹了Python中迭代器的實(shí)現(xiàn)原理,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)Python有一定的幫助,需要的可以參考一下2022-12-12
python基礎(chǔ)教程之基本內(nèi)置數(shù)據(jù)類型介紹
在Python程序中,每個數(shù)據(jù)都是對像,每個對像都有自己的一個類型。不同類型有不同的操作方法,使用內(nèi)置數(shù)據(jù)類型獨(dú)有的操作方法,可以更快的完成很多工作2014-02-02
學(xué)習(xí)createTrackbar的使用方法及步驟
這篇文章主要為大家介紹了學(xué)習(xí)createTrackbar的使用方法及步驟,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2021-10-10
TensorFlow tf.nn.softmax_cross_entropy_with_logits的用法
這篇文章主要介紹了TensorFlow tf.nn.softmax_cross_entropy_with_logits的用法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
Django REST Swagger實(shí)現(xiàn)指定api參數(shù)
這篇文章主要介紹了Django REST Swagger實(shí)現(xiàn)指定api參數(shù),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07

