美文网首页
CentOS7+Nginx+Gunicorn部署Django

CentOS7+Nginx+Gunicorn部署Django

作者: 倪大头 | 来源:发表于2020-04-10 15:51 被阅读0次

gunicorn安装:

pip3 install gunicorn

安装完敲gunicorn找不到命令的话参考:
https://www.jianshu.com/p/bd536b1b9a3c

使用gunicorn的话需要再django项目中的settings.py里把gunicorn加上:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'myblog',
    'gunicorn',
]

接下来在项目根目录(manage.py同级目录)创建gunicorn的配置文件,文件是.py格式(如gunicorn.conf.py):

import multiprocessing

# 并行工作进程数,官网文档示例2 * cpu数 + 1
workers = multiprocessing.cpu_count() * 2 + 1
# 指定每个进程的线程数
threads = 2
# 监听内网端口
bind = '0.0.0.0:8001'
# 设置守护进程(linux有效)
daemon = 'true'
# 工作模式协程 使用gevent异步模式 提高了响应速度 必须先安装好gevent才能用
# worker_class = 'gevent'
# 超时时间
timeout = 60
# 设置最大并发量
worker_connections = 2000
# 设置进程文件目录
pidfile = '/home/DjangoProject/blogserver/gunicorn.pid'
# 设置访问日志和错误信息日志路径
accesslog = '/home/DjangoProject/blogserver/gunicorn_access.log'
errorlog = '/home/DjangoProject/blogserver/gunicorn_error.log'
# 设置日志记录水平
loglevel = 'debug'

路径千万别写错了,接下来cd到项目根目录执行:

gunicorn -c gunicorn.conf.py 项目名.wsgi:application

成功了可以查看当前活跃的gunicorn进程:

ps -ef|grep gunicorn

这时候访问你的公网ip:端口号就可以访问项目了,端口号是gunicorn.conf.py中bind的端口号,上面的例子中是8001,可以修改成别的

nginx安装:

yum install nginx

nginx配置文件的路径是/etc/nginx/nginx.conf,打印出来内容如下:

[root@datou ~]# cat /etc/nginx/nginx.conf 

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/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  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

看到最后一行include /etc/nginx/conf.d/*.conf;
那直接在conf.d目录下创建项目的nginx配置文件就好了,这里我命名为blogserver.conf:

vim /etc/nginx/conf.d/blogserver.conf

在里面输入以下内容:

server {
    listen 80;
    server_name www.yourdomain.cn;
    access_log /home/DjangoProject/blogserver/nginx_access.log;
    error_log /home/DjangoProject/blogserver/nginx_error.log;

    location / {
        proxy_pass http://127.0.0.1:8001;
        proxy_pass_header       Authorization;
        proxy_pass_header       WWW-Authenticate;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

proxy_pass的端口号要和之前在gunicorn里绑定的端口号一致
server_name可以写域名也可以写公网ip
配置好了启动nginx
如果有以下报错需要在nginx主配置文件的http括号里加一行:
报错:

nginx: [emerg] could not build server_names_hash, you should increase server_names_hash_bucket_size: 64

nginx主配置文件路径:

/etc/nginx/nginx.conf

加入代码:

server_names_hash_bucket_size 512;
image.png

现在nginx和gunicorn都在启动状态了,直接访问公网ip或者域名就可以打开项目了

相关文章

网友评论

      本文标题:CentOS7+Nginx+Gunicorn部署Django

      本文链接:https://www.haomeiwen.com/subject/yburmhtx.html