互連網創業的準備——web server:apache、nginx、lighttpd與php module、fastcgi

來源:互聯網
上載者:User

測試環境:

vr.org 的 VPS

CPU:2核共用

記憶體:512M獨立

OS:Ubuntu 12.04 LTS x64

uname -a

u1@www:~$ cat /etc/lsb-release DISTRIB_ID=UbuntuDISTRIB_RELEASE=12.04DISTRIB_CODENAME=preciseDISTRIB_DESCRIPTION="Ubuntu 12.04 LTS"u1@www:~$ uname -aLinux www.shaixuan.org 3.4.0-cloud #1 SMP Thu May 24 04:54:53 EDT 2012 x86_64 x86_64 x86_64 GNU/Linuxu1@www:~$ toptop - 23:57:39 up  1:38,  2 users,  load average: 0.04, 0.03, 0.16Tasks:  55 total,   1 running,  54 sleeping,   0 stopped,   0 zombieCpu(s):  0.7%us,  0.4%sy,  0.0%ni, 97.6%id,  0.1%wa,  0.0%hi,  0.1%si,  1.1%stMem:    506764k total,   125264k used,   381500k free,     7736k buffersSwap:   499708k total,        0k used,   499708k free,    70096k cached

軟體版本:Apache httpd 2.4.3、nginx 1.2.3、lighttpd 1.4.31、php 5.4.6

 

結論:

靜態伺服器:

並發:nginx(17000)> lighttpd(14000)> apache(5000)

注意:lighttpd開啟gzip壓縮時,無法禁用etag……不建議使用。參考:《高效能網站建設指南》

 

動態伺服器:

大約並發:nginx + php-fpm(1500)> lighttpd + spawn-fcgi(1000)>apache + php module(400)

注意:web server與fastcgi在同一個機器時,建議使用unix domain socket,比tcp socket效果好一點點。

 

雖然apache httpd 2.4 像 nginx一樣使用了epoll,但是效能還是比nginx低很多。

伺服器OS一定要使用Linux 2.6核心及更高,因為才能支援epoll。

建議伺服器使用Ubuntu server LTS x64,因為工程師PC使用Ubuntu x64較多(Ubuntu的顯卡驅動安裝方便),這樣編譯部署都一致。

Ubuntu、CentOS等各個Linux發行版對伺服器來說沒有區別,核心都一樣,挑一個順手的即可。

 

參考互連網公司的web server:

163.com 靜態nginx
t.qq.com 靜態squid,動態nginx
taobao.com 靜態Tengine,動態Tengine(nginx衍生版)
小米論壇 靜態Tengine,動態Tengine
百度 靜態lighttpd,動態lighttpd(有etag)

 

 

 

測試過程:

Apache httpd 2.4.3

編譯參數:

apache

./configure --prefix=/usr/local/apache2 --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util/ --with-pcre=/home/u1/pcre-8.30 --enable-so --enable-rewrite --enable-deflate --with-module=metadata:expires 

MPM:預設event

httpd -V

root@www:/usr/local/apache2/bin# ./httpd -VServer version: Apache/2.4.3 (Unix)Server built:   Aug 26 2012 10:27:04Server's Module Magic Number: 20120211:6Server loaded:  APR 1.4.6, APR-UTIL 1.4.1Compiled using: APR 1.4.6, APR-UTIL 1.4.1Architecture:   64-bitServer MPM:     event  threaded:     yes (fixed thread count)    forked:     yes (variable process count)Server compiled with.... -D APR_HAS_SENDFILE -D APR_HAS_MMAP -D APR_HAVE_IPV6 (IPv4-mapped addresses enabled) -D APR_USE_SYSVSEM_SERIALIZE -D APR_USE_PTHREAD_SERIALIZE -D APR_HAS_OTHER_CHILD -D AP_HAVE_RELIABLE_PIPED_LOGS -D DYNAMIC_MODULE_LIMIT=256 -D HTTPD_ROOT="/usr/local/apache2" -D SUEXEC_BIN="/usr/local/apache2/bin/suexec" -D DEFAULT_PIDLOG="logs/httpd.pid" -D DEFAULT_SCOREBOARD="logs/apache_runtime_status" -D DEFAULT_ERRORLOG="logs/error_log" -D AP_TYPES_CONFIG_FILE="conf/mime.types" -D SERVER_CONFIG_FILE="conf/httpd.conf"

預設並發400,MaxRequestWorkers即以前的MaxClients,參考:http://httpd.apache.org/docs/2.4/mod/mpm_common.html#MaxRequestWorkers

<IfModule mpm_event_module>    StartServers             3       MinSpareThreads         75      MaxSpareThreads        250     ThreadsPerChild         25      MaxRequestWorkers      400     MaxConnectionsPerChild   0   </IfModule>

並發改成40000,提示錯誤,加上ServerLimit即可:

