Varnish+nginx+mysql+php 环境搭建(一) 转载(lamppr)

说明:
我在设计系统架构时,进行了大胆的尝试,只用6台Web服务器,达到了可承受4000万PV(页面访问量)的性能:

抛弃了 Apache,因为它能承受的并发连接相对较低;
抛弃了 Squid,因为它在内存利用、访问速度、并发连接、清除缓存等方面不如 Varnish;
抛弃了 PHP4,因为 PHP5 处理面向对象代码的速度要比 PHP4 快,另外,PHP4 已经不再继续开发;
抛弃了 F5 BIG-IP 负载均衡交换机,F5 虽然是个好东西,但由于价格不菲,多个部门多个产品都运行在其之上,流量大、负载高,从而导致性能大打折扣;

利用 Varnish cache 减少了90%的数据库查询,解决了MySQL数据库瓶颈;
利用 Varnish cache 的内存缓存命中加快了网页的访问速度;
利用 Nginx + PHP5(FastCGI) 的胜过Apache 10倍的高并发性能,以最少的服务器数量解决了PHP动态程序访问问题;
利用 Memcached 处理实时数据读写;
利用 HAProxy 做接口服务器健康检查;

经过压力测试,每台Web服务器能够处理3万并发连接数,承受4千万PV完全没问题。

保证4千万PV的并发连接数:555-5555PV / 86400秒 * 10个派生连接数 * 5秒内响应 * 5倍峰值) / 6台Web服务器 = 19290连接数

实验证明:

举个简单的例子,服务器192.168.0.2上运行Nginx+PHP,192.168.0.3上运行Apache+PHP,你在 192.168.0.4上安装压力测试工具webbench,以30万并发连接分别请求Nginx和Apache服务器上的一个PHP文件60秒钟。在这 期间,你用你的浏览器访问Apache服务器上的PHP文件,会发现要么是“该页无法显示”、要么是等待好几秒钟才能打开,而Nginx服务器的PHP文 件,依然没有丝毫影响,访问速度仍然飞快。

webbench -c 300000 -t 60 http://192.168.0.2/index.php

webbench -c 300000 -t 60 http://192.168.0.3/index.php

以下为 Nginx 0.5.33 + PHP 5.2.5 (FastCGI) 服务器在3万并发连接下,开启的10个Nginx进程和250个php-cgi进程时的系统负载情况:

nginx2

安装步骤:
(系统要求:Linux 2.6+ 内核,本文中的Linux操作系统为AS4.3)

一、获取相关开源程序:
1、下载程序源码包到当前目录:
本文中提到的所有开源软件为截止到2007年9月21日的最新稳定版。我将它们打了两个压缩包。

第一个压缩包:nginx_php_mysql_1.0_1of2.zip:
下载地址:http://ishare.iask.sina.com.cn/cgi-bin/fileid.cgi?fileid=555-5555

第二个压缩包:nginx_php_mysql_1.0_2of2.zip:
下载地址:http://ishare.iask.sina.com.cn/cgi-bin/fileid.cgi?fileid=555-5555

2、解压缩:

unzip nginx_php_mysql_1.0_1of2.zip
unzip nginx_php_mysql_1.0_2of2.zip

一、) 安装Nginx
1.) 安装
Nginx (”engine x”) 是一个高性能的 HTTP 和反向代理服务器,也是一个 IMAP/POP3/SMTP 代理服务器。 Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的,它已经在该站点运行超过两年半了。Igor 将源代码以类BSD许可证的形式发布。尽管还是测试版,但是,Nginx 已经因为它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名了。欢迎访问 Nginx 的中文维基,http://wiki.codemongers.com/NginxChs.

2.)安装Nginx所需的pcre库:

[xxxxx@xxxxx.com]#tar zxvf pcre-7.2.tar.gz
[xxxxx@xxxxx.com]#cd pcre-7.2/
[xxxxx@xxxxx.com]#./configure
[xxxxx@xxxxx.com]#make && make install
[xxxxx@xxxxx.com]#cd ../
3.)Nginx的编译参数如下:
[xxxxx@xxxxx.com]#./configure –user=www –group=www –prefix=/usr/local/nginx –with-openssl=/usr/include/openssl –with-pcre=/usr/local/lib –with-http_stub_status_module –without-http_memcached_module –without-http_fastcgi_module –without-http_rewrite_module –without-http_map_module –without-http_geo_module –without-http_autoindex_module

在这里,需要说明一下,由于Nginx的配置文件中我想用到正则,所以需要 pcre 模块的支持。上面安装步骤里我已经安装了 pcre 及 pcre-devel 的rpm包,但是 Ngxin 并不能正确找到 .h/.so/.a/.la 文件,因此我稍微变通了一下:

