Mine?Yours?-Ours!

2008年12月3日星期三

Apache2.0性能优化—MPM的选择与配置[转]

致谢: 原文转自LinuxSir.org

Apache 2.0在性能上的改善最吸引人。在支持POSIX线程的Unix系统上,Apache可以通过不同的MPM运行在一种多进程与多线程相混合的模式下,增强 部分配置的可扩充性能。相比于Apache 1.3,2.0版本做了大量的优化来提升处理能力和可伸缩性,并且大多数改进在默认状态下即可生效。但是在编译和运行时刻,2.0也有许多可以显著提高性 能的选择。本文不想叙述那些以功能换取速度的指令,如HostnameLookups等,而只是说明在2.0中影响性能的最核心特性:MPM(Multi -Processing Modules,多道处理模块)的基本工作原理和配置指令。

毫不夸张地说,MPM的引入是Apache 2.0最重要的变化。大家知道,Apache是基于模块化的设计,而Apache 2.0更扩展了模块化设计到Web服务器的最基本功能。服务器装载了一种多道处理模块,负责绑定本机网络端口、接受请求,并调度子进程来处理请求。扩展模 块化设计有两个重要好处:
◆ Apache可以更简洁、有效地支持多种操作系统;
◆ 服务器可以按站点的特殊需要进行自定制。

在用户级,MPM看起来和其它Apache模块非常类似。主要区别是在任意时刻只能有一种MPM被装载到服务器中。

指定MPM的方法

下面以Red Hat Linux 9为平台,说明在Apache 2.0中如何指定MPM (Apache采用2.0.45)。先解压缩源代码包httpd-2.0.45.tar.gz,生成httpd-2.0.45目录(Apache 1.3源代码包的命名规则是apache_1.3.NN.tar.gz,而2.0版则是httpd-2.0.NN.tar.gz,其中NN是次版本号)。

进入httpd-2.0.45目录,运行以下代码:
$ ./configure –help|grep mpm

显示如下:
–with-mpm=MPM
Choose the process model for Apache to use.
MPM={beos|worker|prefork|mpmt_os2| perchild|leader|threadpool}

