• 隐藏侧边栏
  • 展开分类目录
  • 关注微信公众号
  • 我的GitHub
  • QQ:1753970025
Chen Jiehua

Ubuntu下搭建Nginx+supervisor+pypy+virtualenv 

系统:Ubuntu 14.04 LTS

搭建python的运行环境:Nginx+Supervisor+Pypy+Virtualenv

软件说明:

Nginx:通过upstream进行负载均衡

Supervisor:管理python进程

Pypy:用Python实现的Python解释器

PyPy is a fast, compliant alternative implementation of the Python language (2.7.6 and 3.2.3).

Virtualenv:搭建python虚拟环境

 环境搭建:

安装pypy:

sudo apt-get install pypy 

安装virtualenv:

sudo apt-get install virtualenv 

创建pypy+virtualenv虚拟环境:

#在当前目录下创建PYPYENV
virtualenv -p /usr/bin/pypy PYPYENV
cd PYPYENV

导出原有 python 安装的包列表:

pip freeze > list.txt 

进入virtualenv(PYPYENV)虚拟环境,同时导入list.txt列表:

source bin/activate
pip install -r list.txt

退出virtualenv虚拟环境:

deactivate

安装 supervisor:

sudo apt-get install supervisor 

配置supervisor:

vim /etc/supervisor/supervisord.conf 

添加


[inclue]
files=conf.d/*.conf

vim /etc/supervisor/conf.d/xxxx.conf

代码示例:


[program:blog]
command= runinenv.sh /pathaa/PYPYENV python /pathbb/xxx.py --port=80%(process_num)02d --log_file_prefix=/tmp/xxx-80%(process_num)02d.log
directory=/path
numprocs=4
process_name=%(program_name)s-80%(process_num)02d
autostart=true
autorestart=true
startsecs=3
stdout_logfile=/tmp/xxx.log
stderr_logfile=/tmp/xxxerror.log

runinenv.sh文件代码示例:

#!/bin/bash
VENV=$1
if [ -z $VENV ]; then
echo "usage:runinenv [virtualenv_path] CMDS"
exit 1
fi
source ${VENV}/bin/activate
shift 1
echo "Executing $@ in ${VENV}"
exec "$@"
deactivate

supervisor管理

启动:

sudo supervisord 

管理:

sudo supervisorctl

supervisord > reload
supervisord > status
supervisord > start
supervisord > stop

 安装Nginx:

 sudo apt-get install nginx sudo /etc/init.d/nginx start 

浏览器访问localhost即可看到Welcome to Nginx。

修改nginx配置:

vim /etc/nginx/sites-available/default
#在http中添加
upstream xxxx{
ip_hash;
server localhost:8001;
server localhost:8002;
server localhost:8003;
server localhost:8004;
……
}

#在server中修改:
location / {
proxy_pass http://xxxx
}

nginx压力测试:


sudo apt-get install apache2-utils

# 并发数1000,总请求数10000
ab -c 1000 -n 10000 http://localhost/

 

 参考资料:

1 、使用 ab 进行测试 nginx 时,结果发现 Failed requests 很大,查看日志( /var/log/nginx/error.log ),发现错误是: socket() failed (24: Too many open files),参考这里

2、Nginx中的一些优化

参考nginx wiki这里

 

码字很辛苦,转载请注明来自ChenJiehua《Ubuntu下搭建Nginx+supervisor+pypy+virtualenv》

评论