CentOS7.5搭建Flask环境gunicorn(uWSGI)+nginx

gunicorn 的安装与使用非常的简单   

gunicorn的使用非常简单  一条命令  -w 为工作数量  -b为绑定的host       wsgi所在文件:实例名

sudo pip3 intstall gunicorn


sudo gunicorn -w 4 -b 127.0.0.1:8000  Project.wsgi:application

gunicorn的配置

gunicorn.conf文件也支持py文件

# 并行工作进程数
workers = 4
# 指定每个工作者的线程数
threads = 2
# 监听内网端口5000
bind = '127.0.0.1:5000'
# 设置守护进程,将进程交给supervisor管理
daemon = 'false'
# 工作模式协程
worker_class = 'gevent'
# 设置最大并发量
worker_connections = 2000
# 设置进程文件目录
pidfile = '/var/run/gunicorn.pid'
# 设置访问日志和错误信息日志路径
accesslog = '/var/log/gunicorn_acess.log'
errorlog = '/var/log/gunicorn_error.log'
# 设置日志记录水平
loglevel = 'warning'
使用配置文件中的参数启动

gunicorn -c gunicorn.conf app:app

uWSGI配置

pip 安装 uwsgi

# 启动命令
uwsgi --http :8000 --wsgi-file test.py
    http :8000: 使用http协议,端口8000
    wsgi-file test.py: 加载指定的文件,test.py
uwsgi支持ini、xml等多种配置方式,本文以 ini 为例, 在/etc/目录下新建uwsgi_nginx.ini,添加如下配置:

# mysite_uwsgi.ini file
# 配置
[uwsgi]
#配置文件路径
chdir           = /opt/mysite
# wsgi文件路径
module          = mysite.wsgi
# 环境目录
home            = /opt/venv

# master
master          = true

processes       = 1

socket          = 0.0.0.0:8000
vacuum          = true

nginx的配置

安装

 yum install nginx    默认安装到/usr/local/nginx/目录,进入目录。

简单配置启动

# 打开/usr/local/nginx/conf/nginx.conf文件


server {
    # 监听80端口
    listen 80;
    # 本机
    server_name localhost; 
    # 默认请求的url
    location / {
        #请求转发到 项目服务器(gunicorn,uwsgi)
        proxy_pass http://127.0.0.1:5001; 
        #设置请求头,并将头信息传递给服务器端 
        proxy_set_header Host $host; 
    }
}

启动nginx:

#启动
sbin/nginx
#查看
ps aux | grep nginx
#停止
sbin/nginx -s stop

nginx配置文件

worker_processes  1;
error_log  logs/error.log;
pid        logs/nginx.pid;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    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  logs/access.log  main;
    sendfile        on;
    keepalive_timeout  65;
  #nginx反向代理uwsgi
    server {
        listen       80;
        server_name  192.168.11.64;
        location / {
      #nginx自带ngx_http_uwsgi_module模块,起到nginx和uwsgi交互作用
         #通过uwsgi_pass设置服务器地址和协议,讲动态请求转发给uwsgi处理
         include  /opt/nginx1-12/conf/uwsgi_params;
         uwsgi_pass 0.0.0.0:8000;
            root   html;
            index  index.html index.htm;
        }
      #nginx处理静态页面资源
      location /static{
        alias /opt/nginx1-12/static;   
         }
     #nginx处理媒体资源
     location /media{
        alias /opt/nginx1-12/media;  
         }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

django静态文件配置问题

# 修改django配置

STATIC_ROOT='/opt/nginx1-12/static'
STATIC_URL = '/static/'
STATICFILES_DIRS=[
    os.path.join(BASE_DIR,"static"),
]


通过python3 manage.py collectstatic 收集所有你使用的静态文件保存到STATIC_ROOT!


STATIC_ROOT 文件夹 是用来将所有STATICFILES_DIRS中所有文件夹中的文件,以及各app中static中的文件都复制过来
# 把这些文件放到一起是为了用nginx等部署的时候更方便
 
原文地址:https://www.cnblogs.com/sw-z/p/10704589.html