社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

将你的Python Web程序部署到Ubuntu服务器上

一个普普通通简简单单平平凡凡的神 • 8 年前 • 294 次点击  

在本文记录了我在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

今天看啥 - 高品质阅读平台
本文地址:http://www.jintiankansha.me/t/IynoQeMhGj
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/7529