[TOC]
graph LR
A[user Agent] -->B{Nginx}
B --> C1[Python 8001 server]
B --> C2[Python 8002 server]
B --> C3[Python 8003 server]
C1 -->|lb-8001| B
C2 -->|lb-8002| B
C3 -->|lb-8003| B
C1 --> D[Supervisor ]
C2 --> D[Supervisor]
C3 --> D[Supervisor]
B --> A
%% Example of sequence diagram
sequenceDiagram
User_Agent->>Nginx:
opt 8001 is well
Nginx->>User_Agent: 8001 return html content
end
opt 8002 is well
Nginx->>User_Agent: 8002 return html content
end
opt 8003 is well
Nginx->>User_Agent: 8003 return html content
end
没有 mermaid 插件
编辑好的Django 项目已经放到服务器上
Linux 上Python 和Pip 库都已经安装好
项目检查
1.Linux 项目目录
[root@iZ94571x3tzZ <projectName>]# tree -L 1
.
├── artical
├── <projectName>
├── db.sqlite3
├── exec
├── manage.py #django 项目开始文件
├── media
├── static
└── templates
2.服务跑起来
此处为Django 的运行脚本 ,Flask/Tornado 同样的原理
[root@iZ94571x3tzZ <projectName>] python manage.py runserver 0.0.0.0:8080
November 28, 2017 - 14:24:08
Django version 1.9.1, using settings '<projectSettings>.settings'
Starting development server at http://0.0.0.0:8080/
Quit the server with CONTROL-C.
这里有个井
[root@iZ94571x3tzZ <projectName>]# cat manage.py #!/usr/bin/env python 这里这是项目运行环境,并不是注释 import os .............
3.测试服务可用
curl localhost:8080
安装Supervisor
Supervisor 是基于 Python 的进程管理工具,可以帮助我们更简单的启动、重启和停止服务器上的后台进程,是 Linux 服务器管理的效率工具
1.安装Supervisorctl
easyinstall supervisorctl
or
pip install supervisorctl
echo_supervisord_conf > /etc/supervisord.conf
2.supervisor 自动启动
vim /usr/lib/systemd/system/supervisord.service
[Unit]
Description=Process Monitoring and Control Daemon
After=rc-local.service
[Service]
Type=forking
ExecStart=/usr/bin/supervisord -c /etc/supervisord.conf
SysVStartPriority=99
[Install]
WantedBy=multi-user.target
systemctl enable supervisord.service
3.supervisor配置文件
更改supervisord.conf 配置文件内容 ,以下行保持一致
vim /etc/supervisord.conf
> 22 [inet_http_server] ; inet (TCP) server disabled by default
> 23 port=127.0.0.1:9001 ; ip_address:port specifier, *:port for all iface
> 147 [include]
> 148 files = supervisord.d/*.ini
> 所有的目录 /tmp 更换为 /data/www/logs/supervisord
> 在Linux系统中/tmp文件夹里面的文件会被清空
4.定义项目的supervisor 配置文件
mkdir -p /etc/supervisord.d
vim /etc/supervisord.d/< projectName>.ini
[program:www-<prot>]
directory=/data/www/vhost/<projectName>/
command=python manage.py runserver 0.0.0.0:<prot> ; the program (relative uses PATH, can take args)
numprocs=1 ; number of processes copies to start (def 1)
autostart=true ; start at supervisord start (default: true)
autorestart=true ; when to restart if exited after running (def: unexpected)
user=apache ; setuid to this UNIX account to run the program
redirect_stderr=true ; redirect proc stderr to stdout (default false)
stdout_logfile=/data/www/logs/python_log/<projectName>_8011.log ; stdout log path, NONE for none; default AUTO
stderr_logfile=/data/www/logs/python_log/<projectName>_8011_err.log ; stdout log path, NONE for none; default AUTO
stdout_logfile_maxbytes=10MB ; max # logfile bytes b4 rotation (default 50MB)
stdout_logfile_backups=10
exitcodes=0,1,2
stopsignal=KILL
stopasgroup=true
killasgroup=true
#environment=ops_env="prod"
5.测试supervisor 是否启动正常
关掉之前的Python 运行脚本
使服务停止运行
[root@iZ94571x3tzZ ~]# supervisord #启动服务
能够正常访问说明supervisor 启动成功
**Nginx 需要使用这个 URL**
[root@iZ94571x3tzZ ~]# curl localhost:8080
6.其他命令
[root@vm172-31-32-9 ~]# supervisorctl status all
<supervisor_name> RUNNING pid 18138, uptime 4:28:03
[root@vm172-31-32-9 ~]# supervisorctl reload
Restarted supervisord
[root@vm172-31-32-9 ~]# supervisorctl stop <supervisor_name>
<supervisor_name>: stopped
[root@vm172-31-32-9 ~]# supervisorctl restart all
<supervisor_name>: stopped
<supervisor_name>: started
安装 Nginx
Nginx是一个Web服务器,也可以用作反向代理,负载平衡器和 HTTP缓存。该软件由Igor Sysoev 创建,并于2004年首次公开发布。同名公司成立于2011年,以提供支持。 Nginx 是免费的开源软件,根据类似BSD许可证的条款发布
1.安装Nginx 并自动启动
yum install nginx
chkconfig nginx on
2.测试Nginx 是否启动
curl < host>
3.编辑项目配置文件
vim /etc/nginx/conf.d/<project_name>.conf
server {
listen 80;
server_name <host_name>;
access_log /data/www/logs/nginx_log/<project_name>_access.log main ;
error_log /data/www/logs/nginx_log/<project_name>_error.log ;
root <project_path>;
index index.html;
location / {
expires -10d;
#try_files $uri $uri/ /index.html;
proxy_pass_header Server;
proxy_redirect off;
proxy_pass http://<ip>:<port> ;
}
}
4.重启Nginx
sudo nginx -s reload
如果有问题,自己google 之
5.测试 Nginx 是否转发成功
curl < project_domain>
![github]: https://github.com/wyl
[Write By ]: 2017/11/28