首先進入conda 虛擬環境:nginx
source activate testdjango
安裝gunicorn:vim
pip install gunicornruby
運行gunicorn app
gunicorn -w 2 -b 127.0.0.1:9200 -D test.wsgi:application # -D 爲後臺運行spa
或者配置gunicorn.py 文件,並放置在項目根目錄下:debug
from gevent import monkey monkey.patch_all() import multiprocessing debug = True loglevel = 'debug' bind = '127.0.0.1:9200' //綁定與Nginx通訊的端口 pidfile = 'log/gunicorn.pid' logfile = 'log/debug.log' workers = multiprocessing.cpu_count() * 2 + 1 worker_class = 'gevent' //默認爲阻塞模式,最好選擇gevent模式
並運行 gunicorn -c gunicorn.py test.wsgi:application -D
rest
這時能夠用ps -ef | grep gunicorn 命令看gunicorn是否運行code
接下來配置nginx:server
在配置nginx前,先要在django setting 文件中host中加上localhost,不然會報400錯誤。並重啓gunicorn, kill -HUP pid (ps -ef | grep gunicorn 查看pid)
首先安裝nginx, 運行命令 sudo apt install nginx
nginx 命令:
sudo service nginx start 啓動
sudo service nginx stop 中止
sudo service nginx restart 關閉
配置文件:
sudo vim /etc/nginx/site-available/test.conf
server { listen 8000; charset utf-8; client_max_body_size 75M; location / { proxy_pass http://127.0.0.1:9200; } }
並生成軟連接 sudo ln -s /etc/nginx/site-available/test.conf /etc/nginx/site-enabled/test.conf
重啓nginx, sudo service nginx restart