在本文记录了我在Ubuntu中部署Flask Web站点的过程, 其中包括用户创建、代码获取、Python3环境的安装、虚拟环境设置、uWSGI启动程序设置,并将Nginx作为前端反向代理。希望对各位有所帮助。
建立一个Python Web程序专用账户
adduser haseovim /etc/sudoers #将haseo用户加入导sudo用户清单中sudo usermod -a -G www-data haseo
安装Python3并配置程序运行环境
1.更新Ubuntu的软件库
sudo apt-get updatesudo apt-get -y upgradesudo apt-get install build-essential libssl-dev libffi-dev python3-dev #安装一些必要的工具包
2.安装python包管理工具
python3 -Vsudo apt-get install -y python3-pippip3 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-keygencat ~/.ssh/id_rsa.pub # 复制公钥并增加到GitHub(https://github.com/settings/keys)
4.复制GitHub上的代码
git clone git@xxx .
5.创建log目录
mkdir log
6.创建虚拟目录
pip3 install virtualenvpython3 -m virtualenv venv # 在pricing-service目录下执行./venv/bin/pip install -r requirements.txt./venv/bin/pip install uwsgi
配置uwsgi
1.测试一下python直接运行程序是否可以访问
vim ~/myproject/wsgi.pyfrom flask import Flaskapp = 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.pyfrom myproject import appif __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:appmaster = trueprocesses = 5socket = socket.sockchmod-socket = 660vacuum = truedie-on-term = true
5.创建systemd文件
sudo vim /etc/systemd/system/price_service.service[Unit]Description=uWSGI instance to serve price_serviceAfter=network.target[Service]User=haseoGroup=www-dataWorkingDirectory=/var/www/html/pricing-serviceEnvironment="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
新闻热点
疑难解答