將你的Python Web程序部署到Ubuntu服務器上


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

Python

建立一個Python Web程序專用賬戶

1
2
3
adduser haseo
vim /etc/sudoers #將haseo用戶加入導sudo用戶清單中
sudo usermod -a -G www-data haseo

安裝Python3並配置程序運行環境

1.更新Ubuntu的軟件庫

1
2
3
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.創建程序目錄

mkdir -p /var/www/html/pricing-service

2.修改目錄權限

sudo chown haseo:haseo /var/www/html/pricing-service

3.創建一個SSH Key使得用戶可以同步GitHub的代碼

ssh-keygen
cat ~/.ssh/id_rsa.pub # 復制公鑰並增加到GitHub(https://github.com/settings/keys)

4.復制GitHub上的代碼

git clone git@xxx .

5.創建log目錄

mkdir log

6.創建虛擬目錄

1
2
3
4
pip3 install virtualenv
python3 -m virtualenv venv # 在pricing-service目錄下執行
./venv/bin/pip install -r requirements.txt
./venv/bin/pip install uwsgi

配置uwsgi

1.測試一下python直接運行程序是否可以訪問

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.創建WSGI入口文件

vim ~/myproject/wsgi.py

from myproject import app

if __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:app

master = true
processes = 5

socket = socket.sock
chmod-socket = 660
vacuum = true

die-on-term = true

5.創建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服務

sudo systemctl start price_service
sudo systemctl enable price_service

配置Nginx

1.安裝nginx

apt-get install nginx

2.Nginx狀態查看及進程管理

1
2
3
4
5
6
systemctl status nginx
ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//' #獲取服務器的IP地址
sudo systemctl start nginx
sudo systemctl reload nginx
sudo systemctl disable nginx # 精致nginx在系統啟動的時候啟動
sudo systemctl enable nginx

3.配置Nginx站點

vim /etc/nginx/sites-available/default

server {
        listen 8080; #監聽IP
        real_ip_header X-Forwarded-For;
        set_real_ip_from 127.0.0.1; # 告訴Python程序是誰發送的請求
        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.軟鏈接導nginxsites-enabled目錄

sudo ln -s /etc/nginx/sites-available/default /etc/nginx/sites-enabled

5.測試配置

sudo nginx -t

6.重新啟動nginx大功告成

sudo systemctl restart nginx


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM