Gunicorn Django部署配置方法
更新時間:2019年08月04日 09:50:41 作者:朝朝哥
這篇文章主要介紹了Gunicorn-Django部署配置方法,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
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 (外網(wǎng)訪問時開放端口) 查看網(wǎng)絡:ping 10.0.0.130 訪問:http://10.0.0.130:8000/index/ 
2. 添加環(huán)境變量
gunicorn目標位置:/usr/local/python3/lib/python3.6/site-packages (19.9.0) [root@qqc_os7 /]# vim /etc/profile 末尾追加要加入環(huán)境變量的應用: 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 工作的進程數(shù)量 ;[root@qqc_os7 untitled]# gunicorn -w 2 untitled.wsgi -b 0.0.0.0:8000 -k 工作進程類型,sync(默認), eventlet, gevent, or tornado, gthread, gaiohttp. 參考:http://www.dbjr.com.cn/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 #監(jiān)聽隊列 chdir = '/home/test/server/bin' #gunicorn要切換到的目的工作目錄 timeout = 30 #超時 worker_class = 'gevent' #使用gevent模式,還可以使用sync 模式,默認的是sync模式 workers = multiprocessing.cpu_count() * 2 + 1 #進程數(shù) threads = 2 #指定每個進程開啟的線程數(shù) 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項目目錄結(jié)構(gòu)
[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
總結(jié)
以上所述是小編給大家介紹的Gunicorn Django部署配置方法,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復大家的!
相關文章
centos6.5 安裝hadoop1.2.1的教程詳解【親測版】
這篇文章主要介紹了centos6.5 安裝hadoop1.2.1的教程詳細,該教程小編親測過,非常不錯,值得推薦給大家,需要的朋友可以參考下2018-08-08apache虛擬主機配置: 二級目錄訪問跳轉(zhuǎn)到其他根文件夾
這篇文章主要介紹了apache虛擬主機配置: 二級目錄訪問跳轉(zhuǎn)到其他根文件夾,需要的朋友可以參考下2016-11-1121個常用的apache .htaccess文件配置技巧分享
這篇文章主要介紹了21個常用的apache .htaccess文件配置技巧分享,涵蓋了網(wǎng)站中最常用的一些功能需求解決辦法,需要的朋友可以參考下2014-07-07