Flask Nginx Gunicorn Centos7 环境部署,将项目上线

1、创建一个虚拟环境

cd /home/
mkdir flaskProject
cd ./flaskProject/
pip3 install virtualenv
virtualenv -p python3 --no-site-packages venv

 

2、创建启动文件,如何创建项目,不在这里介绍

# wsgi.py
from flask import app
    
application = app
if __name__ == '__main__':
    application.run()

3、进入一个虚拟环境

source flaskProject/bin/activate

4、安装并测试Gunicorn是否能启动你的项目服务

pip3 install gunicorn
gunicorn --bind 0.0.0.0:8000 wsgi:application

5、创建一个 Gunicorn Systemd Service 文件

vim /etc/systemd/system/gunicorn.service
[Unit]                                                                                                  
Description=gunicorn daemon
After=network.target

[Service]
User=root
Group=nginx
WorkingDirectory=/root/github/FamilyAll/FamilyApi/
ExecStart=/root/github/FamilyAll/FamilyApi/venv/bin/gunicorn -w 4 -b 0.0.0.0:8000 wsgi:application

[Install]
WantedBy=multi-user.target
# systemctl start gunicorn
# systemctl enable gunicorn

6、配置nginx代理通过Gunicorn

# yum install nginx
# vim /etc/nginx/nginx.conf
 user nginx;
 worker_processes auto;
 error_log /var/log/nginx/error.log;
 pid /run/nginx.pid;
 include /usr/share/nginx/modules/*.conf;
 events {
     worker_connections 1024;
 }
 http {
     log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                       '$status $body_bytes_sent "$http_referer" '
                       '"$http_user_agent" "$http_x_forwarded_for"';
     access_log  /var/log/nginx/access.log  main;
     sendfile            on;
     tcp_nopush          on;
     tcp_nodelay         on;
     keepalive_timeout   65;
     types_hash_max_size 2048;
     include             /etc/nginx/mime.types;
     default_type        application/octet-stream;
     server {
         listen       80 default_server;
         listen       [::]:80 default_server;
         server_name  _;
         root         /usr/share/nginx/html;
 
         # Load configuration files for the default server block.
         include /etc/nginx/default.d/*.conf;
 
         location / {
         }
 
         error_page 404 /404.html;
             location = /40x.html {
         }
                                                                                                                                                                                                       
         error_page 500 502 503 504 /50x.html;
             location = /50x.html { 
         }   
     }   
     server {
         listen 8080;
         server_name www.wengdell.com; # 这是HOST机器的外部域名,用地址也行
     
         location / {
             proxy_pass http://127.0.0.1:8000; # 这里是指向 gunicorn host 的服务地址
             proxy_set_header Host $host;
             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
         }   
     
     }

7、开启nginx服务并开机自启

# systemctl start nginx
# systemctl enable nginx

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
nginx是一个高性能的HTTP和反向代理服务器,可以用来处理静态文件和动态请求。gunicorn是一个Python WSGI HTTP服务器,可以将Flask应用程序部署到生产环境中。supervisor是一个进程控制系统,可以用来管理和监控进程。这三个工具可以一起使用来部署Flask应用程序。 以下是使用nginxgunicorn和supervisor部署Flask应用程序的步骤: 1. 安装nginxgunicorn和supervisor。 2. 编写Flask应用程序,并使用工厂函数构建应用程序对象。 3. 创建一个gunicorn配置文件,例如gunicorn.conf.py,指定工作进程数和线程数。 4. 使用gunicorn启动Flask应用程序,例如: ```shell gunicorn -c gunicorn.conf.py "my_project:create_app()" ``` 这将启动一个gunicorn进程,监听8000端口,并将请求转发到Flask应用程序。 5. 配置nginx,将请求转发到gunicorn进程。例如,在nginx.conf文件中添加以下内容: ```nginx server { listen 80; server_name example.com; location / { proxy_pass http://127.0.0.1:8000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } ``` 这将将所有请求转发到gunicorn进程,并将请求头中的一些信息传递给Flask应用程序。 6. 使用supervisor管理gunicorn进程。例如,在supervisor.conf文件中添加以下内容: ```ini [program:gunicorn] command=/path/to/gunicorn -c /path/to/gunicorn.conf.py "my_project:create_app()" directory=/path/to/project user=user autostart=true autorestart=true stopasgroup=true killasgroup=true ``` 这将启动一个名为gunicorn的进程,并在系统启动时自动启动该进程。如果该进程崩溃或被杀死,supervisor将自动重新启动该进程。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值