django
# 刚写的就不复制粘贴了https://my.oschina.net/tianshl/blog/1611257# 列一下目录结构root@tianshl:~# cd server/root@tianshl:~/server# tree serverserver├── db.sqlite3├── manage.py└── server ├── __init__.py ├── settings.py ├── urls.py └── wsgi.py
gunicorn
安装
pip install gunicorn
配置
# 修改django项目的settings.pyINSTALLED_APPS = [ ...... 'gunicorn',]
运行
root@tianshl:~# cd /root/server/server/root@tianshl:~/server/server# gunicorn --pythonpath /root/server/venv/bin/python3 -w 3 -b 0.0.0.0:80 server.wsgi# 测试能否正常运行, 然后ctrl+c结束进程
supervisor
安装
pip install supervisor
配置
# 默认配置 # 使用echo_supervisord_conf命令查看默认配置root@tianshl:~# echo_supervisord_conf# 自定义配置root@tianshl:~# mkdir /etc/supervisorroot@tianshl:~# mkdir /etc/supervisor/conf.droot@tianshl:~# echo_supervisord_conf > /etc/supervisor/supervisor.confroot@tianshl:~# vim /etc/supervisor/supervisor.conf# 内容如下[unix_http_server]file=/tmp/supervisor.sock ; the path to the socket file[supervisord]logfile=/tmp/supervisord.log ; main log file; default $CWD/supervisord.loglogfile_maxbytes=10MB ; max main logfile bytes b4 rotation; default 50MBlogfile_backups=1 ; # of main logfile backups; 0 means none, default 10loglevel=info ; log level; default info; others: debug,warn,tracepidfile=/tmp/supervisord.pid ; supervisord pidfile; default supervisord.pidnodaemon=false ; start in foreground if true; default falseminfds=1024 ; min. avail startup file descriptors; default 1024minprocs=200 ; min. avail process descriptors;default 200[rpcinterface:supervisor]supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface[supervisorctl]serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL for a unix socket[include]files = /etc/supervisor/conf.d/*.conf# 自定义服务配置root@tianshl:~# vim /etc/supervisor/conf.d/server.conf内容如下:[program:server]directory = /root/server/server/command = gunicorn --pythonpath /root/server/venv/bin/python3 -w 3 -b 0.0.0.0:80 server.wsgiautostart = truestartsecs = 5autorestart = truestartretries = 3user = rootredirect_stderr = truestdout_logfile_maxbytes = 10MBstdout_logfile_backups = 1stdout_logfile = /root/logs/server.log
启动
supervisord -c /etc/supervisor/supervisor.conf
常用指令
root@tianshl:~# supervisorctl -c /etc/supervisor/supervisor.conf reload # 重新加载配置文件root@tianshl:~# supervisorctl -c /etc/supervisor/supervisor.conf status # 服务状态root@tianshl:~# supervisorctl -c /etc/supervisor/supervisor.conf start server # 启动某个服务(这里的server是服务名称,即program的名字)root@tianshl:~# supervisorctl -c /etc/supervisor/supervisor.conf restart server # 重启某个服务root@tianshl:~# supervisorctl -c /etc/supervisor/supervisor.conf stop server # 停止某个服务