Flask开发(三)ubuntu16.04生产环境部署
(本文主要内容参考 Deploy flask app with Nginx using Gunicorn | by Tasnuva Zaman | FAUN Publication)
1 所需组件
- Python3.7
- Virtualenv
- Flask
- Gunicorn
- Nginx
组件关系图如下:
2 安装组件
python3.7
安装方法参考 Flask开发(二)部署到ubuntu16.04 - AdamTang - 博客园 (cnblogs.com)
安装成功的标志如下:
~$ python3 Python 3.7.10 (default, Feb 20 2021, 21:21:24)
Virtualenv
sudo python3 -m pip install virtualenv -i https://pypi.tuna.tsinghua.edu.cn/simple /opt/http$ virtualenv flaskvirenv 在/opt/http目录下建立虚拟环境 /opt/http$ source flaskvirenv/bin/activate
此时应处于python3.7虚拟环境中
(flaskvirenv) /opt/http$ python3 Python 3.7.10 (default, Feb 20 2021, 21:21:24)
Flask & Gunicorn
pip install gunicorn flask -i https://pypi.tuna.tsinghua.edu.cn/simple 虚拟环境中执行
pip install flask_restful flask_httpauth -i https://pypi.tuna.tsinghua.edu.cn/simple
3 初步测试gunicorn
(flaskvirenv) /opt/http$ gunicorn -t 180 -w 4 -b 0.0.0.0:8080 app:app
[2021-11-25 17:09:10 +0800] [7038] [INFO] Starting gunicorn 20.1.0
[2021-11-25 17:09:10 +0800] [7038] [INFO] Listening at: http://0.0.0.0:8080 (7038)
[2021-11-25 17:09:10 +0800] [7038] [INFO] Using worker: sync
[2021-11-25 17:09:10 +0800] [7047] [INFO] Booting worker with pid: 7047
[2021-11-25 17:09:10 +0800] [7048] [INFO] Booting worker with pid: 7048
[2021-11-25 17:09:10 +0800] [7049] [INFO] Booting worker with pid: 7049
[2021-11-25 17:09:10 +0800] [7050] [INFO] Booting worker with pid: 7050
-t 180 设定单次请求服务器的最长响应时间为180s,否则会重启线程
-w 4 启动4个工作线程
-b 绑定的IP:port
app:app 启动app.py中的app对象应用
4 封装app.py
建立wsgi.py
from app import app if __name__ == "__main__": app.run()
目录结构如下:
├── app.py
├── flaskvirenv
└── wsgi.py
测试:
gunicorn -t 180 -w 4 -b 0.0.0.0:8080 wsgi:app
5 系统服务封装
sudo vim /etc/systemd/system/app.service
[Unit] # specifies metadata and dependencies Description=Gunicorn instance to serve myproject After=network.target # tells the init system to only start this after the networking target has been reached # We will give our regular user account ownership of the process since it owns all of the relevant files [Service] # Service specify the user and group under which our process will run. User=username 此处替换成自己的用户名 # give group ownership to the www-data group so that Nginx can communicate easily with the Gunicorn processes. Group=www-data # We'll then map out the working directory and set the PATH environmental variable so that the init system knows where our the executables for the process are located (within our virtual environment). WorkingDirectory=/opt/http Environment="PATH=/opt/http/flaskvirenv/bin" # We'll then specify the commanded to start the service ExecStart=/opt/http/flaskvirenv/bin/gunicorn --workers 4 --bind unix:app.sock -m 007 wsgi:app # This will tell systemd what to link this service to if we enable it to start at boot. We want this service to start when the regular multi-user system is up and running: [Install] WantedBy=multi-user.target
说明:
Note: In the last line of [Service] We tell it to start 4 worker processes. We will also tell it to create and bind to a Unix socket file within our project directory called app.sock. We’ll set a umask value of 007 so that the socket file is created giving access to the owner and group, while restricting other access. Finally, we need to pass in the WSGI entry point file name and the Python callable within.
测试:
sudo systemctl start app 启动后/opt/http目录下会增加rwxrws---权限的app.sock
sudo systemctl status app
sudo systemctl enable app
6 配置Nginx
安装nginx
sudo apt-get install nginx
增加app应用服务配置
/etc/nginx$ cat sites-available/app server { listen 80 default_server; 配置为默认服务器,不配置server_name进行判断 location / { include proxy_params; proxy_pass http://unix:/opt/http/app.sock; } }
将app软链接加入启动目录
sudo ln -s /etc/nginx/sites-available/app /etc/nginx/sites-enabled 在sites-enabled目录建立app软链接
sudo rm /etc/nginx/sites-enabled/default 删除default软链接,因为其中包含的default server的配置与目前冲突
调整nginx最大上传文件限制
sudo vim /etc/nginx/nginx.conf http { ## # Basic Settings ## sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; # server_tokens off; # server_names_hash_bucket_size 64; # server_name_in_redirect off; include /etc/nginx/mime.types; default_type application/octet-stream; client_max_body_size 200m;
检测配置合法性
sudo nginx -t
重启服务并测试
sudo systemctl restart nginx
参考链接:
Deploy flask app with Nginx using Gunicorn | by Tasnuva Zaman | FAUN Publication
Flask开发(二)部署到ubuntu16.04 - AdamTang - 博客园 (cnblogs.com)
Deployment Options — Flask Documentation (2.0.x) (palletsprojects.com)
Flask 应用如何部署 - Python有话说 - 博客园 (cnblogs.com)
Django + Uwsgi + Nginx 的生产环境部署 - 云+社区 - 腾讯云 (tencent.com)
(33条消息) Python之web服务利器Flask生产环境部署实践【基于gunicorn部署生产环境】_Together_CZ的博客-CSDN博客