2020年3月30日 星期一 雨
环境:CentOS,1CPU(双核) 关键词:Tornado + supervisor + nginx + logrotate
一、CENTOS安装Python3(2019年7月安装过程) 查看CentOS版本号,7.6 cat /etc/redhat-release
用yum时,nginx提示404,参考https://blog.csdn.net/qq_34625397/article/details/80747617
-
安装编译工具 pip install –upgrade pip pip install wget yum install yum-utils yum -y groupinstall “Development tools” yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel yum install libffi-devel -y
-
下载python3.7 cd #回到用户目录 wget https://www.python.org/ftp/python/3.7.4/Python-3.7.4.tar.xz
-
编译 tar -xvJf Python-3.7.4.tar.xz mkdir /usr/local/python3 cd Python-3.7.4 ./configure –prefix=/usr/local/python3 ./configure –enable-optimizations make && make install
-
软链接 ln -s /usr/local/python3/bin/python3 /usr/local/bin/python3 ln -s /usr/local/python3/bin/pip3 /usr/local/bin/pip3
-
测试 python3 -V pip3 -V
二、安装Virtualenv pip install virtualenv
三、创建python3虚拟环境 mkdir PY3ENV virtualenv -p /usr/local/bin/python3 PY3ENV 进入python3虚拟环境:source PY3ENV/bin/activate 退出python3虚拟环境:deactivate
安装各种python的依赖包。
四、Tornado的main.py文件 pip install tornado
[code] (略) tornado.options.define(‘port’, default=9000, type=int)
def main(): (略) tornado.options.parse_command_line() app = make_app() http_server = tornado.httpserver.HTTPServer(app) http_server.listen(tornado.options.options.port) tornado.ioloop.IOLoop.instance().start() [/code]
五、supervisor相关(管理Tornado进程) yum install epel-release yum install -y supervisor systemctl enable supervisord
vi /etc/supervisord.conf [code]
为了方便管理,增加一个tornado组
[group:tornados] programs=tornado-0,tornado-1
分别定义两个tornado的进程配置
[program:tornado-0]
进程要执行的命令
command=/root/PY3ENV/bin/python /root/scrm/main.py –port=9000 directory=/root/scrm/ user=root autorestart=true redirect_stderr=true stdout_logfile=/root/scrm/log/tornado0.log loglevel=info
no limit on the size
stdout_logfile_maxbytes=0 stderr_logfile_maxbytes=0
no backup with supervisor
stdout_logfile_backups=0 stderr_logfile_backups=0
[program:tornado-1] command=/root/PY3ENV/bin/python /root/scrm/main.py –port=9001 directory=/root/scrm/ user=root autorestart=true redirect_stderr=true stdout_logfile=/root/scrm/log/tornado1.log loglevel=info
no limit on the size
stdout_logfile_maxbytes=0 stderr_logfile_maxbytes=0
no backup with supervisor
stdout_logfile_backups=0 stderr_logfile_backups=0 [/code]
相关指令: unlink /var/run/supervisor/supervisor.sock #有时报错,先执行这个 supervisord -c /etc/supervisord.conf supervisorctl status
六、logrotate管理Log vi /etc/logrotate.d/tornados 内容如下:其中第一行是log的路径 [code] /root/scrm/log/tornado*.log { daily rotate 30 dateext dateyesterday copytruncate delaycompress compress missingok notifempty } [/code]
logrotate /etc/logrotate.d/tornados logrotate -d /etc/logrotate.d/tornados logrotate -vf /etc/logrotate.d/tornados
七、nginx配置 sudo yum install nginx #启动 sudo systemctl start nginx #停止 sudo systemctl stop nginx #重启 sudo systemctl restart nginx #查看状态 sudo systemctl status nginx
/etc/nginx/nginx.conf [code] include /etc/nginx/conf.d/*.conf;
upstream tornadoes {
server 127.0.0.1:9000;
server 127.0.0.1:9001;
}
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
server {
listen 80 default_server;
listen [::]:80 default_server;
listen 443 ssl;
#listen 443 default_server;
#listen [::]:443 default_server;
server_name xxx.xxx.com;
ssl on;
ssl_certificate /etc/nginx/ssl/uegdental.com.key.pem;
ssl_certificate_key /etc/nginx/ssl/uegdental.com.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_dhparam /etc/nginx/ssl/dhparam.pem;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme; # 协议 http https
proxy_pass http://tornadoes;
proxy_http_version 1.1;
proxy_connect_timeout 4s;
proxy_read_timeout 600s;
proxy_send_timeout 600s;
proxy_set_header Upgrade $http_upgrade;#支持websocket
proxy_set_header Connection "Upgrade";#支持websocket
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
[/code]
八、防火墙相关
-
内部防火墙 查看端口:firewall-cmd –zone=public –list-ports 添加端口:firewall-cmd –zone=public –add-port=9100/tcp –permanent 停止防火墙:systemctl stop firewalld.service
-
云防火墙 如果要外网能访问的话,要在云上配置
九、压力测试 yum -y install httpd-tools 查看版本命令:ab -V 查看参数说明:ab –help 常用方式:ab -c 500 -n 5000 http://localhost/
...