上述操作用来选择要使用的进程模型,即哪种MPM模块。Beos、mpmt_os2分别是BeOS和OS/2上缺省的MPM, perchild主要设计目的是以不同的用户和组的身份来运行不同的子进程。这在运行多个需要CGI的虚拟主机时特别有用,会比1.3版中的SuExec 机制做得更好。leader和threadpool都是基于worker的变体,还处于实验性阶段,某些情况下并不会按照预期设想的那样工作,所以 Apache官方也并不推荐使用。因此,我们主要阐述prefork和worker这两种和性能关系最大的产品级MPM ( 有关其它的MPM详细说明,请参见Apache官方文档:http://httpd.apache.org/docs-2.0/mod/)。

prefork的工作原理及配置

如果不用“–with-mpm”显式指定某种MPM,prefork就是Unix平台上缺省的MPM。它所采用的预派生子进程方式也是 Apache 1.3中采用的模式。prefork本身并没有使用到线程,2.0版使用它是为了与1.3版保持兼容性;另一方面,prefork用单独的子进程来处理不 同的请求,进程之间是彼此独立的,这也使其成为最稳定的MPM之一。

若使用prefork,在make编译和make install安装后,使用“httpd -l”来确定当前使用的MPM,应该会看到prefork.c(如果看到worker.c说明使用的是worker MPM,依此类推)。再查看缺省生成的httpd.conf配置文件,里面包含如下配置段:

StartServers 5
MinSpareServers 5
MaxSpareServers 10
MaxClients 150
MaxRequestsPerChild 0

prefork的工作原理是,控制进程在最初建立“StartServers”个子进程后,为了满足MinSpareServers设置的需要创建 一 个进程,等待一秒钟,继续创建两个,再等待一秒钟,继续创建四个……如此按指数级增加创建的进程数,最多达到每秒32个,直到满足 MinSpareServers设置的值为止。这就是预派生(prefork)的由来。这种模式可以不必在请求到来时再产生新的进程,从而减小了系统开销 以增加性能。

MaxSpareServers设置了最大的空闲进程数,如果空闲进程数大于这个值,Apache会自动kill掉一些多余进程。这个值不要设得过 大,但如果设的值比MinSpareServers小,Apache会自动把其调整为MinSpareServers+1。如果站点负载较大,可考虑同时 加大MinSpareServers和MaxSpareServers。

MaxRequestsPerChild设置的是每个子进程可处理的请求数。每个子进程在处理了“MaxRequestsPerChild” 个请求后将自动销毁。0意味着无限,即子进程永不销毁。虽然缺省设为0可以使每个子进程处理更多的请求,但如果设成非零值也有两点重要的好处:
◆ 可防止意外的内存泄漏;
◆ 在服务器负载下降的时侯会自动减少子进程数。

因此,可根据服务器的负载来调整这个值。笔者认为10000左右比较合适。

MaxClients是这些指令中最为重要的一个,设定的是Apache可以同时处理的请求,是对Apache性能影响最大的参数。其缺省值 150是远远不够的,如果请求总数已达到这个值(可通过ps -ef|grep http|wc -l来确认),那么后面的请求就要排队,直到某个已处理请求完毕。这就是系统资源还剩下很多而HTTP访问却很慢的主要原因。系统管理员可以根据硬件配置 和负载情况来动态调整这个值。虽然理论上这个值越大,可以处理的请求就越多,但Apache默认的限制不能大于256。如果把这个值设为大于256,那么 Apache将无法起动。事实上,256对于负载稍重的站点也是不够的。在Apache 1.3中,这是个硬限制。如果要加大这个值,必须在“configure”前手工修改的源代码树下的src/include/httpd.h中查找 256,就会发现“#define HARD_SERVER_LIMIT 256”这行。把256改为要增大的值(如4000),然后重新编译Apache即可。在Apache 2.0中新加入了ServerLimit指令,使得无须重编译Apache就可以加大MaxClients。下面是笔者的prefork配置段:

StartServers 10
MinSpareServers 10
MaxSpareServers 15
ServerLimit 2000
MaxClients 1000
MaxRequestsPerChild 10000

上述配置中,ServerLimit的最大值是20000,对于大多数站点已经足够。如果一定要再加大这个数值,对位于源代码树下server/mpm/prefork/prefork.c中以下两行做相应修改即可:
#define DEFAULT_SERVER_LIMIT 256
#define MAX_SERVER_LIMIT 20000

worker的工作原理及配置

相对于prefork,worker是2.0 版中全新的支持多线程和多进程混合模型的MPM。由于使用线程来处理,所以可以处理相对海量的请求,而系统资源的开销要小于基于进程的服务器。但是, worker也使用了多进程,每个进程又生成多个线程,以获得基于进程服务器的稳定性。这种MPM的工作方式将是Apache 2.0的发展趋势。

在configure -with-mpm=worker后,进行make编译、make install安装。在缺省生成的httpd.conf中有以下配置段:

StartServers 2
MaxClients 150
MinSpareThreads 25
MaxSpareThreads 75
ThreadsPerChild 25
MaxRequestsPerChild 0

worker的工作原理是,由主控制进程生成“StartServers”个子进程,每个子进程中包含固定的ThreadsPerChild 线程数,各个线程独立地处理请求。同样,为了不在请求到来时再生成线程,MinSpareThreads和MaxSpareThreads设置了最少和最 多的空闲线程数;而MaxClients设置了所有子进程中的线程总数。如果现有子进程中的线程总数不能满足负载,控制进程将派生新的子进程。

MinSpareThreads和MaxSpareThreads的最大缺省值分别是75和250。这两个参数对Apache的性能影响并不大,可以按照实际情况相应调节。

ThreadsPerChild是worker MPM中与性能相关最密切的指令。ThreadsPerChild的最大缺省值是64,如果负载较大,64也是不够的。这时要显式使用 ThreadLimit指令,它的最大缺省值是20000。上述两个值位于源码树server/mpm/worker/worker.c中的以下两行:
#define DEFAULT_THREAD_LIMIT 64
#define MAX_THREAD_LIMIT 20000

这两行对应着ThreadsPerChild和ThreadLimit的限制数。最好在configure之前就把64改成所希望的值。注意,不要把这两个值设得太高,超过系统的处理能力,从而因Apache不起动使系统很不稳定。

Worker模式下所能同时处理的请求总数是由子进程总数乘以ThreadsPerChild值决定的,应该大于等于MaxClients。如果负 载 很大,现有的子进程数不能满足时,控制进程会派生新的子进程。默认最大的子进程总数是16,加大时也需要显式声明ServerLimit(最大值是 20000)。这两个值位于源码树server/mpm/worker/worker.c中的以下两行:
#define DEFAULT_SERVER_LIMIT 16
#define MAX_SERVER_LIMIT 20000

需要注意的是,如果显式声明了ServerLimit,那么它乘以ThreadsPerChild的值必须大于等于MaxClients,而且 MaxClients必须是ThreadsPerChild的整数倍,否则Apache将会自动调节到一个相应值(可能是个非期望值)。下面是笔者的 worker配置段:

StartServers 3
MaxClients 2000
ServerLimit 25
MinSpareThreads 50
MaxSpareThreads 200
ThreadLimit 200
ThreadsPerChild 100
MaxRequestsPerChild 0

通过上面的叙述,可以了解到Apache 2.0中prefork和worker这两个重要MPM的工作原理,并可根据实际情况来配置Apache相关的核心参数,以获得最大的性能和稳定性。

阅读Apache2.0官方文件获取详细资料.


Linux-Boot-Step

2008年11月14日星期五

Unix History

Apache+PHP.vs.Nginx+PHP(FastCGI)测试报告

Apache+PHP.vs.Nginx+PHP(FastCGI)测试报告

作者: 张大成 2008-11-14

作者blog

一、平台结构

1、服务器操作系统组成:

1, OS: Ubuntu 8.04.1 hardy

2, Kernel: Linux 2.6.24-16-server #1 SMP i686 GNU/Linux

3, GCC:

root@ftp:/var/www# gcc –v

Using built-in specs.

Target: i486-linux-gnu

Configured with: ../src/configure -v --enable-languages=c,c++,fortran,objc,obj-c++,treelang --prefix=/usr --enable-shared --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --enable-nls --with-gxx-include-dir=/usr/include/c++/4.2 --program-suffix=-4.2 --enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --enable-mpfr --enable-targets=all --enable-checking=release --build=i486-linux-gnu --host=i486-linux-gnu --target=i486-linux-gnu

Thread model: posix

gcc version 4.2.4 (Ubuntu 4.2.4-1ubuntu1)

2、服务器软件组成:

1, APACHE2.2.8-1+PHP5.2.4 (采用aptitude方式安装LAMP)

2, NGINX0.7.20+PHP5.2.6(均采用源代码安装, fastcgi方式安装php), 其它附加软件包括eaccelerator-0.9.5.3/libevent-1.2/libiconv-1.12/libmcrypt-2.5.8/mcrypt-2.6.7/memcache-3.0.2/mhash-0.9.9/ncurses-5.6/pcre-7.7/php-5.2.6-fpm-0.5.9.diff.

3、服务器硬件信息:

1, MEMORY: 2G, Swap: 2G

2, CPU: Intel(R) Xeon(R) CPU 3065 @ 2.33GHz (2)

3, DISK: SCSI 160G

二、安装配置说明:

1NGINX0.7.20+PHP5.2.6(均采用源代码安装)

1Nginx0.7.20安装配置:

A)安装编译参数:

