Nginx + supervisor + uwsgi + flask 部署笔记

linux: CentOS release 6.8

环境安装

sudo yum install nginx
sudo yum install supervisor
pip3 install uwsgi  # uwsgi是个命令行工具,不需要在每个python虚拟环境安装

nginx

相关命令

sudo systemctl start nginx
sudo systemctl restart nginx
sudo systemctl stop nginx
sudo systemctl reload nginx  # 重新加载配置文件
sudo nginx -t  # 测试 nginx 配置文件是否正确

https://www.cnblogs.com/lianxuan1768/p/8383804.html

配置文件

默认配置文件在 /etc/nginx/nginx.conf

# 登录的用户,避免 nginx 用户无权限访问目录
user testhadoop;
worker_processes auto;
pid /var/log/nginx/nginx.pid;
http {
        ##
        # Virtual Host Configs
        ##
        # 包含 /etc/nginx/conf.d 下所有以 .conf 结尾的配置文件
        include /etc/nginx/conf.d/*.conf;
        #include /etc/nginx/sites-enabled/*;
        #    server {... 注释掉默认的 80 配置,我要在 conf.d 文件下自定义
}

/etc/nginx/conf.d/html.conf 静态站点

server {
    listen       80;
    server_name  172.22.145.101;
    charset utf-8;
    client_max_body_size 300M;
    location / {
        root  /home/testhadoop/www/html;
        index index.html index.htm;
    }
    location /reports {
        alias /home/testhadoop/www/reports/;
        index index.html index.htm;
    }
    location /files/ {
        proxy_pass http://172.22.145.101:7000/chfs/shared/files/;
    }
    error_page 404 /404.html;
        location = /40x.html {
    }

    error_page 500 502 503 504 /50x.html;
        location = /50x.html {
    }

}

配置静态 location 时注意 rootalias 两种的区别 参考

/etc/nginx/conf.d/httpRunnerLoan.conf uwsgi + flask 后台 api

server {
    # 因为前端请求的是8090端口
    listen  8090;
    server_name  172.22.145.101;
    charset utf-8;
    client_max_body_size 750M;
    location / {
        include uwsgi_params;
        uwsgi_param SCRIPT_NAME /webapi;
        uwsgi_pass 127.0.0.1:5001;
        uwsgi_param UWSGI_PYHONE /home/testhadoop/.local/share/virtualenvs/HttpRunnerLoan-web-ZW5cxY7L;
        uwsgi_param UWSGI_CHDIR /home/testhadoop/www/HttpRunnerLoan-web;
        uwsgi_param UWSGI_SCRIPT manage:app;
    }

    error_page 404 /404.html;
        location = /40x.html {
    }

    error_page 500 502 503 504 /50x.html;
        location = /50x.html {
    }

}

Nginx 默认日志位置

access_log /var/log/nginx/access.log
error_log /var/log/nginx/error.log

uwsgi

相关命令

uwsgi --ini /etc/uwsgi/httpRunnerLoan.ini

uwsgi 是一个命令行工具,多个项目可以共用同一个,所以不需要每个项目都按照一个 uwsgi,不同的项目用虚拟环境隔离,此时 uwsgi配置文件需要加上 home=you/python/path

配置文件

[uwsgi]
# uwsgi 启动时 socket 所使用的地址与端口
socket = 127.0.0.1:5001
#状态检测地址
stats = 127.0.0.1:5002

# 如果是虚拟环境,需要指定虚拟环境路径
home = /home/testhadoop/.local/share/virtualenvs/HttpRunnerLoan-web-ZW5cxY7L

# 启动主进程, 有主进程 supervisor 才能把所有的进程一起结束掉
master = true
# 在没有主进程的情况下自动结束工作进程
no-orphans = true
# 自动给uWSGI的进程设置一些有意义的名字,例如“uWSGI master”
auto-procname = true

# python 程序内用以启动的 application 变量名
callable = app
# 进程数
processes = 2
# 允许多线程,APScheduler 需要
enable-threads = true

# 规避使用pandas numpy 时报:RuntimeError: implement_array_function method already has a docstring
single-interpreter=true

# 运行标准输出到日志文件(如果通过supervisor查看日志,这里不需要重定向到日志文件,直接标准输出就好了)
logto = /home/testhadoop/www/HttpRunnerLoan-web/flask.log
# 请求日志(如果不想被请求日志干扰,可以把请求日志单独重定向到一个文件)
req-logger = file:/home/testhadoop/www/HttpRunnerLoan-web/uwsgi_req.log
# maxBytes=10485760=10M
log-maxsize = 10485760

mount=/webapi=manage.py
manage-script-name=true

supervisor

相关命令

echo_supervisord_conf > supervisord.conf  # 该命令可以输出一份默认的配置
supervisord -c supervisord.conf  # 指定配置启动
supervisorctl status  # 查看启动的进程状态
# 控制所有进程
supervisorctl start all
supervisorctl stop all
supervisorctl restart all
# 控制目标进程
supervisorctl stop httpRunnerLoan
supervisorctl start httpRunnerLoan
supervisorctl restart httpRunnerLoan

supervisorctl reread  # 重新读取配置
supervisorctl update  # 更新配置

supervisord 用非 root 用户启动若失败,大部分原因是配置中的地址没有读写权限, supervisor 错误集合

配置文件

注意所有配置的文件路径(如 file logfile pidfile files),你的启动使用的用户是否有对应的权限


[unix_http_server]
file=/tmp/supervisor.sock   ; the path to the socket file

[inet_http_server]          ; inet (TCP) server disabled by default 通过 web 端管理服务
port=*:9001                 ; ip_address:port specifier, *:port for all iface 

[supervisord]
logfile=/tmp/supervisord.log ; main log file; default $CWD/supervisord.log
logfile_maxbytes=50MB        ; max main logfile bytes b4 rotation; default 50MB
logfile_backups=10           ; # of main logfile backups; 0 means none, default 10
loglevel=info                ; log level; default info; others: debug,warn,trace
pidfile=/tmp/supervisord.pid ; supervisord pidfile; default supervisord.pid
nodaemon=false               ; start in foreground if true; default false
minfds=1024                  ; min. avail startup file descriptors; default 1024
minprocs=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

; 包含目录下的所有配置文件,可以放在一个目录 如:/etc/supervisor.d/
[include]
files = /etc/supervisord.d/*.ini

/etc/supervisord.d/httpRunnerLoan.ini 单个要启动的子进程配置

[program:httpRunnerLoan]
# 启动命令入口
command=/usr/local/bin/uwsgi --ini /etc/uwsgi/httpRunnerLoan.ini
# 命令程序所在目录
directory=/home/testhadoop/www/HttpRunnerLoan-web
#运行命令的用户名
user=testhadoop
autostart=true
autorestart=true
#日志地址(stderr日志也定向到stdout中)
redirect_stderr=true
stdout_logfile=/home/testhadoop/www/HttpRunnerLoan-web/flask.log

若果要添加环境变量

environment=PYTHONPATH=/opt/mypypath:%(ENV_PYTHONPATH)s,PATH=/opt/mypath:%(ENV_PATH)s

Supervisor 开启网络管理服务效果(对应配置[inet_http_server])

image

jenkins

相关命令

sudo systemctl start jenkins
sudo systemctl restart jenkins
sudo systemctl stop jenkins

jenkins运行需要 java环境,所以记得先配置好jdk环境变量

配置文件

默认配置文件路径/etc/sysconfig/jenkins

# 配置 jenkins home 目录,jenkins 配置和工作空间会在此,注意路径要有权限,后面备份或迁移可以打包该目录
JENKINS_HOME="/home/testhadoop/jenkins"

## Type:        string
## Default:     ""
## ServiceRestart: jenkins
#
# Java executable to run Jenkins
# When left empty, we'll try to find the suitable Java.
#
JENKINS_JAVA_CMD=""

## Type:        string
## Default:     "jenkins"
## ServiceRestart: jenkins
#
# Unix user account that runs the Jenkins daemon
# Be careful when you change this, as you need to update
# permissions of $JENKINS_HOME and /var/log/jenkins.
#
JENKINS_USER="testhadoop"

# 禁用jenkins自动清理工作目录(加入选项 -Dhudson.model.WorkspaceCleanupThread.disabled=true)
JENKINS_JAVA_OPTIONS="-Djava.awt.headless=true -Dhudson.model.WorkspaceCleanupThread.disabled=true"

使用普通用户运行jenkins时需要修改部分目录的权限, 如 log 默认放在var/log/jenkins

sudo chown -R testhadoop:testhadoop /var/log/jenkins
sudo chown -R testhadoop:testhadoop /var/cache/jenkins

第一次启动要去日志中看设置管理员的密钥

tail -f /var/log/jenkins/jenkins.log

批量替换插件下载地址为清华源,加速下载(可选)

因为网络原因,使用官方的 updates地址会比较慢,这里可以替换成清华源镜像地址,注意jenkins_home中的文件需首次启动jenkins后才会生成

cd /your/jenkins/home

#  jenkins_home下的 `updates/default.json` 使用 sed 批量修改插件地址为清华源镜像 
sed -i 's/http:\/\/updates.jenkins-ci.org\/download/https:\/\/mirrors.tuna.tsinghua.edu.cn\/jenkins/g' updates/default.json && sed -i 's/http:\/\/www.google.com/https:\/\/www.baidu.com/g' updates/default.json

# jenkins_home 中的 `hudson.model.UpdateCenter.xml` 地址可以改成清华源镜像地址 
sed -i 's/https:\/\/updates.jenkins.io\/update-center.json/https:\/\/mirrors.tuna.tsinghua.edu.cn\/jenkins\/updates\/update-center.json/g' hudson.model.UpdateCenter.xml

你可能感兴趣的:(Nginx + supervisor + uwsgi + flask 部署笔记)