[xxxxx@xxxxx.com]#mkdir /usr/include/pcre/.libs/
[xxxxx@xxxxx.com]#cp /usr/local/lib/libpcre.a /usr/include/pcre/.libs/libpcre.a
[xxxxx@xxxxx.com]#cp /usr/local/lib/libpcre.a /usr/include/pcre/.libs/libpcre.la
[xxxxx@xxxxx.com]#cp /usr/local/lib/libpcre.a /usr/include/pcre/libpcre.a
[xxxxx@xxxxx.com]#cp /usr/local/lib/libpcre.a /usr/include/pcre/libpcre.la
然后,修改 objs/Makefile 大概在908行的位置上,注释掉以下内容:

./configure –disable-shared

接下来,就可以正常执行 make 及 make install 了。

4.) 修改配置文件 /usr/local/server/nginx/conf/nginx.conf
以下是我的 nginx.conf 内容,仅供参考:

#运行用户
user nobody nobody;

#启动进程
worker_processes 2;

#全局错误日志及PID文件
error_log logs/error.log notice;
pid logs/nginx.pid;

#工作模式及连接数上限
events {
use epoll;
worker_connections 1024;
}

#设定http服务器,利用它的反向代理功能提供负载均衡支持
http {
#设定mime类型
include conf/mime.types;
default_type application/octet-stream;

#设定日志格式
log_format main ‘$remote_addr – $remote_user [$time_local] ‘
‘”$request” $status $bytes_sent ‘
‘”$http_referer” “$http_user_agent” ‘
‘”$gzip_ratio”‘;

log_format download ‘$remote_addr – $remote_user [$time_local] ‘
‘”$request” $status $bytes_sent ‘
‘”$http_referer” “$http_user_agent” ‘
‘”$http_range” “$sent_http_content_range”‘;

#设定请求缓冲
client_header_buffer_size 1k;
large_client_header_buffers 4 4k;

#开启gzip模块
gzip on;
gzip_min_length 1100;
gzip_buffers 4 8k;
gzip_types text/plain;

output_buffers 1 32k;
postpone_output 1460;

#设定access log
access_log logs/access.log main;

client_header_timeout 3m;
client_body_timeout 3m;
send_timeout 3m;

sendfile on;
tcp_nopush on;
tcp_nodelay on;

keepalive_timeout 65;

#设定负载均衡的服务器列表
upstream mysvr {
#weigth参数表示权值,权值越高被分配到的几率越大
#本机上的Squid开启3128端口
server 192.168.8.1:3128 weight=5;
server 192.168.8.2:80 weight=1;
server 192.168.8.3:80 weight=6;
}

#设定虚拟主机
server {
listen 80;
server_name 192.168.0.1 www.test.com;

charset gb2312;

#设定本虚拟主机的访问日志
#access_log logs/access.log main;

#如果访问 /img/*, /js/*, /css/* 资源,则直接取本地文件,不通过squid
#如果这些文件较多,不推荐这种方式,因为通过squid的缓存效果更好
location ~ ^/(img|js|css)/ {
root /home/web;
expires 24h;
}

#对 “/” 启用负载均衡
location / {
proxy_pass http://mysvr;

proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}

#设定查看Nginx状态的地址
location /NginxStatus {
stub_status on;
access_log on;
auth_basic “NginxStatus”;
auth_basic_user_file conf/htpasswd;
}
}
}

运行以下命令检测配置文件是否无误:

如果没有报错,那么就可以开始运行Nginx了,执行以下命令即可:
备注:conf/htpasswd 文件的内容用 apache 提供的 htpasswd 工具来产生即可,内容大致如下:

5.) 查看 Nginx 运行状态
nginx3
输入地址 http://192.168.0.1/NginxStatus/,输入验证帐号密码,即可看到类似如下内容:

Active connections: 328
server accepts handled requests
555-5555 28890
Reading: 1 Writing: 3 Waiting: 324

nginx4

这里我是用虚拟机做的测试,所以链接数比较少.
第一行表示目前活跃的连接数
第三行的第三个数字表示Nginx运行到当前时间接受到的总请求数,如果快达到了上限,就需要加大上限值了。
top -b -nl
查看NGINX的进程号:
netstat -n | awk ‘/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}’

6.)配置支持FCGI文件
vi /usr/local/webserver/nginx/conf/fcgi.conf

输入以下内容:
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx;

fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;

fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;

# PHP only, required if PHP was built with –enable-force-cgi-redirect
#fastcgi_param REDIRECT_STATUS 200;

7.)启动Nginx

ulimit -SHn 51200
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

8.)配置开机自动启动Nginx + PHP

vi /etc/rc.local

在末尾增加以下内容:

/usr/local/php/bin/spawn-fcgi -a 127.0.0.1 -p 10080 -C 250 -u www -f /usr/local/php/bin/php-cgi
ulimit -SHn 51200
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

posted on 2012-04-11 11:13  mashuaimama  阅读(315)  评论(0编辑  收藏  举报

导航