架構 Varnish+nginx+php(FastCGI)+MYSQL5+MenCache+MenCachedbphp
說明:
我在設計系統架構時,進行了大膽的嘗試,只用6臺Web服務器,達到了可承受4000萬PV(頁面訪問量)的性能:css
拋棄了 Apache,由於它能承受的併發鏈接相對較低;
拋棄了 Squid,由於它在內存利用、訪問速度、併發鏈接、清除緩存等方面不如 Varnish;
拋棄了 PHP4,由於 PHP5 處理面向對象代碼的速度要比 PHP4 快,另外,PHP4 已經再也不繼續開發;
拋棄了 F5 BIG-IP 負載均衡交換機,F5 雖然是個好東西,但因爲價格不菲,多個部門多個產品都運行在其之上,流量大、負載高,從而致使性能大打折扣;html
利用 Varnish cache 減小了90%的數據庫查詢,解決了MySQL數據庫瓶頸;
利用 Varnish cache 的內存緩存命中加快了網頁的訪問速度;
利用 Nginx + PHP5(FastCGI) 的賽過Apache 10倍的高併發性能,以最少的服務器數量解決了PHP動態程序訪問問題;
利用 Memcached 處理實時數據讀寫;
利用 HAProxy 作接口服務器健康檢查;node
通過壓力測試,每臺Web服務器可以處理3萬併發鏈接數,承受4千萬PV徹底沒問題。mysql
保證4千萬PV的併發鏈接數:(40000000PV / 86400秒 * 10個派生鏈接數 * 5秒內響應 * 5倍峯值) / 6臺Web服務器 = 19290鏈接數nginx
實驗證實:
舉個簡單的例子,服務器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文件,依然沒有絲毫影響,訪問速度仍然飛快。web
webbench -c 300000 -t 60 http://192.168.0.2/index.phpsql
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進程時的系統負載狀況:apache
-------------------------------------------------------------------------
安裝步驟:
(系統要求: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=2289607
第二個壓縮包:nginx_php_mysql_1.0_2of2.zip:
下載地址:http://ishare.iask.sina.com.cn/cgi-bin/fileid.cgi?fileid=2289595
二、解壓縮:
unzip nginx_php_mysql_1.0_1of2.zip
unzip nginx_php_mysql_1.0_2of2.zip
-------------------------------------------------------------------------
1、) 安裝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庫:
[root@localhost]#tar zxvf pcre-7.2.tar.gz
[root@localhost]#cd pcre-7.2/
[root@localhost]#./configure
[root@localhost]#make && make install
[root@localhost]#cd ../
3.)Nginx的編譯參數以下:
[root@localhost]#./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 文件,所以我稍微變通了一下:
[root@localhost]#mkdir /usr/include/pcre/.libs/
[root@localhost]#cp /usr/local/lib/libpcre.a /usr/include/pcre/.libs/libpcre.a
[root@localhost]#cp /usr/local/lib/libpcre.a /usr/include/pcre/.libs/libpcre.la
[root@localhost]#cp /usr/local/lib/libpcre.a /usr/include/pcre/libpcre.a
[root@localhost]#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 運行狀態
輸入地址 http://192.168.0.1/NginxStatus/,輸入驗證賬號密碼,便可看到相似以下內容:
Active connections: 328
server accepts handled requests
9309 8982 28890
Reading: 1 Writing: 3 Waiting: 324
這裏我是用虛擬機作的測試,因此連接數比較少.
第一行表示目前活躍的鏈接數
第三行的第三個數字表示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
-------------------------------------------------------------------------