https://cloud.tencent.com/developer/article/1112513 supervisor管理界面
api.py文件:
#coding:utf-8
import web
#url映射
urls = (
'/(.*)', 'hello')
#url處理的類
class hello:
def GET(self, name):
print(web.ctx.env.items())#獲取所有的headers
if not name:
name = 'World'
return 'Hello, ' + name + '!'
# 創建一個基於我們剛提交的URL列表的application。這個application會在這個文件的全局命名空間中查找對應類。
app = web.application(urls, globals())
application = app.wsgifunc() #這兩行放到外面
if __name__ == "__main__":
app.run() #啓動web應用
直接執行這個py文件也可以啓動web應用
二、通過gunicorn運行 app(wsgi application)
採用使用配置文件方式啓動gunicorn
config.py文件:
#coding:utf-8
import logging
import logging.handlers
from logging.handlers import WatchedFileHandler
import os
import multiprocessing
bind = '0.0.0.0:8080' #綁定ip和端口號
backlog = 512 #最大掛起的連接數
chdir = '/home/mytest/' #gunicorn要切換到的目的工作目錄
timeout = 30 #超時
workers = multiprocessing.cpu_count() * 2 + 1 #工作進程數
loglevel = 'info' #日誌級別,這個日誌級別指的是錯誤日誌的級別,而訪問日誌的級別無法設置
access_log_format = '%(t)s %(p)s %(h)s "%(r)s" %(s)s %(L)s %(b)s %(f)s" "%(a)s"' #設置gunicorn訪問日誌格式,錯誤日誌無法設置
accesslog = "gunicorn_access.log" #訪問日誌文件
errorlog = "gunicorn_error.log" #錯誤日誌文件
注:
-
如果通過gunicorn(wsgi)運行web,那麼就是gunicorn配置文件裏面的bind = '0.0.0.0:8080。如果寫127.0.0.1,那麼就只能在本機通過127.0.0.1:port來訪問,如果寫0.0.0.0,那就就可以用127.0.0.1:port、內網:port、外網:port來訪問web接口。---https://www.cnblogs.com/qiaoer1993/p/12102055.html
-
在項目目錄(cd /home/mytest/ )下,直接使用命令gunicorn --config=config.py api:application也可啓動web應用
[root@uap mytest]#vi /etc/supervisord.conf
[inet_http_server] #supervisor的web管理界面配置,http方式
port=0.0.0.0:19001
username=admin
password=123456
[supervisord] #不可少,管理服務本身的配置
pidfile=/var/run/supervisord.pid
logfile=/var/log/supervisord.log
logfile_maxbytes=50MB #日誌切割大小,默認50M
logfile_backups=10 #切割的日誌保留份數,默認10
loglevel=info
nodaemon=false #是否後臺開啓,默認false,true爲前臺開啓
minfds=1024 #可以打開的文件描述符的最小值,默認1024
minprocs=200 #可以打開的進程數的最小值,默認200
[program:api]
autorestart=true
command= gunicorn --config=config.py api:application
directory=/home/mytest/ #切換到該目錄
process_name=%(program_name)s
redirect_stderr=true
stdout_logfile=/var/log/di/%(program_name)s.log
注:直接執行命令/usr/bin/supervisord -c /etc/supervisord.conf可以啓動web應用,但開機重啓後因爲supervisor沒有啓動導致web應用起不來,所以接下來要實現supervisor開機自啓
通過pip install supervisor 安裝了supervisor,用來自動重啓服務;但當機器重啓後,supervisor卻不能自啓動,怎樣解決呢??利用linux的service。思路:在/etc/init.d目錄下編寫一個腳本,再用“service 腳本名 start|stop|status|restart”運行即可。chkconfig命令實現supervisor服務的開機自啓動。
#!/bin/bash
#
# supervisord This scripts turns supervisord on
# description: supervisor is a process control utility. It has a web based
# xmlrpc interface as well as a few other nifty features.
# processname: supervisord
# config: /etc/supervisord.conf
# pidfile: /var/run/supervisord.pid
#
# source function library
. /etc/rc.d/init.d/functions
RETVAL=0
start() {
echo -n $"Starting supervisord: "
daemon " /usr/bin/supervisord -c /etc/supervisord.conf"
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/supervisord
}
stop() {
echo -n $"Stopping supervisord: "
killproc supervisord
echo
[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/supervisord
}
restart() {
stop
start
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart|force-reload|reload)
restart
;;
condrestart)
[ -f /var/lock/subsys/supervisord ] && restart
;;
status)
status supervisord
RETVAL=$?
;;
*)
echo $"Usage: $0 {start|stop|status|restart|reload|force-reload|condrestart}"
exit 1
esac
exit $RETVAL
2、設置腳本可執行權限
# chmod +x /etc/init.d/supervisor
3、設置服務開機自啓動
# chkconfig supervisor on
五、重啓機器驗證
[root@zq di]# ps -ef| grep super
root 1960 1 0 23:01 ? 00:00:00 /usr/bin/python2 /usr/bin/supervisord -c /etc/supervisord.conf
root 3356 3085 0 23:19 pts/0 00:00:00 grep --color=auto super
2、驗證web服務是否啓動
[root@zq di]# ps -ef| grep api
root 2306 1960 0 23:01 ? 00:00:00 /usr/bin/python2 /bin/gunicorn --config=config.py api:application
root 2340 2306 0 23:01 ? 00:00:00 /usr/bin/python2 /bin/gunicorn --config=config.py api:application
root 2358 2306 0 23:01 ? 00:00:00 /usr/bin/python2 /bin/gunicorn --config=config.py api:application
root 2368 2306 0 23:01 ? 00:00:00 /usr/bin/python2 /bin/gunicorn --config=config.py api:application
root 2392 2306 0 23:01 ? 00:00:00 /usr/bin/python2 /bin/gunicorn --config=config.py api:application
root 2454 2306 0 23:01 ? 00:00:00 /usr/bin/python2 /bin/gunicorn --config=config.py api:application
root 2527 2306 0 23:01 ? 00:00:00 /usr/bin/python2 /bin/gunicorn --config=config.py api:application
root 2528 2306 0 23:01 ? 00:00:00 /usr/bin/python2 /bin/gunicorn --config=config.py api:application
root 3157 2306 0 23:06 ? 00:00:00 /usr/bin/python2 /bin/gunicorn --config=config.py api:application
root 3160 2306 0 23:06 ? 00:00:00 /usr/bin/python2 /bin/gunicorn --config=config.py api:application
root 3412 3085 0 23:21 pts/0 00:00:00 grep --color=auto api
六、nginx包裝
6.1安裝nginx(yum install nginx)
6.2、修改nginx的配置文件nginx.conf:
[root@zq mytest]#vi /etc/nginx.nginx.conf
server {
listen 888; #需要在防護牆中打開888端口,否則nginx日誌中會出現錯誤:nginx: [emerg] bind() to 0.0.0.0:888 failed (13: Permission denied)
location / {
client_max_body_size 2048M;
proxy_pass http://0.0.0.0:8080; #通過proxy_pass參數反向代理給運行在http://0.0.0.0:8080上的Gunicorn
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
6.3、重新加載nginx
nginx -s reload -c /etc/nginx/nginx.conf
注:若出現錯誤“nginx: [error] invalid PID number "" in "/run/nginx.pid"”,請執行以下兩句命令啓動nginx
sudo nginx -c /etc/nginx/nginx.conf
sudo nginx -s reload
6.4、訪問web瀏覽器 ,端口888