./configure --user=www-data --group=www-data \

> --prefix=/usr/local/webserver/nginx \

> --with-http_stub_status_module --with-http_ssl_module

B) 配置文件nginx.conf:

root@ftp: ~#cd /usr/local/webserver/nginx/

root@ftp: nginx# grep -v "#" conf/nginx.conf

user www-data www-data;

worker_processes 5;

error_log logs/error.log info;

pid logs/nginx.pid;

worker_rlimit_nofile 50000;

events {

worker_connections 2024;

use epoll;

}

http {

include mime.types;

default_type application/octet-stream;

log_format main '$remote_addr - $remote_user [$time_local] "$request" '

'"$status" $body_bytes_sent "$http_referer" '

'"$http_user_agent" "$http_x_forwarded_for"'

'"$gzip_ratio"';

access_log logs/access.log main;

sendfile on;

tcp_nopush on;

tcp_nodelay on;

keepalive_timeout 60;

output_buffers 1 32k;

postpone_output 1460;

server_names_hash_bucket_size 128;

client_header_buffer_size 32k;

client_header_timeout 3m;

client_body_timeout 3m;

large_client_header_buffers 4 32k;

client_max_body_size 10m;

client_body_buffer_size 128k;

send_timeout 3m;

fastcgi_connect_timeout 300;

fastcgi_send_timeout 300;

fastcgi_read_timeout 300;

fastcgi_buffer_size 64k;

fastcgi_buffers 4 64k;

fastcgi_busy_buffers_size 128k;

fastcgi_temp_file_write_size 128k;

gzip on;

gzip_min_length 1k;

gzip_buffers 4 16k;

gzip_http_version 1.0;

gzip_comp_level 2;

gzip_proxied any;

gzip_types text/plain application/x-javascript text/css application/xml text/xml;

gzip_vary on;

server {

listen 81;

server_name ftp.jbird.jb;

access_log logs/nginx.access.log main;

location / {

index index.html index.htm index.php;

root /var/www/nginx/test;

}

location ~ .*\.(php|php5)$ {

root /var/www/nginx/test;

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

include fastcgi_params;

}

location ~ .*\.(gif|jpeg|jpg|png|bmp|swf|asf|wmv|avi)$ {

root /var/www/nginx/test;

expires 1d;

}

location ~ .*\.(js|css)$ {

root /var/www/nginx/test;

expires 1h;

}

error_page 404 /404.html;

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root html;

}

}

server {

listen 82;

server_name ftp.jbird.jb localhost 192.168.3.254 10.10.10.5;

location / {

stub_status on;

access_log off;

allow 192.168.3.0/24;

}

}

}

C) nginx.conf文件所includefastcgi_params配置文件:

root@ftp: ~#cd /usr/local/webserver/nginx/

root@ftp: nginx#cat conf/fastcgi_params

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;

2) PHP5.2.6安装配置:

A) PHP安装编译参数:

./configure '--prefix=/usr/local/webserver/php' '--with-config-file-path=/usr/local/webserver/php/etc' '--with-mysql=/usr/local/webserver/mysql' '--with-mysqli=/usr/local/webserver/mysql/bin/mysql_config' '--with-iconv-dir=/usr/local/' '--with-freetype-dir' '--with-jpeg-dir' '--with-png-dir' '--with-zlib' '--with-libxml-dir=/usr' '--enable-xml' '--disable-rpath' '--enable-discard-path' '--enable-safe-mode' '--enable-bcmath' '--enable-shmop' '--enable-sysvsem' '--enable-inline-optimization' '--with-curl' '--with-curlwrappers' '--enable-mbregex' '--enable-fastcgi' '--enable-fpm' '--enable-force-cgi-redirect' '--enable-mbstring' '--with-mcrypt' '--enable-gd-native-ttf' '--with-openssl'

Bphp.ini文件配置(以下是修改和添加部分):

root@ftp: etc#cd /usr/local/webserver/php/etc

root@ftp:etc#egrep 'output_buffer|extens' php.ini |grep -v "^;"

output_buffering = On

extension_dir = "/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20060613/"

extension = "memcache.so"

zend_extension="/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20060613/eaccelerator.so"

root@ftp: etc# tail -n 16 php.ini

[eaccelerator]

zend_extension="/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20060613/eaccelerator.so"

eaccelerator.shm_size="128"

eaccelerator.cache_dir="/usr/local/webserver/php/eaccelerator_cache/"

eaccelerator.enable="1"

eaccelerator.optimizer="1"

eaccelerator.check_mtime="1"

eaccelerator.debug="0"

eaccelerator.filter=""

eaccelerator.shm_max="0"

eaccelerator.shm_ttl="300"

eaccelerator.shm_prune_period="120"

eaccelerator.shm_only="0"

