加载中…
个人资料
王江源
王江源
  • 博客等级:
  • 博客积分:0
  • 博客访问:1,379,685
  • 关注人气:1,086
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

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

(2018-11-13 10:00:26)
标签:

python

一、各组件介绍:

1、Anaconda:集成的python科学计算运行环境。
2、Flask : python web开发框架
3、Gunicorn:python wsgi服务器
4、Nginx:著名web服务器,这里用作Gunicorn的反向代理
5、Supervisor:linux下的进程守护工具,防止Gunicorn进程意外退出

各组件示意图:

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

 

 二:安装过程


1ANACONDA

(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下安装,命令:

        pip install gunicorn

(2)查看gunicorn进程:

        pstree -ap|grep gunicorn

(3)杀掉gunicorn进程:

        kill -9 xxxxx

(4)运行命令配置:

   可以用python脚本作为命令配置文件。flask项目跟目录下建立gunicorn_config.py文件,内容如下(根据实际情况修改):


# 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)
注意:runlogs文件夹需要手动创建

 

3、Supervisor

 

(1)安装: 

        yum install epel-release

        yum install -y supervisor

(2)编辑配置文件和设置开机启动:

   服务配置文件文件位置:/usr/lib/systemd/system/supervisord.service

   修改内容如下:

[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)启动服务:

        systemctl enable supervisord   # 开机自启动

        systemctl start supervisord     # 启动supervisord服务

        systemctl status supervisord    # 查看supervisord服务状态

(4)守护任务配置文件:

文件位置:/etc/supervisord.conf

添加内容:

[program:gunicorn_flask_dochelper]

directory = /home/www

command =  /root/anaconda3/bin/gunicorn  -c  /home/www/gunicorn_config.py   app:app

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 {

    listen 443;

    server_name www.xxx.com;

    client_max_body_size 10M;

    ssl on;

    ssl_certificate      "xxx.crt";

    ssl_certificate_key  "xxx.key";

    ssl_session_timeout  10m;

    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;

    ssl_prefer_server_ciphers on;

 

    location / {

        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        proxy_set_header Host $host;

        proxy_set_header X-Real-IP $remote_addr;

        proxy_set_header X-Scheme $scheme;

        proxy_redirect http:// $scheme://;

        proxy_pass http://127.0.0.1:8080;

    }

    location /static/(.*) {

             root /home/www/app/static/;

    }

}

 

负载均衡

upstream www.test.com { 

     server 192.168.0.118:8080; 

     server 192.168.0.119:8080; 

 

server {

    listen 443;

    server_name www.xxx.com;

    client_max_body_size 10M;

    ssl on;

    ssl_certificate      "xxx.crt";

    ssl_certificate_key  "xxx.key";

    ssl_session_timeout  10m;

    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;

    ssl_prefer_server_ciphers on;

 

    location / {

        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        proxy_set_header Host $host;

        proxy_set_header X-Real-IP $remote_addr;

        proxy_set_header X-Scheme $scheme;

        proxy_redirect http:// $scheme://;

        proxy_pass http://www.test.com;

    }

    location /static/(.*) {

             root /home/www/app/static/;

    }

}

 

(4)启动服务:

nginx

 

(5)其他命令

nginx -s stop

nginx -s quit

nginx -s reload 

nginx –t  # 测试配置文件是否正确




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

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有