最新版請(qǐng)參考
https://github.com/lihuacai168/AnotherFasterRunner
1.安裝uWSGI
pip install uWSGI
2.uWSGI配置uwsgi.ini
[uwsgi]
# 設(shè)置變量
project = FasterRunner
base = /home/faster
# 通過(guò)引用變量的方式設(shè)置Django項(xiàng)目根路徑
chdir = %(base)/%(project)
# Django項(xiàng)目的wsgi配置
module = %(project).wsgi:application
master = true
processes = 4
# socket 通信文件
socket = %(base)/%(project)/%(project).sock
chmod-socket = 666
vacuum = true
3.配置Django的settings文件wsgi.py
Django設(shè)置多配置文件(生產(chǎn)和開(kāi)發(fā))
import os
from django.core.wsgi import get_wsgi_application
# 指定Django使用生產(chǎn)配置FasterRunner.settings.pro
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'FasterRunner.settings.pro')
application = get_wsgi_application()
3.1測(cè)試uWSGI
# 進(jìn)入Django項(xiàng)目的根路徑
cd /home/faster/FasterRunner
# 運(yùn)行uwsgi
uwsgi --ini ./uwsgi.ini
4.Nginx配置uwsgi.conf,最好通過(guò)include的方式導(dǎo)入配置
server {
# 監(jiān)聽(tīng)8000端口
listen 8000;
# 服務(wù)器ip
server_name 10.0.3.57;
location / {
include uwsgi_params;
# 需要和uwsgi.ini 中的 socket 通信文件一樣
uwsgi_pass unix:/home/faster/FasterRunner/FasterRunner.sock;
}
}
4.1Nginx測(cè)試
# 啟動(dòng)nginx
systemctl start nginx
# 測(cè)試Nginx配置文件
nginx -t
# 重新加載,使Nginx配置文件生效
nginx -s reload
# 查看當(dāng)前端口監(jiān)聽(tīng),確認(rèn)8000端口被Nginx監(jiān)聽(tīng)
(fasterenv) [root@faster_3_57 FasterRunner]# netstat -tunlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:8000 0.0.0.0:* LISTEN 27106/nginx: master
5.Supervisor管理uWSGI
supervisor守護(hù)進(jìn)程虛擬環(huán)境部署Django
創(chuàng)建uWSGI啟動(dòng)的配置文件uwsgi.conf
command 中需要指定uwsgi的絕對(duì)路徑.此處是虛擬環(huán)境中的uwsgi路徑
Supervisor配置的注釋是;
[program:uwsgi]
command= /home/faster/.virtualenvs/fasterenv/bin/uwsgi --ini ./uwsgi.ini
directory=/home/faster/FasterRunner
stopasgroup=true
stopsignal=QUIT
user=faster
autostart=true
autorestart=true
stderr_logfile=/home/faster/err.log
stdout_logfile=/home/faster/out.log
5.1通過(guò)Supervisor啟動(dòng)uWSGI
# 啟動(dòng)Supervisor
supervisord -c /etc/supervisord.conf
# 重啟整個(gè) supervisor的服務(wù)
supervisorctl reload
# 指定啟動(dòng)uwsgi,此處是program后跟隨的名字
supervisorctl start uwsgi
# 停止uwsgi
supervisorctl stop uwsgi
6.uWSGI日志拋出異常Permission denied
tailf /var/log/nginx/error.log
2019/06/15 15:41:01 [crit] 29077#0: *65 connect() to unix:/home/faster/FasterRunner/FasterRunner.sock failed (13: Permission denied) while connecting to upstream,
client: 192.168.24.131, server: 10.0.3.57, request: "OPTIONS /api/user/login/ HTTP/1.1",
upstream: "uwsgi://unix:/home/faster/FasterRunner/FasterRunner.sock:", host: "10.0.3.57:8000", referrer: "http://10.0.3.57:8080/fastrunner/login"
6.1修改Nginx啟動(dòng)用戶(hù)
# 找到Nginx當(dāng)前主配置
[root@faster_3_57 FasterRunner]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
# 編輯Nginx主配置
vim /etc/nginx/nginx.conf
#user nginx;
user faster;
# 測(cè)試Nginx配置文件
nginx -t
# 重新加載,使Nginx配置文件生效
nginx -s reload
6.2異常原因分析
- 修改Nginx啟動(dòng)用戶(hù)前,默認(rèn)Nginx用戶(hù)啟動(dòng),因?yàn)闄?quán)限問(wèn)題,無(wú)法訪問(wèn)uWSGI的sock文件
(fasterenv) [root@faster_3_57 faster]# ps -ef|grep nginx
root 27106 1 0 15:01 ? 00:00:00 nginx: master process /usr/sbin/nginx
nginx 29077 27106 0 15:40 ? 00:00:00 nginx: worker process
root 29132 23308 0 15:41 pts/1 00:00:00 grep --color=auto nginx
- 查看uWSGI 啟動(dòng)用戶(hù),是faster
(fasterenv) [root@faster_3_57 faster]# ps -ef|grep uwsgi
faster 27354 28184 0 15:06 ? 00:00:00 /home/faster/.virtualenvs/fasterenv/bin/uwsgi --ini ./uwsgi.ini
faster 27358 27354 0 15:06 ? 00:00:00 /home/faster/.virtualenvs/fasterenv/bin/uwsgi --ini ./uwsgi.ini
faster 27359 27354 0 15:06 ? 00:00:00 /home/faster/.virtualenvs/fasterenv/bin/uwsgi --ini ./uwsgi.ini
faster 27360 27354 0 15:06 ? 00:00:00 /home/faster/.virtualenvs/fasterenv/bin/uwsgi --ini ./uwsgi.ini
faster 27361 27354 0 15:06 ? 00:00:00 /home/faster/.virtualenvs/fasterenv/bin/uwsgi --ini ./uwsgi.ini
root 29325 23308 0 15:45 pts/1 00:00:00 grep --color=auto uwsgi
- 修改Nginx啟動(dòng)用戶(hù)為faster(和啟動(dòng)uWSGI的用戶(hù)一樣),就闊以正常訪問(wèn)啦~~~
[root@faster_3_57 FasterRunner]# ps -ef|grep nginx
root 27106 1 0 15:01 ? 00:00:00 nginx: master process /usr/sbin/nginx
faster 29451 27106 0 15:47 ? 00:00:00 nginx: worker process
root 32093 22403 0 16:37 pts/0 00:00:00 grep --color=auto nginx
[root@faster_3_57 FasterRunner]# tailf /var/log/nginx/access.log
"http://10.0.3.57:8080/fastrunner/api_record/5" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36" "-"
192.168.24.131 - - [15/Jun/2019:16:40:09 +0800] "GET /api/fastrunner/tree/5/?token=4ee722ce4d8d5fdb9c5894879efb9760&type=1 HTTP/1.1" 200 966 "http://10.0.3.57:8080/fastrunner/api_record/5" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36" "-"
192.168.24.131 - - [15/Jun/2019:16:40:15 +0800] "GET /api/fastrunner/api/?token=4ee722ce4d8d5fdb9c5894879efb9760&node=66&project=5&search= HTTP/1.1" 200 13316 "http://10.0.3.57:8080/fastrunner/api_record/5" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36" "-"
192.168.24.131 - - [15/Jun/2019:16:40:18 +0800] "OPTIONS /api/user/login/ HTTP/1.1" 200 0 "http://10.0.3.57:8080/fastrunner/login" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36" "-"
192.168.24.131 - - [15/Jun/2019:16:40:18 +0800] "POST /api/user/login/ HTTP/1.1" 200 112 "http://10.0.3.57:8080/fastrunner/login" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36" "-"
192.168.24.131 - - [15/Jun/2019:16:40:18 +0800] "GET /api/fastrunner/project/?token=c4cd03206ac7e0deb42d1c2618624b75 HTTP/1.1" 200 593 "http://10.0.3.57:8080/fastrunner/project_list" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36" "-"