eaccelerator.compress="1"

eaccelerator.compress_level="9"

C) php-fpm.conf文件(红色部分是修改部分):

root@ftp: etc#cat php-fpm.conf

/usr/local/webserver/php/logs/php-fpm.pid

/usr/local/webserver/php/logs/php-fpm.log

notice

10

1m

5s

yes

default

127.0.0.1:9000

-1

0666

/usr/sbin/sendmail -t -i

0

www-data

www-data

static

128

20

5

35

0s

0s

logs/slow.log

50000

0

yes

10240

127.0.0.1

$HOSTNAME

/usr/local/bin:/usr/bin:/bin

/tmp

/tmp

/tmp

$OSTYPE

$MACHTYPE

2

D) 以下是http://192.168.3.254:81/info.php (nginx服务器)(内容是:)显示的内容:

PHP Version 5.2.6

System

Linux ftp.jbird.jb 2.6.24-16-server #1 SMP Thu Apr 10 13:58:00 UTC 2008 i686

Build Date

Nov 11 2008 14:05:01

Configure Command

'./configure' '--prefix=/usr/local/webserver/php' '--with-config-file-path=/usr/local/webserver/php/etc' '--with-mysql=/usr/local/webserver/mysql' '--with-mysqli=/usr/local/webserver/mysql/bin/mysql_config' '--with-iconv-dir=/usr/local/' '--with-freetype-dir' '--with-jpeg-dir' '--with-png-dir' '--with-zlib' '--with-libxml-dir=/usr' '--enable-xml' '--disable-rpath' '--enable-discard-path' '--enable-safe-mode' '--enable-bcmath' '--enable-shmop' '--enable-sysvsem' '--enable-inline-optimization' '--with-curl' '--with-curlwrappers' '--enable-mbregex' '--enable-fastcgi' '--enable-fpm' '--enable-force-cgi-redirect' '--enable-mbstring' '--with-mcrypt' '--enable-gd-native-ttf' '--with-openssl'

Server API

CGI/FastCGI

PHP API

20041225

PHP Extension

20060613

Zend Extension

220060519

Thread Safety

disabled

Zend Memory Manager

enabled

Registered PHP Streams

php, file, data, tftp, ftp, telnet, dict, ldap, ldaps, http, https, ftps, compress.zlib

Registered Stream Socket Transports

tcp, udp, unix, udg, ssl, sslv3, sslv2, tls

Registered Stream Filters

string.rot13, string.toupper, string.tolower, string.strip_tags, convert.*, consumed, convert.iconv.*, zlib.*

cgi-fcgi

Directive

Local Value

Master Value

cgi.check_shebang_line

1

1

cgi.fix_pathinfo

1

1

cgi.force_redirect

1

1

cgi.nph

0

0

cgi.redirect_status_env

no value

no value

cgi.rfc2616_headers

0

0

fastcgi.error_header

no value

no value

fastcgi.logging

1

1

php-fpm

active


php-fpm version

0.5.9







eAccelerator

eAccelerator support

enabled


Version

0.9.5.3


Caching Enabled

true


Optimizer Enabled

true


Memory Size

134,217,692 Bytes


Memory Available

134,213,128 Bytes


Memory Allocated

4,564 Bytes


Cached Scripts

1


Removed Scripts

0


Cached Keys

0


Directive

Local Value

Master Value

eaccelerator.allowed_admin_path

no value

no value

eaccelerator.cache_dir

/usr/local/webserver/php\

/eaccelerator_cache/

/usr/local/webserver/php\

/eaccelerator_cache/

eaccelerator.check_mtime

1

1

eaccelerator.compress

1

1

eaccelerator.compress_level

9

9

eaccelerator.debug

0

0

eaccelerator.enable

1

1

eaccelerator.filter

no value

no value

eaccelerator.log_file

no value

no value

eaccelerator.name_space

no value

no value

eaccelerator.optimizer

1

1

eaccelerator.shm_max

0

0

eaccelerator.shm_only

0

0

eaccelerator.shm_prune_period

120

120

eaccelerator.shm_size

128

128

eaccelerator.shm_ttl

300

300

memcache

memcache support

enabled

Version

3.0.2

Revision

$Revision: 1.83.2.28 $

Directive

Local Value

Master Value

memcache.allow_failover

1

1

memcache.chunk_size

32768

32768

memcache.default_port

11211

11211

memcache.hash_function

crc32

crc32

memcache.hash_strategy

consistent

consistent

memcache.max_failover_attempts

20

20

memcache.protocol

ascii

ascii

memcache.redundancy

1

1

memcache.session_redundancy

2

2

2APACHE2.2.8-1+PHP5.2.4 (采用aptitude方式安装LAMP)

1Apache2.2.8安装配置:

A)安装采用aptitude方式安装LAMP;

B) Apache配置文件:

root@ftp:~# grep -v "^#" /etc/apache2/apache2.conf | uniq

ServerRoot "/etc/apache2"

LockFile /var/lock/apache2/accept.lock

PidFile ${APACHE_PID_FILE}

Timeout 30

KeepAlive On

MaxKeepAliveRequests 300

KeepAliveTimeout 10

StartServers 5

MinSpareServers 5

MaxSpareServers 10

ServerLimit 2000

MaxRequestsPerChild 0

User ${APACHE_RUN_USER}

Group ${APACHE_RUN_GROUP}

AccessFileName .htaccess

Order allow,deny

Deny from all

DefaultType text/plain

HostnameLookups Off

ErrorLog /var/log/apache2/error.log

