服务器 
首页 > 服务器 > 浏览文章

Gunicorn Django部署配置方法

(编辑:jimmy 日期: 2024/11/15 浏览:3 次 )

1. 简单部署

1. sudo pip3 install gunicorn
2. cd 到django项目中 sudo python3 manage.py migrate
3.启动服务:sudo python3 manage.py runserver 0.0.0.0:8000

4. 使用gunicorn 来运行项目
注:项目名untitled
[root@qqc_os7 untitled]# gunicorn untitled.wsgi -b 0.0.0.0:8000
[2019-08-04 09:31:17 +0800] [16614] [INFO] Starting gunicorn 19.9.0
[2019-08-04 09:31:17 +0800] [16614] [INFO] Listening at: http://0.0.0.0:8000 (16614)
[2019-08-04 09:31:17 +0800] [16614] [INFO] Using worker: sync
[2019-08-04 09:31:17 +0800] [16617] [INFO] Booting worker with pid: 16617

5. 查看进程
[root@qqc_os7 untitled]# ps aux | grep 8000
root  15383 0.2 1.9 213440 19028 pts/3 S+ 19:27 0:00 /usr/local/python3/bin/python3.6 /usr/local/python3/bin/gunicorn untitled.wsgi -b 0.0.0.0:8000
root  15386 0.2 3.3 256572 33676 pts/3 S+ 19:27 0:00 /usr/local/python3/bin/python3.6 /usr/local/python3/bin/gunicorn untitled.wsgi -b 0.0.0.0:8000
root  15389 0.0 0.0 112676 992 pts/2 S+ 19:30 0:00 grep --color=auto 8000

6.杀死进程
[root@qqc_os7 untitled]# ps aux | grep 8000 | grep -v grep | awk '{print $2}' | xargs kill


查看开放的端口:firewall-cmd --list-ports 
开启端口:firewall-cmd --zone=public --add-port=80/tcp --permanent (外网访问时开放端口)
查看网络:ping 10.0.0.130


访问:http://10.0.0.130:8000/index/
![](https://img2018.cnblogs.com/blog/1357260/201908/1357260-20190804092940438-114633478.png)

2. 添加环境变量

gunicorn目标位置:/usr/local/python3/lib/python3.6/site-packages (19.9.0)
[root@qqc_os7 /]# vim /etc/profile

末尾追加要加入环境变量的应用:
export PATH=/opt/mysql/bin:$PATH
export PATH=/opt/redis-3.2.10/src:$PATH:/usr/local/python3/bin
export RABBIT_HOME=/data/soft/rabbitmq_server-3.7.13
export PATH=$RABBIT_HOME/bin:$PATH 

3. gunicorn常用配置

Gunicorn“绿色独角兽”是一个被广泛使用的高性能的Python WSGI UNIX HTTP服务器
-c 指定一个配置文件(py文件)
-b 与指定的socket进行绑定
-D 以守护进程形式来运行Gunicorn进程,其实就是将这个服务放到后台去运行
-w 工作的进程数量 ;[root@qqc_os7 untitled]# gunicorn -w 2 untitled.wsgi -b 0.0.0.0:8000
-k 工作进程类型,sync(默认), eventlet, gevent, or tornado, gthread, gaiohttp.

参考:https://www.jb51.net/article/166871.htm
http://docs.gunicorn.org/en/latest/settings.html

配置文件(py文件,与django中的manage.py在同一目录)

# gunicorn_config.py
import logging
import logging.handlers
from logging.handlers import WatchedFileHandler
import os
import multiprocessing
bind = '10.0.0.130:8000'  #绑定ip和端口号
backlog = 512    #监听队列
chdir = '/home/test/server/bin' #gunicorn要切换到的目的工作目录
timeout = 30  #超时
worker_class = 'gevent' #使用gevent模式,还可以使用sync 模式,默认的是sync模式

workers = multiprocessing.cpu_count() * 2 + 1 #进程数
threads = 2 #指定每个进程开启的线程数
loglevel = 'info' #日志级别,这个日志级别指的是错误日志的级别,而访问日志的级别无法设置
access_log_format = '%(t)s %(p)s %(h)s "%(r)s" %(s)s %(L)s %(b)s %(f)s" "%(a)s"' 
accesslog = "/home/test/server/log/gunicorn_access.log"  #访问日志文件
errorlog = "/home/test/server/log/gunicorn_error.log"  #错误日志文件
通过配置文件启动django服务:
[root@qqc_os7 untitled]# gunicorn untitled.wsgi -c gunicorn_config.py

django项目目录结构

[root@qqc_os7 untitled]# tree
.
├── app01
│ ├── admin.py
│ ├── apps.py
│ ├── __init__.py
│ ├── migrations
│ │ ├── __init__.py
│ │ └── __pycache__
│ │  └── __init__.cpython-36.pyc
│ ├── models.py
│ ├── __pycache__
│ │ ├── admin.cpython-36.pyc
│ │ ├── apps.cpython-36.pyc
│ │ ├── __init__.cpython-36.pyc
│ │ ├── models.cpython-36.pyc
│ │ └── views.cpython-36.pyc
│ ├── tests.py
│ └── views.py
├── app02
│ ├── admin.py
│ ├── apps.py
│ ├── __init__.py
│ ├── migrations
│ │ ├── __init__.py
│ │ └── __pycache__
│ │  └── __init__.cpython-36.pyc
│ ├── models.py
│ ├── __pycache__
│ │ ├── admin.cpython-36.pyc
│ │ ├── __init__.cpython-36.pyc
│ │ ├── models.cpython-36.pyc
│ │ └── views.cpython-36.pyc
│ ├── templates
│ │ └── new_app
│ │  └── index.html
│ ├── tests.py
│ └── views.py
├── db.sqlite3
├── gunicorn_config.py
├── manage.py
├── static
├── templates
│ └── index.html
└── untitled
 ├── __init__.py
 ├── __pycache__
 │ ├── __init__.cpython-36.pyc
 │ ├── settings.cpython-36.pyc
 │ ├── urls.cpython-36.pyc
 │ └── wsgi.cpython-36.pyc
 ├── settings.py
 ├── urls.py
 └── wsgi.py

日志文件

[root@qqc_os7 log]# cat gunicorn_access.log 
[04/Aug/2019:01:15:14 +0000] <16598> 10.0.0.1 "GET /index/ HTTP/1.1" 200 0.050109 170 -" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36"
[04/Aug/2019:01:15:24 +0000] <16597> 10.0.0.1 "GET /index/ HTTP/1.1" 200 0.045950 170 -" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36

总结

以上所述是小编给大家介绍的Gunicorn Django部署配置方法,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!

上一篇:Ubuntu18.04.2下安装 RTX2080 Nvidia显卡驱动的方法
下一篇:Linux系统为什么要吃掉我的“内存”