u1@www:~$ sudo vi /usr/local/apache2/conf/extra/httpd-mpm.conf u1@www:~$ sudo vi /usr/local/apache2/conf/httpd.confu1@www:~$ sudo /etc/init.d/apache2 restartAH00515: WARNING: MaxRequestWorkers of 40000 would require 1600 servers and  would exceed ServerLimit of 16, decreasing to 400. To increase, please see the ServerLimit directive.

 

nginx 1.2.3:

編譯參數:

View Code

./configure --prefix=/usr/local/nginx --with-pcre=/usr/local/pcre

 

lighttpd 1.4.31:

預設並發1024,server.max-fds最大可修改為65535,server.max-connections最大可修改為32767:

193 ##194 ## As lighttpd is a single-threaded server, its main resource limit is195 ## the number of file descriptors, which is set to 1024 by default (on196 ## most systems).197 ##198 ## If you are running a high-traffic site you might want to increase this199 ## limit by setting server.max-fds.200 ##201 ## Changing this setting requires root permissions on startup. see202 ## server.username/server.groupname.203 ##204 ## By default lighttpd would not change the operation system default.205 ## But setting it to 2048 is a better default for busy servers.206 ##207 server.max-fds = 2048218 219 ##220 ## Fine tuning for the request handling221 ##222 ## max-connections == max-fds/2 (maybe /3)223 ## means the other file handles are used for fastcgi/files224 ##225 server.max-connections = 1024

 

 

1、靜態伺服器測試

即apache、nginx、lighttpd效能對比。

伺服器相同配置 :

開啟gzip、關閉etag

測試程式和web server在同一台伺服器上,測試命令:

ab -c 1000 -n 50000 http://localhost/index.htmlsiege -c 1000 -r 20 http://localhost/index.html

 

apache:html檔案13k,gzip壓縮為4.9k

header:

apache

測試結果:

並發  httpd子進程數 整機512M記憶體使用量率 CPU使用率 iowait
0 3 19% 0 0
1000 10 30% 38% 0
3000 14 60% 38% 0
5000 10-30 65%-99% 20%-40% 0-30
8000 100 99% 3%-50% 20-100

 

 

nginx:html檔案13.2k,gzip壓縮為5.34k

header:

nginx

Request URL:http://shaixuan.org/index.htmlRequest Method:GETStatus Code:200 OKRequest Headersview sourceAccept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8Accept-Charset:UTF-8,*;q=0.5Accept-Encoding:gzip,deflate,sdchAccept-Language:zh-CN,zh;q=0.8,en-US;q=0.6,en;q=0.4Cache-Control:no-cacheConnection:keep-aliveHost:shaixuan.orgPragma:no-cacheUser-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.8 (KHTML, like Gecko) Chrome/23.0.1251.2 Safari/537.8Response Headersview sourceConnection:keep-aliveContent-Encoding:gzipContent-Type:text/htmlDate:Mon, 03 Sep 2012 13:19:23 GMTLast-Modified:Mon, 03 Sep 2012 12:33:45 GMTServer:nginx/1.2.3Transfer-Encoding:chunked

測試結果:

 並發 整機512M記憶體使用量率 CPU使用率 iowait
0 29% 0 0
1000 35% 40% 0
3000 46% 43% 0
5000 55% 42% 0-1
8000 65% 45% 0
15000 88% 45% 0
17000 97% 47% 0

 

lighttpd:html檔案13k,gzip壓縮為4.96k

header:

lighttpd

Request URL:http://shaixuan.org/Request Method:GETStatus Code:200 OKRequest Headersview sourceAccept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8Accept-Charset:UTF-8,*;q=0.5Accept-Encoding:gzip,deflate,sdchAccept-Language:zh-CN,zh;q=0.8,en-US;q=0.6,en;q=0.4Cache-Control:max-age=0Connection:keep-aliveHost:shaixuan.orgPragma:no-cacheUser-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.8 (KHTML, like Gecko) Chrome/23.0.1251.2 Safari/537.8Response Headersview sourceAccept-Ranges:bytesContent-Encoding:gzipContent-Length:4819Content-Type:text/htmlDate:Tue, 04 Sep 2012 12:51:42 GMTETag:"1028714895"Last-Modified:Tue, 04 Sep 2012 12:29:10 GMTServer:lighttpd/1.4.31Vary:Accept-Encoding

測試結果:

 並發 整機512M記憶體使用量率 CPU使用率 iowait
0 29% 0 0
1000     0
3000 46% 41% 0-6
5000 60% 40% 0-1
8000 75% 45% 0
12000 81% 35% 0
14000 87% 36% 0
16000 99% 40% 0

 

 

2、動態伺服器測試

即apache + php module、nginx + php-fpm、nginx + php-cgi、lighttpd + spawn-fcgi對比。

測試命令:

siege -c 1200 -r 20 http://localhost/phpinfo.php/usr/local/apache2/bin/ab -c 100 -t 50 http://localhost/phpinfo.php

 

 

