相关技术
涉及到相关的技术:gunicorn、flask、nginx、supervisor
- supervisor 进程管理,使得nginx、gunicorn开机自启动,服务挂了可以自动重启 。
- nginx 服务器反向代理,高并发,日志等。
- gunicorn 生产环境下部署flask就不用开发者模式了,用gunicorn来实现生产环境提高性能和服务的稳定性。
- flask python语言实现的一种web应用框架。
安装
sudo apt-get install supervisor
sudo apt install nginx
pip install gunicorn
配置文件及注解
sudo vim /etc/nginx/sites-available/default
# nginx 默认配置文件
cat /etc/nginx/nginx.conf
# nginx 的用户配置文件,相比默认配置文件优先级高
cat /etc/supervisor/conf.d/gunicorn.conf
# supervisor 管理gunicorn进程的配置文件,要自己创建
cat /etc/supervisor/conf.d/nginx.conf
# supervisor 管理nginx进程的配置文件,要自己创建
反向代理配置nginx的默认配置
listen 8080 是公网暴露的端口
proxy_pass http://localhost:8000是内网服务器接口的地址,这里是服务器本地
server {
listen 8080 default_server;
listen [::]:8080 default_server;
server_name xjfd;
# SSL configuration
#
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
proxy_pass http://localhost:8000;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
/etc/supervisor/conf.d/gunicorn.conf配置文件
手动创建
/home/xjfd/.conda/envs/cp37/bin/gunicorn命令路径
/home/xjfd/server/server_all项目文件夹
user=xjfd用户名
[program:gunicorn]
command=/home/xjfd/.conda/envs/cp37/bin/gunicorn -w 2 -b :8000 server:app -D
directory=/home/xjfd/server/server_all
autostart=true
autorestart=true
user=xjfd
redirect_stderr=true
/etc/supervisor/conf.d/nginx.conf配置文件
手动创建
/usr/sbin/nginx -g 'daemon on;' 守护线程
以root权限启动nginx
[program:nginx]
command=/usr/sbin/nginx -g 'daemon on;'
autostart=true
autorestart=true
user=root
redirect_stderr=true
重启or启动
sudo /etc/init.d/nginx restart
# 启动nginx
sudo /etc/init.d/supervisor restart
# 启动supervisor
测试
cd /home/njit/wind/test/server_all
# 此路径是服务器文件路径
find / -name gunicorn 2>/dev/null
# 用这条命令来查找 /home/user/virtualenvs/py37/bin/gunicorn是查找结果
/home/user/virtualenvs/py37/bin/gunicorn -w 2 -b :8080 server:app -D
# server是文件名
# -w 是线程数 -b是端口号 -D 是守护线程
# gunicorn 路径是需要查找本机的路径的
curl -X POST 127.0.0.1:8000
curl --request POST --url http://127.0.0.1:8000/rest/wt/v2/tilt
# 测试 使用curl命令,没有的需要安装,具体去查一下吧,此处不详解
日志文件
tail /var/log/nginx/error.log
# nginx 报错日志文件
tail /var/log/nginx/access.log
# nginx 访问日志文件
关闭服务
sudo /etc/init.d/supervisor stop
sudo /etc/init.d/nginx stop
sudo pkill nginx
sudo pkill gunicorn
本文介绍如何使用gunicorn和nginx部署Flask应用,并通过supervisor进行进程管理,确保服务稳定运行。涉及配置文件详解及命令操作。
575

被折叠的 条评论
为什么被折叠?



