在本文记录了我在Ubuntu中部署Flask Web站点的过程, 其中包括用户创建、代码获取、Python3环境的安装、虚拟环境设置、uWSGI启动程序设置,并将Nginx作为前端反向代理。希望对各位有所帮助。
建立一个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包管理工具
1
2
3
|
python3 - V
sudo apt - get install - y python3 - pip
pip3 install virtualenv
|
配置Python 程序
1.创建程序目录
1
|
mkdir - p / var / www / html / pricing - service
|
2.修改目录权限
1
|
sudo chown haseo:haseo / var / www / html / pricing - service
|
3.创建一个SSH Key使得用户可以同步GitHub的代码
1
2
|
ssh - keygen
cat ~ / .ssh / id_rsa.pub # 复制公钥并增加到GitHub(https://github.com/settings/keys)
|
4.复制GitHub上的代码
1
|
git clone git@xxx .
|
5.创建log目录
1
|
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直接运行程序是否可以访问
1
2
3
4
5
6
7
8
9
|
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入口文件
1
2
3
4
|
vim ~ / myproject / wsgi.py
from myproject import app
if __name__ = = "__main__" :
app.run()
|
3.测试uWSGI是否正常运行
1
|
uwsgi - - socket 0.0 . 0.0 : 5000 - - protocol = http - w wsgi:app
|
4.创建uWSGI配置文件
前面测试没问题之后我们开始创建uWSGI配置文件
1
2
3
4
5
6
7
8
9
10
|
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文件
1
2
3
4
5
6
7
8
9
10
11
12
13
|
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服务
1
2
|
sudo systemctl start price_service
sudo systemctl enable price_service
|
配置Nginx
1.安装nginx
1
|
apt - get install nginx
|
2.Nginx状态查看及进程管理
1
2
3
4
5
6
|
systemctl status nginx
sudo systemctl start nginx
sudo systemctl reload nginx
sudo systemctl disable nginx # 精致nginx在系统启动的时候启动
sudo systemctl enable nginx
|
3.配置Nginx站点
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
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 目录
1
|
sudo ln - s / etc / nginx / sites - available / default / etc / nginx / sites - enabled
|
5.测试配置
1
|
sudo nginx - t
|
6.重新启动nginx大功告成
1
|
sudo systemctl restart nginx
|
总结
以上所述是小编给大家介绍的Python Web程序部署到Ubuntu服务器上的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!
原文链接:http://www.cnblogs.com/haseo/p/8458222.html