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

# 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





阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页