Python Web程序部署到Ubuntu服务器上的方法

yipeiwu_com6年前服务器

在本文记录了我在Ubuntu中部署Flask Web站点的过程, 其中包括用户创建、代码获取、Python3环境的安装、虚拟环境设置、uWSGI启动程序设置,并将Nginx作为前端反向代理。希望对各位有所帮助。

建立一个Python Web程序专用账户

adduser haseo
vim /etc/sudoers #将haseo用户加入导sudo用户清单中
sudo usermod -a -G www-data haseo

安装Python3并配置程序运行环境

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.创建程序目录

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.创建虚拟目录

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状态查看及进程管理

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.软链接导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

总结

以上所述是小编给大家介绍的Python Web程序部署到Ubuntu服务器上的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对【听图阁-专注于Python设计】网站的支持!

相关文章

Python搭建HTTP服务器和FTP服务器

Python搭建HTTP服务器和FTP服务器

本教程为大家分享了Python搭建HTTP服务器和FTP服务器的具体步骤,供大家参考,具体内容如下 默认安装版本为pytho2.7 http服务器搭建教程: 进入要开放访问的目录下,执行...

Python代码使用 Pyftpdlib实现FTP服务器功能

Python代码使用 Pyftpdlib实现FTP服务器功能

当你想快速共享一个目录的时候,这是特别有用的,只需要1行代码即可实现。 FTP 服务器,在此之前我都是使用Linux的vsftpd软件包来搭建FTP服务器的,现在发现了利用pyftpdl...

开启CURL扩展,让服务器支持PHP curl函数(远程采集)

curl()、file_get_contents()、snoopy.class.php这三个远程页面抓取或采集中用到的工具,默迹还是侵向于用snoopy.class.php,因为他效率比...

Windows服务器中PHP如何安装redis扩展

Windows服务器中PHP如何安装redis扩展

一、总结 一句话总结:下载扩展的dll,放入指定文件夹(php对应的扩展的目录php/ext),在配置文件php.ini中注册dll 尽量不要选择最新版php和redis扩展,下载时文件...

浅析使用Python搭建http服务器

浅析使用Python搭建http服务器

David Wheeler有一句名言:“计算机科学中的任何问题,都可以通过加上另一层间接的中间层解决。”为了提高Python网络服务的可移植性,Python社区在PEP 333中提出了...