0
点赞
收藏
分享

微信扫一扫

68. Django项目部署nginx + gunicorn

需求

在使用 uwsgi配置dwebsocket的时候,总会有使用上的问题。

68. Django项目部署nginx + gunicorn_运维

image-20200420111715653

但是,直接使用​​python manage.py runserver​​​启动是没问题的。其实只是我没有将uwsgi配置好,不过本章节主要是想看看如何使用​​gunicorn​​来部署试试。

安装gunicorn

安装命令如下:

pip3 install gunicorn

配置全局命令:安装完毕之后,全局环境是无法直接执行的,需要找到二进制文件软链接到​​/usr/bin​​路径下。

# 安装之后,无法直接执行命令
[root@server01 ~]# gunicorn -h
-bash: gunicorn: command not found

搜索安装之后,​​gunicorn二进制可执行文件​​的位置:

[root@server01 ~]# find / -name "*gunicorn*" -ls | grep python3 | grep bin
405121 4 -rwxr-xr-x 1 root root 236 Dec 12 08:31 /usr/local/python3/bin/gunicorn
[root@server01 ~]#

设置软链接如下:

[root@server01 ~]# ln -s /usr/local/python3/bin/gunicorn /usr/bin/gunicorn
[root@server01 ~]#
[root@server01 ~]# ls -ll /usr/bin/gunicorn
lrwxrwxrwx 1 root root 31 Dec 12 08:38 /usr/bin/gunicorn -> /usr/local/python3/bin/gunicorn
[root@server01 ~]#

配置软链接之后,就可以全局环境使用gunicorn了,例如查看版本如下:

[root@server01 ~]# gunicorn -v
gunicorn (version 20.0.4)
[root@server01 ~]#

使用gunicorn启动Django项目

在项目根目录新建配置文件 gunicorn.confg

bind = "0.0.0.0:8000"

# workers是工作线程数,一般设置成:服务器CPU个数 + 1
workers = 2

#./代表当前目录
errorlog = './logs/gunicorn.error.log'
accesslog = './logs/gunicorn.access.log'

项目启动

普通命令行启动

gunicorn -w 4 -b 0.0.0.0:8000 --access-logfile access.log --error-logfile error.log 项目名.wsgi

使用配置文件启动

gunicorn 项目名.wsgi -c gunicorn.confg -D

配置文件​​gunicorn.confg​

bind = "0.0.0.0:8000"

# workers是工作线程数,一般设置成:服务器CPU个数 + 1
workers = 2

#./代表当前目录
errorlog = './logs/gunicorn.error.log'
accesslog = './logs/gunicorn.access.log'

执行如下:

gunicorn performance.wsgi -c gunicorn.confg -D

重启gunicorn

当代码出现部署变更,那么则需要重启一下 gunicorn

# 查看当前的gunicorn进程
[root@locust01 performance]# ps -ef | grep gunicorn
root 848 30090 0 17:13 pts/2 00:00:00 grep --color=auto gunicorn
root 2973 27177 0 May12 ? 00:00:17 /usr/local/python3/bin/python3.7 /usr/bin/gunicorn performance.wsgi -c gunicorn.confg -D
root 4940 27177 0 May12 ? 00:00:14 /usr/local/python3/bin/python3.7 /usr/bin/gunicorn performance.wsgi -c gunicorn.confg -D
root 27177 1 0 May07 ? 00:01:33 /usr/local/python3/bin/python3.7 /usr/bin/gunicorn performance.wsgi -c gunicorn.confg -D
[root@locust01 performance]#
[root@locust01 performance]# ps -ef | grep gunicorn | grep -v color | awk '{print $2}'
2973
4940
27177

# kill 当前gunicorn 进程
[root@locust01 performance]# ps -ef | grep gunicorn | grep -v color | awk '{print $2}' | xargs kill -9
[root@locust01 performance]#
[root@locust01 performance]# ps -ef | grep gunicorn
root 903 30090 0 17:13 pts/2 00:00:00 grep --color=auto gunicorn

# 重启服务
[root@locust01 performance]# gunicorn performance.wsgi -c gunicorn.confg -D
!!!
!!! WARNING: configuration file should have a valid Python extension.
!!!

[root@locust01 performance]#
[root@locust01 performance]# ps -ef | grep gunicorn
root 989 1 0 17:14 ? 00:00:00 /usr/local/python3/bin/python3.7 /usr/bin/gunicorn performance.wsgi -c gunicorn.confg -D
root 992 989 3 17:14 ? 00:00:00 /usr/local/python3/bin/python3.7 /usr/bin/gunicorn performance.wsgi -c gunicorn.confg -D
root 993 989 3 17:14 ? 00:00:00 /usr/local/python3/bin/python3.7 /usr/bin/gunicorn performance.wsgi -c gunicorn.confg -D
root 1044 30090 0 17:14 pts/2 00:00:00 grep --color=auto gunicorn
[root@locust01 performance]#

或者使用 ​​--reload​​参数,自动重启。

也可以直接修改配置文件如下:

bind = "0.0.0.0:8000"

# workers是工作线程数,一般设置成:服务器CPU个数 + 1
workers = 2

#./代表当前目录
errorlog = './logs/error.log'
accesslog = './logs/access.log'

# 设置自动重启
reload = True

使用gunicorn启动默认是无法找到statics文件的

启动服务之后,访问网页,可以看到statics的文件目录是

68. Django项目部署nginx + gunicorn_docker_02

image-20200420121828554

根据解决问题的办法,使用nginx转发static即可。

Nginx配置访问gunicorn

安装nginx这个步骤就省略了,下面来看看如何设置转发。

Nginx配置:

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

...
http {
....
# 配置performance项目的上游服务
upstream performance{
server 127.0.0.1:8888;
}

# 配置server
server {
listen 80;
server_name localhost;
#charset koi8-r;

access_log logs/host.access.log main;

# 配置动态请求使用gunicorn
location / {
#请求转发到gunicorn服务器
proxy_pass http://performance;
#设置请求头,并将头信息传递给服务器端
proxy_set_header Host $host;
proxy_http_version 1.1;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

# 解决nginx转发websocket连接失败的问题
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}

# 配置静态文件路径
location /static/ {
alias /work/performance/static/;
}

...
}

}



举报

相关推荐

0 条评论