鉴于论坛里有会员对NGINX、APACHE各自与各自的方式运行PHP的性能有疑问,我从国外转载并翻译一篇文章并在后面发表一点自己的看法,希望对大家有帮助。
原文: http://blog.a2o.si/2009/06/24/apache-mod_php-compared-to-nginx-php-fpm/
所 有的软件均从源码编译(详见如下)。压测工具使用APACHE的ApacheBench(ab),下面的结果是在同一台机器上测试的,所有伺服系统 (nginx、apache)均关闭了日志功能(防止对性能的影响),测试过程中keepalive配置的是一次启用、一次禁用,每样测试均测试5遍,然 后取得的平均值。测试的文件如下:
HelloWorld.php ? 简短的输出“Hello, World!” (13 bytes)的PHP脚本,用来代表PHP文件测试处理过程的开销,相比的静态文件在下面
HelloWorld.txt ? 一个静态文件,输出“Hello, World!” (同样13 bytes),用来代表静态文件测试处理过程的开销
100KB.txt ? 一个静态的100KB大小的文件
1MB.txt ? 一个静态的1MB 大小的文件
index.php ? 一个包含多重复杂的处理过程的PHP文件,包括数据库查询、文件缓存读取、模板[编译]处理。
硬件环境
硬件: HP DL380 G5
硬件CPU: 2x Intel Xeon E5420 (4 cores each, total of 8 cores)
硬件内存: 8GB of ECC RAM
硬件磁盘: Smart Array P400i RAID-1 with 2x 147GB SAS drives
操作系统: Slackware 12.2 with almost all software compiled from source
文件系统: ext3
Apache 版本: 2.2.11, php 由mod_php方式运行
Nginx 版本: 0.7.59, php 由 php-fpm (通过socket方式运行)
PHP 版本: 5.2.9
Eaccelerator 版本: 0.9.5.3 (for both, Apache and Nginx)
MySQL 版本: 5.0.77
OpenSSL 版本: 0.9.8k
服务日志、请求日志均已禁用
Apache编译选项:
./configure ?prefix=/usr/local/$PDESTDIR_HTTPD ?sysconfdir=/etc/httpd \
?enable-authn-file ?enable-authn-default \
?enable-authz-host ?disable-authz-groupfile ?enable-authz-user ?enable-authz-default \
?enable-auth-basic \
?disable-include ?disable-filter ?disable-charset-lite \
?enable-log-config \
?enable-env ?enable-setenvif \
?enable-ssl ?with-ssl=/usr/local/openssl-$PVERSION_OPENSSL \
?enable-http ?enable-mime ?enable-status \
?disable-autoindex ?disable-asis \
?enable-info \
?enable-cgi ?disable-cgid \
?enable-vhost-alias \
?disable-negotiation \
?enable-dir \
?disable-actions \
?disable-userdir \
?enable-info \
?enable-rewrite \
?enable-so \
?with-mpm=prefork
Nginx编译选项:
./configure ?prefix=/usr/local/$PDIR \
?conf-path=/etc/nginx/nginx.conf \
?error-log-path=/var/log/nginx/nginx_error.log \
?pid-path=/var/run/nginx.pid \
?lock-path=/var/run/nginx.lock \
?user=httpd \
?group=httpd \
?with-openssl=/usr/local/openssl-0.9.8k
PHP编译选项(与APACHE一起运行的MOD_PHP):
??[These lines are for PHP with Apache (mod_php)]?????-
./configure ?prefix=/usr/local/$PDESTDIR_HTTPD/$PDIR \
?with-apxs2=/usr/local/$PDESTDIR_HTTPD/bin/apxs ?enable-cli ?enable-cgi \
?with-config-file-path=/etc/php/httpd \
与NGINX一起运行的(php-fpm)
??[These lines are for PHP with Nginx (php-fpm)]?????-
./configure ?prefix=/usr/local/php-fpm \
?enable-cli ?enable-fastcgi ?enable-fpm \
?with-fpm-conf=/etc/php/php-fpm/php-fpm.conf \
?with-fpm-log=/var/log/php-fpm.log \
?with-fpm-pid=/var/run/php-fpm.pid \
?with-config-file-path=/etc/php/php-fpm \
PHP共同配置
??[These lines are common for both]?????-
?disable-short-tags \
?disable-ipv6 \
?disable-all \
\
?enable-libxml \
?with-openssl=/usr/local/openssl-$PVERSION_OPENSSL \
?with-pcre-regex \
?with-zlib \
?with-bz2 \
?with-curl ?with-curlwrappers \
?enable-dba=shared ?with-db4 ?enable-inifile ?enable-flatfile \
?enable-dom ?with-libxml-dir \
?enable-filter \
?enable-ftp \
?with-gd ?with-jpeg-dir ?with-png-dir ?with-freetype-dir \
?with-gettext \
?enable-hash ?with-mcrypt \
?with-iconv=/usr/local/lib ?with-iconv-dir=/usr/local/lib \
?with-imap=/usr/local/imap-$PVERSION_CYRUSIMAP ?with-imap-ssl \
?enable-json \
?enable-mbstring ?enable-mbregex ?enable-mbregex-backtrack \
?with-mysql=/usr/local/mysql-$PVERSION_MYSQL ?with-mysqli=/usr/local/mysql-$PVERSION_MYSQL/bin/mysql_config \
?enable-pdo ?with-pdo-mysql=/usr/local/mysql-$PVERSION_MYSQL ?with-pdo-sqlite ?enable-sqlite-utf8 \
?enable-reflection \
?enable-session ?with-mm \
?enable-shmop \
?enable-simplexml \
?enable-soap \
?enable-sockets \
?enable-spl \
?with-regex \
?enable-sysvmsg ?enable-sysvsem ?enable-sysvshm \
?enable-tokenizer \
?enable-xml ?enable-xmlreader ?with-xmlrpc ?enable-xmlwriter ?with-xsl \
?enable-zip \
\
?with-pear \
?enable-zend-multibyte
配置文档:
Apache mod_php: httpd.conf http://blog.a2o.si/wp-content/uploads/2009/06/httpd.conf.txt
Apache mod_php: php.ini http://blog.a2o.si/wp-content/uploads/2009/06/php.ini.txt
Nginx php-fpm: nginx.conf http://blog.a2o.si/wp-content/uploads/2009/06/nginx.conf.txt
Nginx php-fpm: php-fpm.conf http://blog.a2o.si/wp-content/uploads/2009/06/php-fpm.conf.txt
Nginx php-fpm: php.ini与mod_php方式相同
测试结果:
HelloWorld.php
在 这里你可以看到每个PHP请求施加的开销。有趣的的是一个事实是Apache在这个测试执行的更好而且好得多。这里的原因是,Apache的PHP“内 置”通过mod_php,并且模块处理。另一方面Nginx的代理PHP请求到另一个应用程序服务器(PHP-FPM)。 Nginx的性能在上面的图是大约为apache的一半,容易解释。这里的PHP只是简单输出字符串。
[font='Lucida Grande', Verdana, Arial, sans-serif]HelloWorld.txt
[font='Lucida Grande', Verdana, Arial, sans-serif]
[font='Lucida Grande', Verdana, Arial, sans-serif]
[font='Lucida Grande', Verdana, Arial, sans-serif]
在本次测试的Apache开始落后。 NGINX的性能超过了apache性能的两倍。这项测试是展示静态文件服务的开销。
[font='Lucida Grande', Verdana, Arial, sans-serif]100KB.txt
[font='Lucida Grande', Verdana, Arial, sans-serif]
[font='Lucida Grande', Verdana, Arial, sans-serif]
在 这里的测试已经接近生产应用中的静态文件了(100KB),而且,我们能够证明,Nginx 禁用keepalive后与Apache 启用KeepAlive压力发现NGINX仍优于APACHE约两倍。此次测试的吞吐量约为1.2GB/s,但是所有测试均未真正出因特网(测试是在局域 网进行的)。
[font='Lucida Grande', Verdana, Arial, sans-serif]1MB.txt
这次测试没所有伺服均未开启keep alive,每个请求都要新建立连接,但相比于数据传输,这新建连接的开销就无所谓了。
真正意义上的PHP程序:
或许你会惊奇地看到,apache跑真实的、复杂点的PHP程序的时候,性能不及NGINX。
但原文作者或许没有注意到,NGINX PHP这时执行的时候肯定有不少是弹了回错误了的(而且作者没开启日志,测试过后没查证)
作者在原文只是这样说到:
apache开启.htaccess(NGINX没有这个功能)过后,会导致连接数据库的性能有所下降。
后面还有两项测试(分别是比较APACHE自己处理静态与自己处理动态的能力和NGINX的能力。)我就不在这里翻译了。
总结:
APACHE处理静态的能力不及NGINX,但NGINX处理PHP的稳定性不及apache mod_php。
如果取长补短哩?
NGINX做前端,APACHE做后端。NGINX proxy_pass apache,然后两个伺服doc_root配成一致,并让静态文件直接让NGINX输出,只转发PHP的请求给后端apache处理。
有会员的疑问:两个伺服,根本是多此一举。
回答:我的目标是高承载能力 稳定同时兼顾。而不是舍一取一。

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools