使用gunicorn部署你的django项目

这篇文章主要介绍了gunicorn 部署django的配置方法,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下

1. 安装gunicorn

pip install gunicorn
cd 到django项目中 python3  manage.py  migrate
启动服务:sudo python3 manage.py runserver 0.0.0.0:8080

 

2.  使用gunicorn启动django 

注:项目名为student

 

进入到项目目录下

gunicorn  student.wsgi -b 0.0.0.0:8000

 

3. gunicorn常用参数

-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

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

# vim  gunicorn_config.py 
import logging
import logging.handlers
from   logging.handlers import WatchedFileHandler
import multiprocessing
import os

bind = '192.16.30.11:9898'  #绑定ip和端口号
backlog = 512    #监听队列
#chdir = '/home/test/server/bin' #gunicorn要切换到的目的工作目录
timeout = 30  #超时
worker_class = 'sync' #使用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 = "/tmp/gunicorn_access.log"  #访问日志文件
errorlog = "/tmp/gunicorn_error.log"  #错误日志文件

通过配置文件启动django服务:

gunicorn  student.wsgi -c gunicorn_config.py  (如果加上-D参数,表示后台运行)

查看进程

 

posted @ 2023-02-13 18:08  羊脂玉净瓶  阅读(1325)  评论(0)    收藏  举报