互联网创业的准备:Web Server

开发 项目管理
互联网创业的准备,Web Server怎么去选择?本文讲解了目前比较流行的web server搭配分别是Apache+Nginx+Lighttpd与PHP+Module+FastCGI!

测试环境:

vr.org 的 VPS

CPU:2核共享

内存:512M独立

OS:Ubuntu 12.04 LTS x64

  1. uname -a  
  2.  
  3. u1@www:~$ cat /etc/lsb-release   
  4. DISTRIB_ID=Ubuntu  
  5. DISTRIB_RELEASE=12.04  
  6. DISTRIB_CODENAME=precise  
  7. DISTRIB_DESCRIPTION="Ubuntu 12.04 LTS" 
  8. u1@www:~$ uname -a  
  9. Linux www.shaixuan.org 3.4.0-cloud #1 SMP Thu May 24 04:54:53 EDT 2012 x86_64 x86_64 x86_64 GNU/Linux  
  10. u1@www:~$ top 
  11.  
  12. top - 23:57:39 up  1:38,  2 users,  load average: 0.04, 0.03, 0.16  
  13. Tasks:  55 total,   1 running,  54 sleeping,   0 stopped,   0 zombie  
  14. Cpu(s):  0.7%us,  0.4%sy,  0.0%ni, 97.6%id,  0.1%wa,  0.0%hi,  0.1%si,  1.1%st  
  15. Mem:    506764k total,   125264k used,   381500k free,     7736k buffers  
  16. Swap:   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 x64,因为工程师PC使用Ubuntu x64较多(Ubuntu的显卡驱动安装方便),这样编译部署都一致。

如果服务器使用CentOS x64,则工程师PC使用Fedora x64,即可保持一致。

因为内核都是一样的,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

编译参数:

  1. ./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

  1. httpd -V  
  2.  
  3. root@www:/usr/local/apache2/bin# ./httpd -V  
  4. Server version: Apache/2.4.3 (Unix)  
  5. Server built:   Aug 26 2012 10:27:04  
  6. Server's Module Magic Number: 20120211:6  
  7. Server loaded:  APR 1.4.6, APR-UTIL 1.4.1  
  8. Compiled using: APR 1.4.6, APR-UTIL 1.4.1  
  9. Architecture:   64-bit 
  10. Server MPM:     event  
  11.   threaded:     yes (fixed thread count)  
  12.     forked:     yes (variable process count)  
  13. Server compiled with....  
  14.  -D APR_HAS_SENDFILE  
  15.  -D APR_HAS_MMAP  
  16.  -D APR_HAVE_IPV6 (IPv4-mapped addresses enabled)  
  17.  -D APR_USE_SYSVSEM_SERIALIZE  
  18.  -D APR_USE_PTHREAD_SERIALIZE  
  19.  -D APR_HAS_OTHER_CHILD  
  20.  -D AP_HAVE_RELIABLE_PIPED_LOGS  
  21.  -D DYNAMIC_MODULE_LIMIT=256  
  22.  -D HTTPD_ROOT="/usr/local/apache2" 
  23.  -D SUEXEC_BIN="/usr/local/apache2/bin/suexec" 
  24.  -D DEFAULT_PIDLOG="logs/httpd.pid" 
  25.  -D DEFAULT_SCOREBOARD="logs/apache_runtime_status" 
  26.  -D DEFAULT_ERRORLOG="logs/error_log" 
  27.  -D AP_TYPES_CONFIG_FILE="conf/mime.types" 
  28.  -D SERVER_CONFIG_FILE="conf/httpd.conf" 

默认并发400,MaxRequestWorkers即以前的MaxClients,参考:http://httpd.apache.org/docs/2.4/mod/mpm_common.html#MaxRequestWorkers

  1. <IfModule mpm_event_module>  
  2.     StartServers             3     
  3.     MinSpareThreads         75    
  4.     MaxSpareThreads        250   
  5.     ThreadsPerChild         25    
  6.     MaxRequestWorkers      400   
  7.     MaxConnectionsPerChild   0     
  8. </IfModule> 

并发改成40000,提示错误,加上ServerLimit即可:

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

nginx 1.2.3:

编译参数:

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

lighttpd 1.4.31:

默认并发1024,server.max-fds最大可修改为65535,server.max-connections最大可修改为32767:

  1. ##  
  2.  ## As lighttpd is a single-threaded server, its main resource limit is 
  3.  ## the number of file descriptors, which is set to 1024 by default (on 
  4.  ## most systems).  
  5.  ##  
  6.  ## If you are running a high-traffic site you might want to increase this  
  7.  ## limit by setting server.max-fds.  
  8.  ##  
  9.  ## Changing this setting requires root permissions on startup. see  
  10.  ## server.username/server.groupname.  
  11.  ##  
  12.  ## By default lighttpd would not change the operation system default.  
  13.  ## But setting it to 2048 is a better default for busy servers.  
  14.  ##  
  15.  server.max-fds = 2048  
  16.    
  17.  ##  
  18.  ## Fine tuning for the request handling  
  19.  ##  
  20.  ## max-connections == max-fds/2 (maybe /3)  
  21.  ## means the other file handles are used for fastcgi/files  
  22.  ##  
  23.  server.max-connections = 1024 