php版本 5.4.6,測試代碼phpinfo();

 

apache + php module:php檔案63.57k,gzip壓縮為11.21k

測試結果:

 並發 整機512M記憶體使用量率 CPU使用率 iowait
0 29% 0 0
100 68% 8% 0
200 72% 10% 0
400 95% 10% 0
600 99% 15% 1-15

 

nginx + php-fpm(多個php-fpm子進程):php檔案57.41k,gzip壓縮為10.84k

phpinfo顯示Server API:FPM/FastCGI

fastcgi的socket方式:tcp socket

php編譯參數:

php-fpm

./configure --prefix=/usr/local/php-fpm --enable-fpm --with-fpm-user=www-data --with-fpm-group=www-data --with-curl --with-mcrypt --enable-mbstring --enable-pdo --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --enable-zip

測試結果:

 並發 整機512M記憶體使用量率 CPU使用率 iowait siege成功率
0 33% 0 0  
100 37% 8% 0 100%
200        
400 45% 10% 0 99.96%
800 55% 14個php-fpm,每個5.9M 12% 0 96.31%
1000 60% 10% 0 90.77%
1200 69%,Linux file限制,測試出錯 30%    

 

nginx + php-fpm(多個php-fpm子進程):php檔案54.57k,gzip壓縮為9.16k

phpinfo顯示Server API:FPM/FastCGI

fastcgi的socket方式:unix domain socket

測試結果:

 並發 整機512M記憶體使用量率 CPU使用率 iowait siege成功率
0 32% 0 0  
100 35% 7% 0 100%
200 40% 12% 0 100%
400 43% 14% 0 100%
800 55% 14% 0 96.70%
1000 60% 21個php-fpm,每個5.9M 14% 0 92.02%
1200 50%,Linux file限制,測試出錯 50%  0 49%

 

nginx + 一個php-cgi:php檔案64.65k,gzip壓縮為11.89k 

sudo /usr/local/php-fpm/bin/php-cgi -b 9000 -q

php-cgi可以常駐(long-live)監聽一個連接埠,所以是fastcgi,但是由於沒有進程管理器,只能啟動一個進程,並發承受力低。

phpinfo顯示Server API:FPM/FastCGI

測試結果:

並發100時,經過5秒,php-cgi崩潰退出。

 

lighttpd + lighttpd內建的spawn-fcgi進程管理器(多個php-cgi子進程):php檔案58.68k,gzip壓縮為9.83k 

phpinfo顯示Server API:CGI/FastCGI

fastcgi的socket方式:unix domain socket

lighttpd的fastcgi.conf配置:

fastcgi.server

 22 fastcgi.server = ( ".php" => 23                    ( "php-local" => 24                      ( 25                        "socket" => socket_dir + "/php-fastcgi-1.socket", 26                        "bin-path" => "/usr/local/php-fpm/bin/php-cgi", 27                        "max-procs" => 2, 28                        "broken-scriptfilename" => "enable", 29                        "bin-environment" => ( 30                          "PHP_FCGI_CHILDREN" => "16", 31                          "PHP_FCGI_MAX_REQUESTS" => "500", 32                        ), 33                      ) 34                    ),

測試結果:

 

 並發 整機512M記憶體使用量率 CPU使用率 iowait siege成功率
0 53% 34個php-cgi進程,每個3M 0 0  
100 65% 每個5.3M 10% 0  
200 65% 每個5.7M 10% 0 100%
400 72% 每個5.7M 11% 0 100%
800 84% 每個5.7M 10% 0 99.49%
1000 90% 每個5.7M 12% 0 97.41%
1200 Linux file限制,測試出錯      

 

參考資料:

http://www.cnblogs.com/killkill/archive/2010/04/14/1711810.html

http://httpd.apache.org/docs/2.4/mod/mpm_common.html#MaxRequestWorkers

http://www.php.net/manual/zh/install.fpm.php

http://www.niutian365.com/blog/article.asp?id=263

http://redmine.lighttpd.net/projects/1/wiki/Docs_ModCompress

http://redmine.lighttpd.net/projects/lighttpd/wiki/Docs_PerformanceFastCGI

http://php.net/manual/zh/install.unix.lighttpd-14.php

http://www.361way.com/mpm/1052.html

http://blog.chinaunix.net/uid-13939710-id-2861331.html

http://www.yylib.com/blog/?p=70

http://kb.cnblogs.com/page/95605/

http://www.dbanotes.net/web/lighttpd_spawn-fcgi.html

http://www.mike.org.cn/articles/what-is-cgi-fastcgi-php-fpm-spawn-fcgi/

http://www.php.net/manual/zh/install.unix.apache2.php

http://ferreousbox.iteye.com/blog/196212

http://nigelzeng.iteye.com/blog/1197339

http://blog.csdn.net/tujiyue/article/details/7027134

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.