python版本: python 2.7.5
一、 nginx安装
1. 下载
wget http://nginx.org/download/nginx-1.10.2.tar.gz
tar -xvf nginx-1.10.2.tar.gz
yum -y install pcre-devel openssl openssl-devel
2. 安装
cd nginx-1.10.2
./configure \
–prefix=/usr/local/nginx \
–with-http_realip_module \
–with-http_sub_module\
–with-http_gzip_static_module \
–with-http_stub_status_module\
–with-pcre \
–with-http_ssl_module
启动:
/usr/local/nginx/sbin/nginx
重启:
/usr/local/nginx/sbin/nginx -s reload
3. 配置
确保80端口没有被占用
netstat -ano|grep 80
打开配置文件
vim /usr/local/nginx/nginx.conf
server {
listen 80;
server_name localhost;
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:5010;
root html;
index index.html index.htm;
}
}
二、 uwsgi安装
pip install uwsgi 安装uwsgi
pip install uwsgi -I –no-cache-dir 安装失败可以尝试此方法
pip uninstall uwsgi 卸载
配置:/application/portal_api/uwsgi_config.ini (flask下面)
[uwsgi]
socket = 127.0.0.1:5000
processes = 2
threads = 2
master = true
pythonpath = /application/portal_api
module = run #flask的启动文件名称
callable = app
memory-report = true
记得进入:
source venv/bin/activate
启动uwsgi(本框架不需要手动启动):
uwsgi –ini /application/portal_api/uwsgi_config.ini -d /var/log/uwsgi.log
三、supervisor守护进程
安装supervisord
source py2.7_venv/bin/activate
pip install supervisor
生成配置文件
echo_supervisord_conf > /etc/supervisord.conf
修改配置文件, 添加托管项目:
[program:trident]
user=root
process_name = %(program_name)s
directory=/application/trident
command = /application/venv/bin/uwsgi –ini /application/trident/nginx_trident_socket.ini #注意, 不能-d 指定日志输出目录 涉及精灵进程
autostart=true
autorestart=true
redirect_stderr=true
startretries=5
stderr_logfile=/application/trident/logs/trident_err.log
stderr_logfile_maxbytes=200MB
stopsignal=QUIT
启动supervisor
supervisord
supervisorctl
start all
四、访问
网页直接输入 ip地址(默认80), 如果nginx监听6000 则 ip:6000
测试端口
telnet 139.129.225.106 6000
五、说明
- supervisor代理了uwsgi启动
- uwsgi作为底层和服务器之间的信息交换中间层,增加客户端响应.
- uwsgi 代理了flask的启动
- nginx作为server端,将请求抛向uwsgi
- 框架只需要启动nginx和supervisor