#p#

1、静态服务器测试

即apache、nginx、lighttpd性能对比。

服务器相同配置 :

开启gzip、关闭etag

测试程序和web server在同一台服务器上,测试命令:

  1. ab -c 1000 -n 50000 http://localhost/index.html  
  2. siege -c 1000 -r 20 http://localhost/index.html 

apache:html文件13k,gzip压缩为4.9k

header:

[[94243]]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:

  1. nginx  
  2.  
  3. Request URL:http://shaixuan.org/index.html  
  4. Request Method:GET  
  5. Status Code:200 OK  
  6. Request Headersview source  
  7. Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8  
  8. Accept-Charset:UTF-8,*;q=0.5  
  9. Accept-Encoding:gzip,deflate,sdch  
  10. Accept-Language:zh-CN,zh;q=0.8,en-US;q=0.6,en;q=0.4  
  11. Cache-Control:no-cache  
  12. Connection:keep-alive  
  13. Host:shaixuan.org  
  14. Pragma:no-cache  
  15. User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.8 (KHTML, like Gecko) Chrome/23.0.1251.2 Safari/537.8  
  16. Response Headersview source  
  17. Connection:keep-alive  
  18. Content-Encoding:gzip  
  19. Content-Type:text/html  
  20. Date:Mon, 03 Sep 2012 13:19:23 GMT  
  21. Last-Modified:Mon, 03 Sep 2012 12:33:45 GMT  
  22. Server:nginx/1.2.3  
  23. Transfer-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:

  1. lighttpd  
  2.  
  3. Request URL:http://shaixuan.org/  
  4. Request Method:GET  
  5. Status Code:200 OK  
  6. Request Headersview source  
  7. Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 
  8. Accept-Charset:UTF-8,*;q=0.5 
  9. Accept-Encoding:gzip,deflate,sdch  
  10. Accept-Language:zh-CN,zh;q=0.8,en-US;q=0.6,en;q=0.4 
  11. Cache-Control:max-age=0 
  12. Connection:keep-alive  
  13. Host:shaixuan.org  
  14. Pragma:no-cache  
  15. User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.8 (KHTML, like Gecko) Chrome/23.0.1251.2 Safari/537.8 
  16. Response Headersview source  
  17. Accept-Ranges:bytes  
  18. Content-Encoding:gzip  
  19. Content-Length:4819 
  20. Content-Type:text/html  
  21. Date:Tue, 04 Sep 2012 12:51:42 GMT  
  22. ETag:"1028714895" 
  23. Last-Modified:Tue, 04 Sep 2012 12:29:10 GMT  
  24. Server:lighttpd/1.4.31 
  25. Vary: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

#p#

2、动态服务器测试

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

测试命令:

  1. siege -c 1200 -r 20 http://localhost/phpinfo.php  
  2. /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编译参数:

  1. ./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配置:

  1. fastcgi.server  
  2.  fastcgi.server = ( ".php" => 
  3.                     ( "php-local" => 
  4.                       (  
  5.                         "socket" => socket_dir + "/php-fastcgi-1.socket",  
  6.                         "bin-path" => "/usr/local/php-fpm/bin/php-cgi",  
  7.                         "max-procs" => 2,  
  8.                         "broken-scriptfilename" => "enable",  
  9.                         "bin-environment" => (  
  10.                           "PHP_FCGI_CHILDREN" => "16",  
  11.                           "PHP_FCGI_MAX_REQUESTS" => "500",  
  12.                         ),  
  13.                       )  
  14.                     ), 

测试结果:

 并发 整机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

原文链接:http://www.cnblogs.com/sink_cup/archive/2012/09/14/web_server.html

责任编辑:林师授 来源: 博客园
相关推荐

2012-09-18 13:41:09

2012-09-19 15:23:06

2012-09-18 13:58:58

互联网创业架构

2012-09-18 13:47:54

2012-09-18 13:55:02

互联网创业数据备份

2012-09-18 13:34:27

互联网创业带宽

2015-05-28 16:11:07

互联网+

2012-09-27 13:49:54

2012-09-18 11:10:05

互联网创业项目

2012-12-31 09:50:12

互联网创业创业者创业

2013-06-24 09:39:34

移动互联网创业投资

2013-06-24 13:52:31

创业互联网创业

2013-09-03 10:58:28

李开复移动互联网创业

2015-10-30 14:24:44

互联网+中医创业

2013-09-11 11:46:06

腾讯云互联网创业

2013-09-12 14:20:06

腾讯云腾讯

2012-09-28 03:19:27

互联网创业调研报告

2015-06-24 15:35:54

2013-08-14 16:36:40

移动互联网母婴市场创业移动应用

2015-07-29 10:58:21

点赞
收藏

51CTO技术栈公众号