PythonWeb基础服务器搭建记录:Anaconda+Flask+Gunicorn+Nginx+Supervisor+Centos

标签:
python |

1、ANACONDA
(1)登录网址:https://www.anaconda.com/download/
(2)下载对系统和python版本的文件,比如Anaconda3-5.3.0-Linux-x86_64.sh
(3)执行安装命令:
yum install -y bzip2
bash ./ Anaconda3-5.3.0-Linux-x86_64.sh
接下来按提示操作即可
2、Gunicorn
(1)在python下安装,命令:
(2)查看gunicorn进程:
(3)杀掉gunicorn进程:
(4)运行命令配置:
# coding=utf-8
import sys
import os
import multiprocessing
path_of_current_file = os.path.abspath(__file__)
path_of_current_dir = os.path.split(path_of_current_file)[0]
_file_name = os.path.basename(__file__)
sys.path.insert(0, path_of_current_dir)
worker_class = 'egg:meinheld#gunicorn_worker'
workers = multiprocessing.cpu_count() * 2 + 1
chdir = path_of_current_dir
worker_connections = 1000
timeout = 30
max_requests = 2000
graceful_timeout = 30
loglevel = 'info'
reload = True
debug = True
bind = "%s:%s" % ("127.0.0.1", 8080)
pidfile = '%s/run/%s.pid' % (path_of_current_dir, _file_name)
errorlog = '%s/logs/%s_error.log' % (path_of_current_dir, _file_name)
accesslog = '%s/logs/%s_access.log' % (path_of_current_dir, _file_name)
注意:run和logs文件夹需要手动创建
3、Supervisor
(1)安装:
(2)编辑配置文件和设置开机启动:
[Unit]
Description=Supervisor daemon
[Service]
Type=forking
PIDFile=/var/run/supervisord.pid
ExecStart=/bin/supervisord -c /etc/supervisord.conf
ExecStop=/bin/supervisorctl shutdown
ExecReload=/bin/supervisorctl reload
KillMode=process
Restart=on-failure
RestartSec=42s
[Install]
WantedBy=multi-user.target
(3)启动服务:
(4)守护任务配置文件:
文件位置:/etc/supervisord.conf
添加内容:
[program:gunicorn_flask_dochelper]
directory = /home/www
command =
autostart =
true
startsecs = 10
autorestart = true
startretries = 3
user = root
redirect_stderr = true
stdout_logfile_maxbytes =
20MB
stdout_logfile_backups =
20
也可以在/etc/supervisord.d目录下新建xxx.ini文件,写入以上内容,会自动加载。
(5)常用命令:
启动服务
supervisorctl start all
supervisorctl start service_name
关闭服务
supervisorctl stop all
supervisorctl stop service_name
查看状态
supervisorctl status service_name
重新启动所有服务或者是某个服务
supervisorctl restart all
supervisorctl restart service_name
配置文件修改后重新载入
supervisorctl reload
(6)常见错误:
Error: Another program is already listening on a port that one of our HTTP servers is configured to use. Shut this program down first before starting
解决方法:
find / -name supervisor.sock
unlink
/name/supervisor.sock
4、nginx
(1)安装:
yum install nginx
(2)配置文件
默认路径为:/etc/nginx/nginx.conf
可以通过下面命令查看路径:
nginx –t
(3)反向代理配置:
支持https
server {
}
负载均衡
upstream www.test.com
{
}
server {
}
(4)启动服务:
nginx
(5)其他命令
nginx -s stop
nginx -s quit
nginx -s
reload
nginx
–t