debian 与 centos, apache 与 nginx。(修正)

2016-03-25 23:01:06 +08:00
 yangxiu

重新编辑发一遍,之前的完全乱了

受人所托,要做一个比较正式的网站,可自己以前折腾都是 debian + lighttpd + php-fastcgi + sqlite 这样的奇葩组合,肯定不能这么用。

首先是系统,一直都是 debian 而且也没有什么不稳定的情况,但 centos 在很多人口中似乎都是:红帽亲儿子,企业,稳定。于是便纠结应该用自己熟悉的 debian 还是换到口碑似乎更好的 centos 。

第二是 web 服务器,自己用 lighttpd 只是为了低资源占用,普遍用的应该都是 apache 和 nginx 吧,但用法也有好多种。

  1. 单 apache ,并发弱 但 mod-php 性能好,资源占用高。
  2. 单 nginx ,并发强 但 php-fpm 会有 502 ,资源占用低。
  3. 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)

分别测试三次,取最好结果。

  1. 测试结果是 debian 的更好,但内存已经满了。 centos 则已经开始使用 swap 了。
  2. 系统负载方面也是 debian 更低,但微乎其微,至少和内存方面的差距比起来是这样。
  3. 测试结束后内存占用率回落也是 debian 更快, centos 回落速度慢是不是受 swap 硬盘速度的影响不得而知。
  4. 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 这个组合了,但也留下几个疑问:

  1. 为什么那么多人说 centos 要比 debian 稳定? centos 中的软件真的会依靠 backport 做到版本号老,但软件不旧吗?
  2. 为什么那么多人推荐 nginx 反代 apache 这种组合?因为后端运算集中的应用下 apache 更为强大?因为 nginx 使用 php-fpm 会出现 502 ?
  3. 为什么 debian 的 stable 源中的 php-gd 会是 dev 版?因为这个 dev 版已经经过测试足够稳定了?
  4. mariadb 能否取代 mysql ?把 mysql 换成 mariadb 是否更好?我对 mariadb 完全陌生,看来得多学习学习了:)
  5. 为什么压力测试时,单 nginx 压力越高,反而表现越好了?
  6. 有没有比 debian + nginx + php-fpm + mysql 这个组合更好的方案?似乎问题又回到了原点。。。
3018 次点击
所在节点    服务器
18 条回复
yangqi
2016-03-25 23:11:13 +08:00
centos 主要就是能花钱买到技术支持,对个人来说没用,但是对企业来说是非常重要的。
yylzcom
2016-03-25 23:11:18 +08:00
Debian 和 CentOS 自己熟悉用哪个,喜欢哪个用哪个,没必要纠结太多, MariaDB 和 MySQL 亦然。
感觉可以单跑 Nginx 了,性能更强易用性也不差,或者就 Nginx+Apache 的组合
shinko
2016-03-25 23:17:16 +08:00
呵呵,让新手看起来这是一篇很有技术含量严谨的测试。
lhbc
2016-03-25 23:22:30 +08:00
1. Red Hat 花这么多钱,这么多工程师,每个版本都经过大量测试,稳定性自然相对高, bug 也少点。另外持续的维持一个版本,兼容性无疑是最佳的,在维护周期内,再也不需要担心兼容问题和安全问题。部分软件有 backport ,但 backport 的 feature 其实不多。
2. 其实 99%的站还轮不到谈性能,只要不犯低级错误。
3. 不清楚
4. 对大多数人来说,没啥区别,除非你用到新特性。 Red Hat 敢在 RHEL 里替换掉 MySQL ,放心用即是。一般人嘛, yum 和 apt-get 是啥就用啥呗。
5. ab 的测试不准,你的结果存在明显错误。另外测试 100 个请求算不上测试,没过万别说话。
6. 少折腾,多喝热水,不行就重启,熟悉啥用啥。
yangxiu
2016-03-25 23:31:22 +08:00
@shinko
这个测试是很不严谨的我也知道,但是没有刻意让它“看起来很有技术含量很严谨”,各种测试条件我都如实说了
毕竟我是菜鸟一只,能力有限,希望能指出不足,我也希望能做一次更靠谱的测试
sujin190
2016-03-25 23:32:29 +08:00
其实,你每天没有上百万上千万的 request 请求,爱用啥用啥,没区别,话说 ab 100 似乎不能说明啥,随机误差都不能排除
kn007
2016-03-25 23:39:45 +08:00
系统看每个人习惯,至于安装源,可以选择编译。。。
shinko
2016-03-25 23:40:15 +08:00
@yangxiu 看你楼上的回复吧。顺便感谢一下他。重点还是第六条
yangxiu
2016-03-25 23:40:44 +08:00
@lhbc
正式服务器是一台 E3-1230 和 4G 内存的独立服务器,这边我先测试下,电脑性能不好虚拟机只敢分配 512M 内存,上万的话是必死无疑吧。。。

ab 测试中的错误是?

自己只有几个小打小闹的站,独立服务器都没碰过,更没弄过这么大的,只想尽力做好
skydiver
2016-03-25 23:42:04 +08:00
>单 nginx ,并发强 但 php-fpm 会有 502 ,资源占用低。

502 ?这是陈年老皇历了吧。。

公司里用了这么多年了,只有代码写错的时候才有 50x
yangxiu
2016-03-25 23:48:17 +08:00
@skydiver
baidu+google 看到的,可能是早前的吧
gotounix
2016-03-25 23:56:14 +08:00
1.虚拟机测试没有意义。
2.小内存 VPS 不要上反代。
3.非关键应用先试运行,遇到性能瓶颈再调优。
lhbc
2016-03-25 23:56:56 +08:00
@yangxiu
1. ab 太旧了,不适宜用来测试 web 性能。现在应该用 wrk, siege
2. 100 个请求,完全不具有参考价值。应该按并发量逐渐增加,请求总数应该过完,然后把结果画曲线。你的结果是错误的,很可能是请求量太少造成的误差。
3. 上独服还得整硬件,直接云就完事了,性能不够直接加 CPU 内存。
4. 如果是用 PHP ,直接 PHP7 就完事了。
yangxiu
2016-03-26 00:21:05 +08:00
@gotounix
实机测试估计是没可能了,因为不知道选哪个所以只能先测试,只能虚拟机
虚拟机测试没有意义是因为?
yangxiu
2016-03-26 00:26:20 +08:00
@lhbc
楼上说虚拟机测试没意义是真的吗?不是的话明天我换工具重新测试一遍
nginx + php7 直接用?是指用 php7 就不用 fpm 了吗?我去了解下
ryd994
2016-03-26 03:06:57 +08:00
@yangxiu 不是,是让你不要纠结 php 版本号,直接用 7 而不是 5/6
直接 php 一个是不安全,另一个对静态资源不是浪费么
ryd994
2016-03-26 03:21:23 +08:00
"为什么那么多人推荐 nginx 反代 apache 这种组合?"
你要考虑多台服务器的情况
即使是内网,后端暴露 http 也比直接暴露 fpm 好
更何况有些时候后端上也有静态资源
更何况 apache 有些模块挺方便

其实用 nginx+nginx+php 也是可以的
不过既然能横向扩展,那多点性能少点性能无所谓了,而 apache 对很多人来说比 nginx 方便
jookie
2016-03-31 16:49:40 +08:00
freebsd. nginx php. mysql.

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/266404

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX