项目工程目录如下:
第一步,测试runserver
cd django3demo1/mysite
python manage.py runserver 0.0.0.0:8002
http://127.0.0.1:8002/polls/ 访问正常
第二步, 配置uwsgi启动
配置文件uwsgi.ini
[uwsgi1]
ini=:base
uwsgi-socket=0.0.0.0:8101
stats=0.0.0.0:8201
pidfile=/mnt/run/django3demo1.pid
;使用supervisor管理进程时不能设置daemonize模式
daemonize = /mnt/logs/uwsgi.log
;logger=file:$(LOG_ROOT)uwsgi1.log
;env=LOG_FILE_SUFFIX=default_01
;代码更改自动链式重启,也可手动touch /mnt/run/reload_lobby1
;touch-chain-reload=/mnt/run/reload_lobby1
enable-threads=true
single-interpreter=true
[base]
;加载指定的python WSGI模块
module=mysite.wsgi
;开启主进程模式
master=true
;设置socket的监听队列大小(默认:100)
listen=100
;设置工作进程的数量,默认可以设置为cpu核数*2
processes=2
;定义日志格式
;logformat=%(addr) - [%(ctime)] %(method) %(uri) %(proto) %(status) %(size) [%(msecs) msecs] [pid:%(pid)] [rss:%(rssM)M] [vsz:%(vszM)M] "%(uagent)"
logformat=%(addr) - [%(ctime)] %(method) %(uri) %(proto) %(status) %(size) %(msecs) msecs "%(referer)" "%(uagent)"
;经过N秒不活跃状态后销毁所有工作进程(这时就进入了cheap模式),只会剩下主进程
;idle=30
;为每个工作进程设置请求数的上限,超过上限后工作进程就会被回收
max-requests=50000
;顺序重载每个工作进程,优先加载完的工作进程立即开始工作,需要配合touch-chain-reload参数使用才能生效
lazy-apps=true
;开启内存使用情况报告
memory-report=true
;每个工作进程的线程数
threads=1
;请求超过harakiri指定的秒数之后,会丢弃这个请求,且当前woker会被回收(即重启woker)
harakiri=45
;当一个请求是“harakiri”杀死的,会记录信息到uwsgi日志里。打开这个选项会输出额外的信息
harakiri-verbose=true
启动uwsgi
uwsgi --ini mysite/uwsgi.ini:uwsgi1
uwsgi --stop /mnt/run/django3demo1.pid
uwsgi --reload /mnt/run/django3demo1.pid
第三步, nginx配置反向代理到uwsgi
upstream mysite_app {
server 127.0.0.1:7101 weight=5;
}
server {
listen 8000;
server_name _;
charset utf-8;
gzip on;
gzip_min_length 1k;
gzip_buffers 16 64k;
gzip_http_version 1.1;
gzip_comp_level 6;
gzip_types text/plain application/x-javascript text/css application/xml application/json;
#gzip_vary on;
location / {
include uwsgi_params;
uwsgi_pass mysite_app;
}
}
http://127.0.0.1:8000/polls/ 访问正常
第四步, uwsgi启动配置gevent(可选)
uwsgi --gevent 100 --gevent-monkey-patch --ini mysite/uwsgi.ini:uwsgi1
第五步, shell方法启动uwsgi
改成以人类能直观看懂的方式,配置文件bin/h~~~~ttp_server.sh
#!/bin/bash
function usage()
{
cat <<EOF
Usage: bin/http_server.sh {start|reload|stop|gevent-start|restart}
Options:
[-h|--help] : show this help
EOF
}
SCRIPT_PATH=$0
echo $SCRIPT_PATH
WORK_PATH=$(dirname $(dirname $SCRIPT_PATH))
echo $WORK_PATH
while [ "$#" -gt 0 ]
do
case "$1" in
-h|--help)
usage
exit 1
;;
start)
count=ps auxf|grep uwsgi.ini|grep -v grep |wc -l`
if [ $count -gt 0 ]; then
echo "http server already start ......"
exit 1
fi
uwsgi --ini mysite/uwsgi.ini:uwsgi1
exit 1
;;
restart)
echo "killing servers ... "
ps auxf|grep uwsgi.ini|grep -v grep|awk {'print $2'}|sudo xargs kill -9
echo "wait for http server to exit...."
sleep 3
uwsgi --ini mysite/uwsgi.ini:uwsgi1
exit 1
;;
gevent-start)
uwsgi --ini mysite/uwsgi.ini:uwsgi1
exit 1
;;
stop)
uwsgi --stop /mnt/run/django3demo1.pid
exit 1
;;
reload)
uwsgi --reload /mnt/run/django3demo1.pid
exit 1
;;
esac
shift
done
启动uwsgi
sh bin/http_server.sh start
第六步, 配置supervisor启动uwsgi
配置文件: mysite_supervisor.conf
[program:uwsgi1]
directory = /mnt/hgfs/vmshare/study/python/django3demo1/mysite
environment = PYTHONPATH=./
command = /mnt/venv/python38/bin/uwsgi --ini mysite/supervisor_uwsgi.ini:uwsgi
autostart = true
;startsecs = 30
autorestart = true
startretries = 3
user = zyb
redirect_stderr = True
stdout_logfile = /mnt/logs/supervisor.log
stdout_logfile_maxbytes = 0
stdout_logfile_backups = 0
stdout_capture_maxbytes = 10MB
priority = 31
stopsignal=QUIT
pip3 install supervisor
sudo supervisorctl update
sudo supervisorctl restart all
supervisor的具体使用方式就不具体介绍了
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。