基于python-flask的web部署方案

部署前检查

在部署web应用之前,需要先检查一下即将部署的web应用的端口是否被占用,若被占用则需要考虑是否更换web应用的端口,或者关闭占用端口的其他应用。

查看所有监听端口:

netstat -ntlp

查看具体端口:

netstat -lnp|grep 80    #查看80端口

部署步骤:

1. 安装WSGI组件gunicorn

通过python安装工具pip安装gunicorn

pip install gunicorn

安装成功,测试运行

gunicron -w4 -b0.0.0.0:8000 myapp:app   #其中myapp为项目文件名,app为application程序

2. 安装进程任务管理器supervisor

pip install supervisor

3. 生成 supervisor 默认配置文件

echo_supervisord_conf > supervisor.conf

4. 修改 supervisor 配置文件,添加 gunicorn 进程管理

在myapp supervisor.conf 配置文件底部添加

vim supervisor.conf 
[program:myapp] 
command=/myproject/MyProject1/venv/bin/gunicorn -w4 -b0.0.0.0:2170 myapp:app ;  directory=/myproject/MyProject1 ; 
startsecs=0 ; 
stopwaitsecs=0 ; 
autostart=true ;
autorestart=true ; 
stdout_logfile=/myproject/MyProject1/log/gunicorn.log ; 
stderr_logfile=/myproject/MyProject1/log/gunicorn.err ; 

5. 调用supervisor进程任务管理器

supervisor基本使用命令:
supervisord -c supervisor.conf 通过配置文件启动
supervisorctl -c supervisor.conf status 察看
supervisor的状态 supervisorctl -c supervisor.conf reload 重新载入 配置文件 
supervisorctl -c supervisor.conf start [all]|[appname] 启动指定/所有 supervisor管理的程序进程 
supervisorctl -c supervisor.conf stop [all]|[appname] 关闭指定/所有 supervisor管理的程序进程
supervisorctl -c supervisor.conf update 		更新配置文件(当supervisor.conf被修改的时候)

# 取消配置文件
unlink [配置文件位置]		# 一般为/tmp/supervisor.sock

6. 安装配置 nginx

1) 安装依赖

yum -y install pcre-devel  #支持正则的pcre模块
yum -y install openssl openssl-devel

2) 下载并解压

wget http://nginx.org/download/nginx-1.10.3.tar.gz
tar -xvf   nginx-1.10.3.tar.gz

3) 配置路径

./configure --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/nginx --conf-path=/usr/local/nginx/nginx.conf --pid-path=/usr/local/nginx/nginx.pid

4) 编辑安装

make && make install

5) 测试

/usr/local/nginx/nginx  -t

出现以下信息表示安装成功

nginx: the configuration file /usr/local/nginx/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/nginx.conf test is successful

7. 配置nginx

将/usr/local/nginx/nginx.conf中相应的部分改为以下:

server {
    listen 80;
    server_name example.org; # 这是HOST机器的外部域名,用地址也行

    location / {
        proxy_pass http://127.0.0.1:8080; # 这里是指向 gunicorn host 的服务地址
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
  }

8. https配置

server {
    listen 443;
    server_name your-domain-name;
    ssl on;
    root html;
    index index.html index.htm;
    ssl_certificate   cert/214293826510663.pem;
    ssl_certificate_key  cert/214293826510663.key;
    ssl_session_timeout 5m;
    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 / {
        root html;
        index index.html index.htm;
    }
}

server {
        listen 443 ssl;
        server_name www.example.com;
        ssl on;
        ssl_certificate /usr/local/nginx/key/2141051716***.pem;
        ssl_certificate_key /usr/local/nginx/key/2141051716***.key;
        ssl_session_timeout  5m;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;     #指定SSL服务器端支持的协议版本
        ssl_ciphers  ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;    #指定加密算法
        ssl_prefer_server_ciphers   on;    #在使用SSLv3和TLS协议时指定服务器的加密算法要优先于客户端的加密算法
        location /{
            proxy_pass http://localhost:8081;
        } 
    }

最后运行:supervisorctl -c supervisor.conf reload

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章