重新编辑发一遍,之前的完全乱了
受人所托,要做一个比较正式的网站,可自己以前折腾都是 debian + lighttpd + php-fastcgi + sqlite 这样的奇葩组合,肯定不能这么用。
首先是系统,一直都是 debian 而且也没有什么不稳定的情况,但 centos 在很多人口中似乎都是:红帽亲儿子,企业,稳定。于是便纠结应该用自己熟悉的 debian 还是换到口碑似乎更好的 centos 。
第二是 web 服务器,自己用 lighttpd 只是为了低资源占用,普遍用的应该都是 apache 和 nginx 吧,但用法也有好多种。
- 单 apache ,并发弱 但 mod-php 性能好,资源占用高。
- 单 nginx ,并发强 但 php-fpm 会有 502 ,资源占用低。
- nginx 反代 apache ,前端 nginx 抗并发,后端 apache 处理 php ,似乎是最佳方案。
开始动手测试,测试是在虚拟机下进行的,两台虚拟机都是 512M 内存,系统是 debian 8 和 centos 7 ,都用默认源,都用 apt/yum 安装。
apache 部分
Centos
Server version: Apache/2.4.6 (CentOS) Server built: Nov 19 2015 21:43:13
Debian
Server version: Apache/2.4.10 (Debian) Server built: Nov 28 2015 14:05:48
从版本号上看,是 debian 中的 apache 较新,但是有听到一种说法, centos 中的包都有 backport ,虽然版本号旧,但软件不旧,具体不知如何考证。
php 部分
Centos
PHP 5.4.16 (cli) (built: Jun 23 2015 21:17:27)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies
Debian
PHP 5.6.17-0+deb8u1 (cli) (built: Jan 13 2016 09:10:12)
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2015 Zend Technologies
with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2015, by Zend Technologies
从版本号上看,是 debian 中的 php 较新。
但是安装 php-gd 的时候, centos 中是 bundle 版, debian 中是 dev 版, dev 字面上应该是开发版。明明用的是 debian 的 stable 源,为什么会是开发版?
mysql 部分
Centos
mysql Ver 15.1 Distrib 5.5.44-MariaDB, for Linux (x86_64) using readline 5.1
Debian
mysql Ver 14.14 Distrib 5.5.47, for debian-linux-gnu (x86_64) using readline 6.3
数据库这里就不能看版本号了,因为 centos 用 yum 安装 mysql 会自动安装 mariadb 。
压力测试部分
debian 和 centos 下分别安装 Discuz x3.2 后进行压力测试,放个雅黑探针观察系统负载。
使用 apache 的压力测试工具, ab -c 100 -n 100 http://127.0.0.1/forum.php ,之后逐步增加。
Centos
Server Software: Apache/2.4.6
Server Hostname: 127.0.0.1
Server Port: 80
Document Path: /forum.php
Document Length: 12872 bytes
Concurrency Level: 100
Time taken for tests: 3.589 seconds
Complete requests: 100
Failed requests: 0
Write errors: 0
Total transferred: 1359500 bytes
HTML transferred: 1287200 bytes
Requests per second: 27.86 [#/sec] (mean)
Time per request: 3589.014 [ms] (mean)
Time per request: 35.890 [ms] (mean, across all concurrent requests)
Transfer rate: 369.92 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 1 1 0.3 1 2
Processing: 74 1974 996.3 1995 3513
Waiting: 71 1974 996.3 1995 3513
Total: 74 1975 996.0 1997 3514
Percentage of the requests served within a certain time (ms)
50% 1997
66% 2560
75% 2925
80% 3046
90% 3374
95% 3484
98% 3494
99% 3514
100% 3514 (longest request)
Debian
Server Software: Apache/2.4.10
Server Hostname: 127.0.0.1
Server Port: 80
Document Path: /forum.php
Document Length: 12872 bytes
Concurrency Level: 100
Time taken for tests: 1.215 seconds
Complete requests: 100
Failed requests: 0
Total transferred: 1367400 bytes
HTML transferred: 1287200 bytes
Requests per second: 82.30 [#/sec] (mean)
Time per request: 1215.040 [ms] (mean)
Time per request: 12.150 [ms] (mean, across all concurrent requests)
Transfer rate: 1099.02 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 3 25 4.0 26 27
Processing: 149 705 315.9 706 1187
Waiting: 134 694 316.7 705 1186
Total: 170 730 317.1 732 1212
Percentage of the requests served within a certain time (ms)
50% 732
66% 917
75% 1010
80% 1055
90% 1161
95% 1205
98% 1211
99% 1212
100% 1212 (longest request)
分别测试三次,取最好结果。
- 测试结果是 debian 的更好,但内存已经满了。 centos 则已经开始使用 swap 了。
- 系统负载方面也是 debian 更低,但微乎其微,至少和内存方面的差距比起来是这样。
- 测试结束后内存占用率回落也是 debian 更快, centos 回落速度慢是不是受 swap 硬盘速度的影响不得而知。
- centos 是 mariadb 而 debian 是 mysql ,不知道这是不是也是个影响因素。
在 debian 虚拟机下快照还原后重新安装
接下来测试 nginx+apache 和 单 nginx
nginx + apache
结果忘了保存,压力测试结果比单 apache 更好,资源占用比单 apache 更低。
单 nginx
Server Software: nginx/1.6.2
Server Hostname: 127.0.0.1
Server Port: 80
Document Path: /forum.php
Document Length: 12871 bytes
Concurrency Level: 100
Time taken for tests: 1.024 seconds
Complete requests: 100
Failed requests: 0
Total transferred: 1363960 bytes
HTML transferred: 1287100 bytes
Requests per second: 97.66 [#/sec] (mean)
Time per request: 1023.960 [ms] (mean)
Time per request: 10.240 [ms] (mean, across all concurrent requests)
Transfer rate: 1300.82 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 9 28 3.4 28 32
Processing: 56 519 288.0 503 991
Waiting: 45 514 287.2 501 991
Total: 73 547 290.2 531 1023
Percentage of the requests served within a certain time (ms)
50% 531
66% 720
75% 819
80% 866
90% 964
95% 1011
98% 1016
99% 1023
100% 1023 (longest request)
单 nginx 的测试结果是最好的,压力测试最好,资源占用最低
而且在压力测试参数增加后, nginx 的测试结果更好了,有点不解。
结果
现在决定用 debian + nginx + php-fpm + mysql 这个组合了,但也留下几个疑问:
- 为什么那么多人说 centos 要比 debian 稳定? centos 中的软件真的会依靠 backport 做到版本号老,但软件不旧吗?
- 为什么那么多人推荐 nginx 反代 apache 这种组合?因为后端运算集中的应用下 apache 更为强大?因为 nginx 使用 php-fpm 会出现 502 ?
- 为什么 debian 的 stable 源中的 php-gd 会是 dev 版?因为这个 dev 版已经经过测试足够稳定了?
- mariadb 能否取代 mysql ?把 mysql 换成 mariadb 是否更好?我对 mariadb 完全陌生,看来得多学习学习了:)
- 为什么压力测试时,单 nginx 压力越高,反而表现越好了?
- 有没有比 debian + nginx + php-fpm + mysql 这个组合更好的方案?似乎问题又回到了原点。。。