LogLevel warn

Include /etc/apache2/mods-enabled/*.load

Include /etc/apache2/mods-enabled/*.conf

Include /etc/apache2/httpd.conf

Include /etc/apache2/ports.conf

LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined

LogFormat "%h %l %u %t \"%r\" %>s %b" common

LogFormat "%{Referer}i -> %U" referer

LogFormat "%{User-agent}i" agent

ServerTokens Full

ServerSignature On

Include /etc/apache2/conf.d/

Include /etc/apache2/sites-enabled/

NameVirtualHost *

ServerAdmin webmaster@localhost

ServerAlias *.192.168.3.254

DocumentRoot /var/www/

Options FollowSymLinks

AllowOverride FileInfo Options

Options Indexes FollowSymLinks MultiViews

AllowOverride FileInfo Options

Order allow,deny

allow from all

ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/

AllowOverride None

Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch

Order allow,deny

Allow from all

ErrorLog /var/log/apache2/error.log

LogLevel warn

CustomLog /var/log/apache2/access.log combined

ServerSignature On

Alias /doc/ "/usr/share/doc/"

Options Indexes MultiViews FollowSymLinks

AllowOverride None

Order deny,allow

Deny from all

Allow from 127.0.0.0/255.0.0.0 ::1/128

root@ftp:~# apache2 -l

//启用prefork.c这个MPM

Compiled in modules:

core.c

mod_log_config.c

mod_logio.c

prefork.c

http_core.c

mod_so.c

2) PHP5.2.4安装配置 :

A) 安装采用aptitude方式安装LAMP;

B) php.ini文件采用默认配置.

C) 以下是http://192.168.3.254/info.php (apache服务器)(内容是:)显示的内容:

PHP Version 5.2.4-2ubuntu5.3

System

Linux ftp.jbird.jb 2.6.24-16-server #1 SMP Thu Apr 10 13:58:00 UTC 2008 i686

Build Date

Jul 23 2008 06:28:41

Server API

Apache 2.0 Handler

Virtual Directory Support

disabled

Configuration File (php.ini) Path

/etc/php5/apache2

Loaded Configuration File

/etc/php5/apache2/php.ini

Scan this dir for additional .ini files

/etc/php5/apache2/conf.d

additional .ini files parsed

/etc/php5/apache2/conf.d/gd.ini, /etc/php5/apache2/conf.d/mysql.ini, /etc/php5/apache2/conf.d/mysqli.ini, /etc/php5/apache2/conf.d/pdo.ini, /etc/php5/apache2/conf.d/pdo_mysql.ini

PHP API

20041225

PHP Extension

20060613

Zend Extension

220060519

Thread Safety

disabled

Zend Memory Manager

enabled

IPv6 Support

enabled

Registered PHP Streams

zip, php, file, data, http, ftp, compress.bzip2, compress.zlib, https, ftps

Registered Stream Socket Transports

tcp, udp, unix, udg, ssl, sslv3, sslv2, tls

Registered Stream Filters

string.rot13, string.toupper, string.tolower, string.strip_tags, convert.*, consumed, convert.iconv.*, bzip2.*, zlib.*

三、测试

1、启动apachenignx

root@ftp:~# cat ./startapache+nginx.sh

// 启动脚本

#!/bin/sh

#

ulimit -SHn 50000

echo 134217728 > /proc/sys/kernel/shmmax

# 因为在php.ini文件中有eaccelerator.shm_size="128"的设定, 134217728bytes/1024/1024 = 128MB

echo 30 > /proc/sys/net/ipv4/tcp_fin_timeout

echo 300 > /proc/sys/net/ipv4/tcp_keepalive_time

echo 1 > /proc/sys/net/ipv4/tcp_syncookies

echo 1 > /proc/sys/net/ipv4/tcp_tw_reuse

echo 1 > /proc/sys/net/ipv4/tcp_tw_recycle

echo "5000 65000" > /proc/sys/net/ipv4/ip_local_port_range

# start php5+nginx

/usr/local/webserver/php/sbin/php-fpm start

/usr/local/webserver/nginx/sbin/nginx

# start apache2

/etc/init.d/apache2 start

// 开始启动

root@ftp:~# source ./startapache+nginx.sh

//查看状态

root@ftp:~# netstat -an | egrep '80|81|9000'

tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN

tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN

tcp 0 0 0.0.0.0:81 0.0.0.0:* LISTEN

//查看进程

root@ftp:~# ps aux | egrep 'nginx|apache'

root 14959 0.0 0.0 4472 648 ? Ss 12:33 0:00 nginx: master process /usr/local/webserver/nginx/sbin/nginx

www-data 14961 0.0 0.0 5044 1736 ? S 12:33 0:00 nginx: worker process

www-data 14962 0.0 0.0 4884 1352 ? S 12:33 0:00 nginx: worker process

www-data 14971 0.0 0.0 4884 1352 ? S 12:33 0:00 nginx: worker process

www-data 14972 0.0 0.0 4884 1352 ? S 12:33 0:00 nginx: worker process

www-data 14975 0.0 0.0 4884 1352 ? S 12:33 0:00 nginx: worker process

root 15006 0.0 0.3 24964 7244 ? Ss 12:33 0:00 /usr/sbin/apache2 -k start

www-data 15013 0.0 0.2 25004 6192 ? S 12:33 0:00 /usr/sbin/apache2 -k start

www-data 15019 0.0 0.2 25004 4264 ? S 12:33 0:00 /usr/sbin/apache2 -k start

www-data 15021 0.0 0.2 25004 4264 ? S 12:33 0:00 /usr/sbin/apache2 -k start

www-data 15023 0.0 0.2 25004 4264 ? S 12:33 0:00 /usr/sbin/apache2 -k start

www-data 15025 0.0 0.2 25004 4264 ? S 12:33 0:00 /usr/sbin/apache2 -k start

www-data 15133 0.0 0.2 25004 4264 ? S 12:34 0:00 /usr/sbin/apache2 -k start

root@ftp:~# ps aux | egrep 'php-cgi'

root 14946 0.0 0.1 150464 2804 ? Ss 12:33 0:00 /usr/local/webserver/php/bin/php-cgi --fpm --fpm-config /usr/local/webserver/php/etc/php-fpm.conf

......

www-data 15104 0.0 0.1 149484 2600 ? S 12:33 0:00 /usr/local/webserver/php/bin/php-cgi --fpm --fpm-config /usr/local/webserver/php/etc/php-fpm.conf

root@ftp:~# ps aux | egrep 'php-cgi' |grep -v "egrep" |wc -l

129


2、测试 : php测试

A) 测试方法:我们的测试采用webbench1.5这个软件来模拟clients,分别以20050010002000clients去运行30秒;另外采用sar工具来采集数据;

B)采集数据:包括sar工具的cpumemoryswap输出,sar以每隔5秒的频率共采集3组;

C)采集的页面均是info.php,内容是

root@ftp: report# cat info.php

?

phpinfo();

?>

第一组php测试:200clients数据:

1) Apache200clients数据

//首先是webbech采集数据:

root@ftp: ~#webbench -c200 -t30 http://192.168.3.254/info.php

Speed=49450 pages/min, 40113860 bytes/sec.

Requests: 24725 susceed, 0 failed.

//说明apache200clients,响应速度是40.1M/s824pages/s共接收请求24725没有失败。

//下面是sar采集数据 :

root@ftp: report#sar -ur 5 3

16:09:26 cpu %usr %nice %sys %irq %softirq %wait %idle _cpu_

16:09:30 all 90 0 7 0 3 0 0

0 90 0 7 0 3 0 0

1 90 0 8 0 2 0 0

16:09:26 memtot memfree buffers cached slabmem swptot swpfree _mem_

16:09:30 2024M 1502M 8M 334M 31M 2000M 2000M

16:09:30 cpu %usr %nice %sys %irq %softirq %wait %idle _cpu_

16:09:35 all 88 0 9 0 3 0 0

0 87 0 9 0 3 0 0

1 90 0 8 0 3 0 0

16:09:30 memtot memfree buffers cached slabmem swptot swpfree _mem_

16:09:35 2024M 1488M 8M 334M 32M 2000M 2000M

16:09:35 cpu %usr %nice %sys %irq %softirq %wait %idle _cpu_

16:09:40 all 91 0 6 0 3 0 0

0 91 0 6 0 3 0 0

1 91 0 6 0 3 0 0

16:09:35 memtot memfree buffers cached slabmem swptot swpfree _mem_

16:09:40 2024M 1479M 8M 334M 33M 2000M 2000M

//说明apache200clients, CPU利用率usr占用到90%内存使用535M软件中断3%, swap没有使用。

2Nginx200clients数据:

//首先是webbech采集数据:

root@ftp: ~#webbench -c200 -t30 http://192.168.3.254:81/info.php

Speed=56014 pages/min, 44434882 bytes/sec.

Requests: 28007 susceed, 0 failed.

//说明: nginx200clients, 响应速度是44.4M/s933pages/s,共接收请求28007个,没有失败。

//下面是sar采集数据 :

root@ftp: ~#sar -ur 5 3

16:12:36 cpu %usr %nice %sys %irq %softirq %wait %idle _cpu_

16:12:40 all 88 0 8 0 4 0 0

0 87 0 8 0 4 0 0

1 88 0 8 0 4 0 0

16:12:36 memtot memfree buffers cached slabmem swptot swpfree _mem_

16:12:40 2024M 1450M 9M 337M 30M 2000M 2000M

16:12:40 cpu %usr %nice %sys %irq %softirq %wait %idle _cpu_

16:12:45 all 86 0 8 0 5 0 0

0 85 0 8 0 7 0 0

1 87 0 9 0 4 0 0

16:12:40 memtot memfree buffers cached slabmem swptot swpfree _mem_

16:12:45 2024M 1449M 9M 338M 30M 2000M 2000M

16:12:45 cpu %usr %nice %sys %irq %softirq %wait %idle _cpu_

16:12:50 all 87 0 8 0 4 0 0

0 86 0 9 0 4 0 0

1 88 0 7 0 4 0 0

16:12:45 memtot memfree buffers cached slabmem swptot swpfree _mem_

16:12:50 2024M 1448M 9M 338M 30M 2000M 2000M

//说明:nginx200clients, CPU利用率usr占用到87%,内存使用576M,软件中断3.7%, swap没有使用。

第二组php测试:500clients数据:

1) Apache500clients数据:

//首先是webbech采集数据:

root@ftp: ~#webbench –c500 -t30 http://192.168.3.254/info.php

Speed=49946 pages/min, 40376572 bytes/sec.

Requests: 24875 susceed, 98 failed.

//说明apache500clients,响应速度是40.3M/s832pages/s共接收请求24876失败98

//下面是sar采集数据 :

root@ftp: report#sar -ur 5 3

16:15:35 cpu %usr %nice %sys %irq %softirq %wait %idle _cpu_

16:15:39 all 71 0 7 0 3 0 19

0 71 0 6 0 4 0 19

1 71 0 8 0 3 0 19

16:15:35 memtot memfree buffers cached slabmem swptot swpfree _mem_

16:15:39 2024M 1456M 9M 341M 42M 2000M 2000M

16:15:39 cpu %usr %nice %sys %irq %softirq %wait %idle _cpu_

16:15:44 all 88 0 8 0 3 0 0

0 90 0 6 0 4 0 0

1 87 0 10 0 3 0 0

16:15:39 memtot memfree buffers cached slabmem swptot swpfree _mem_

16:15:44 2024M 1441M 9M 341M 44M 2000M 2000M

16:15:44 cpu %usr %nice %sys %irq %softirq %wait %idle _cpu_

16:15:49 all 88 0 8 0 4 0 0

0 88 0 8 0 4 0 0

1 89 0 7 0 4 0 0

16:15:44 memtot memfree buffers cached slabmem swptot swpfree _mem_

16:15:49 2024M 1429M 9M 341M 45M 2000M 2000M

//说明apache500clients, CPU利用率usr占用到83%内存使用582M软件中断3.3%, swap没有使用。

2Nginx500clients数据:

//首先是webbech采集数据:

root@ftp: ~#webbench –c500 -t30 http://192.168.3.254:81/info.php

Speed=56238 pages/min, 44480931 bytes/sec.

Requests: 28039 susceed, 80 failed.

//说明: nginx500clients, 响应速度是44.4M/s937pages/s,共接收请求28039个,失败80

//下面是sar采集数据 :

root@ftp: ~#sar -ur 5 3

16:23:54 cpu %usr %nice %sys %irq %softirq %wait %idle _cpu_

16:23:58 all 86 0 9 0 4 0 0

0 85 0 11 0 4 0 0

1 87 0 8 0 5 0 0

16:23:54 memtot memfree buffers cached slabmem swptot swpfree _mem_

16:23:58 2024M 1401M 11M 349M 40M 2000M 2000M

16:23:58 cpu %usr %nice %sys %irq %softirq %wait %idle _cpu_

16:24:03 all 87 0 8 0 4 0 0

0 88 0 9 0 3 0 0

1 87 0 8 0 5 0 0

16:23:58 memtot memfree buffers cached slabmem swptot swpfree _mem_

16:24:03 2024M 1400M 11M 349M 40M 2000M 2000M

16:24:03 cpu %usr %nice %sys %irq %softirq %wait %idle _cpu_

16:24:08 all 86 0 8 0 6 0 0

0 85 0 8 0 7 0 0

1 86 0 8 0 6 0 0

16:24:03 memtot memfree buffers cached slabmem swptot swpfree _mem_

16:24:08 2024M 1400M 11M 350M 41M 2000M 2000M

//说明:nginx500clients, CPU利用率usr占用到86%,内存使用624M,软件中断4.5%, swap没有使用。

第三组php测试:1000clients数据:

2) Apache1000clients数据:

//首先是webbech采集数据:

root@ftp: ~#webbench –c1000 -t30 http://192.168.3.254/info.php

Speed=48496 pages/min, 38980372 bytes/sec.

Requests: 24011 susceed, 237 failed.

//说明:apache1000clients,响应速度是38.9M/s808pages/s,共接收请求24011个,失败237

//下面是sar采集数据 :

root@ftp: report#sar -ur 5 3

16:26:54 cpu %usr %nice %sys %irq %softirq %wait %idle _cpu_

16:26:58 all 87 0 7 0 3 0 3

0 85 0 7 0 5 0 3

1 89 0 6 0 2 0 3

16:26:54 memtot memfree buffers cached slabmem swptot swpfree _mem_

16:26:58 2024M 1383M 11M 360M 59M 2000M 2000M

16:26:58 cpu %usr %nice %sys %irq %softirq %wait %idle _cpu_

16:27:03 all 90 0 7 0 3 0 0

0 90 0 7 0 3 0 0

1 90 0 7 0 3 0 0

16:26:58 memtot memfree buffers cached slabmem swptot swpfree _mem_

16:27:03 2024M 1361M 12M 360M 60M 2000M 2000M

16:27:03 cpu %usr %nice %sys %irq %softirq %wait %idle _cpu_

16:27:08 all 90 0 8 0 3 0 0

0 89 0 8 0 3 0 0

1 91 0 7 0 2 0 0

16:27:03 memtot memfree buffers cached slabmem swptot swpfree _mem_

16:27:08 2024M 1347M 12M 361M 62M 2000M 2000M

//说明apache1000clients, CPU利用率usr占用到88%内存使用664M软件中断3%, swap没有使用。

2Nginx1000clients数据:

//首先是webbech采集数据:

root@ftp: ~#webbench –c1000 -t30 http://192.168.3.254:81/info.php

Speed=56608 pages/min, 44287607 bytes/sec.

Requests: 27917 susceed, 387 failed.

//说明: nginx1000clients, 响应速度是44.2M/s943pages/s,共接收请求27917个,失败387

//下面是sar采集数据 :

root@ftp: ~#sar -ur 5 3

16:32:11 cpu %usr %nice %sys %irq %softirq %wait %idle _cpu_

16:32:15 all 87 0 9 0 4 0 0

0 87 0 9 0 3 0 0

1 86 0 9 0 5 0 0

16:32:11 memtot memfree buffers cached slabmem swptot swpfree _mem_

16:32:15 2024M 1330M 12M 370M 57M 2000M 2000M

16:32:15 cpu %usr %nice %sys %irq %softirq %wait %idle _cpu_

16:32:20 all 88 0 9 0 4 0 0

0 89 0 8 0 3 0 0

1 87 0 9 0 5 0 0

16:32:15 memtot memfree buffers cached slabmem swptot swpfree _mem_

16:32:20 2024M 1328M 12M 371M 57M 2000M 2000M

16:32:20 cpu %usr %nice %sys %irq %softirq %wait %idle _cpu_

16:32:25 all 85 0 10 0 5 0 0

0 86 0 10 0 5 0 0

1 85 0 11 0 5 0 0

16:32:20 memtot memfree buffers cached slabmem swptot swpfree _mem_

16:32:25 2024M 1328M 12M 371M 58M 2000M 2000M

//说明:nginx1000clients, CPU利用率usr占用到86%,内存使用697M,软件中断4.3%, swap没有使用。

第四组php测试:2000clients数据:

3) Apache2000clients数据:

//首先是webbech采集数据:

root@ftp: ~#webbench –c2000 -t30 http://192.168.3.254/info.php

Speed=47904 pages/min, 38329354 bytes/sec.

Requests: 23649 susceed, 303 failed.

//说明apache2000clients,响应速度是38.3M/s798pages/s共接收请求23649失败303

//下面是sar采集数据 :

root@ftp: report#sar -ur 5 3

16:40:33 cpu %usr %nice %sys %irq %softirq %wait %idle _cpu_

16:40:37 all 87 0 9 0 4 0 0

0 87 0 8 0 5 0 0

1 87 0 10 0 3 0 0

16:40:33 memtot memfree buffers cached slabmem swptot swpfree _mem_

16:40:37 2024M 1255M 13M 386M 91M 2000M 2000M

16:40:37 cpu %usr %nice %sys %irq %softirq %wait %idle _cpu_

16:40:42 all 89 0 8 0 3 0 0

0 91 0 7 0 2 0 0

1 88 0 10 0 3 0 0

16:40:37 memtot memfree buffers cached slabmem swptot swpfree _mem_

16:40:42 2024M 1249M 13M 386M 92M 2000M 2000M

16:40:42 cpu %usr %nice %sys %irq %softirq %wait %idle _cpu_

16:40:47 all 89 0 7 0 4 0 0

0 89 0 7 0 4 0 0

1 89 0 7 0 4 0 0

16:40:42 memtot memfree buffers cached slabmem swptot swpfree _mem_

16:40:47 2024M 1242M 13M 386M 92M 2000M 2000M

//说明apache2000clients, CPU利用率usr占用到88%内存使用775M软件中断4%, swap没有使用。

2Nginx2000clients数据:

//首先是webbech采集数据:

root@ftp: ~#webbench –c2000 -t30 http://192.168.3.254:81/info.php

Speed=56150 pages/min, 43946270 bytes/sec.

Requests: 27698 susceed, 377 failed.

//说明: nginx2000clients, 响应速度是43.9M/s935pages/s共接收请求27698失败377

//下面是sar采集数据:

root@ftp: ~#sar -ur 5 3

16:44:06 cpu %usr %nice %sys %irq %softirq %wait %idle _cpu_

16:44:10 all 88 0 8 0 4 0 0

0 88 0 9 0 3 0 0

1 88 0 7 0 5 0 0

16:44:06 memtot memfree buffers cached slabmem swptot swpfree _mem_

16:44:10 2024M 1228M 14M 391M 88M 2000M 2000M

16:44:10 cpu %usr %nice %sys %irq %softirq %wait %idle _cpu_

16:44:15 all 86 0 11 0 3 0 0

0 85 0 11 0 4 0 0

1 86 0 11 0 3 0 0

16:44:10 memtot memfree buffers cached slabmem swptot swpfree _mem_

16:44:15 2024M 1227M 14M 392M 89M 2000M 2000M

16:44:15 cpu %usr %nice %sys %irq %softirq %wait %idle _cpu_

16:44:20 all 85 0 10 0 5 0 0

0 85 0 10 0 6 0 0

1 85 0 10 0 5 0 0

16:44:15 memtot memfree buffers cached slabmem swptot swpfree _mem_

16:44:20 2024M 1225M 14M 392M 89M 2000M 2000M

//说明nginx2000clients, CPU利用率usr占用到86%内存使用797M软件中断4%, swap没有使用。

四、最终总结:

总结见下表:

clients

sar -ur 5 3

webbench -c<clients> -t30 url

CPU(%)

Memory(M)

pages/s

susceed

failed

apache

nginx

apache

nginx

apache

nginx

apache

nginx

apache

nginx

200
Clients

90

87

535

576

824

933

24725

28007

0

0

500
Clients

83

86

582

624

832

937

24876

28039

98

80

1000
Clients

88

86

664

697

808

943

24011

27917

237

387

2000
Clients

88

86

775

797

798

935

23649

27698

303

516

1、 nginxpages/sapache500client以下的情况下快大约100, 但是1000以上后, 差距则越来越小;

2、 failed率在clients500以下没有太大差别, client越来越多的时候, nginxfailed率要比apache多出50%.

3、 nginxapachememory使用上似乎要多一些, 但是因为有FastCGIph-cgi的进程存在, 所以就不显得多了.

4、 在请求数量中, nginx的成功数(susceed)要明显多于apache(每次多约4000).