6月 172014
 

Time is money. This somewhat shallow and overused saying fits perfectly well into any on-line business. Users are impatient and every millisecond brings them closer to leaving your website.

If you’ve ever been trying to squeeze more out of hardware you must have come across Nginx (engine x). Nginx usually appears in context of PHP-FPM (FastCGI Process Manager) and APC (Alternative PHP Cache). This setup is often pitched to be the ultimate combo for a web server but what that really means? How much faster a PHP application is going to be on a different web server? I had to check it and the answer as often is – that depends.

I benchmarked three different types of PHP software:

– Large application based on Zend Framework 1
– Small PHP script
– WordPress

The software was hosted on Amazon EC2 large instance. All benchmarks were run from EC2 Tiny instance to be as close as it possible to the web server.

To make sure I benchmark web servers instead of disk I/O I set all logs to go to memory (/dev/shm). PHP sessions were directed to memcached.
Both servers were using Zend Optimizer Plus with opcache.revalidate_freq set to 1 hour. I use Zend Optimizer because APC wasn’t stable for me with PHP 5.4.x.
If you aren’t familiar with PHP accelerators they convert PHP scripts into byte code and keep them in shared memory. That brings significant boost of performance (40-90%) because PHP scripts don’t have to be read from a disc and parsed on every request. Using the accelerator helped me removing I/O from the equation.

It’s very important to make sure Apache won’t read .htaccess. You can achieve it by setting Allow Overwrite to none. Parsing htaccess in real time will drop performance on the Apache side. It’s recommended to not use this file in production environment. Continue reading »

12月 192012
 

1.重要的就是RAM,对应到参数设置,就是要设置合理的Maxclients值,避免进程运行在swap里。

2.HostnameLookups 要设置为Off,避免处理每次请求前还要查询dns

3.避免使用SymLinksIfOwnerMatch,即除非必要,一般最好全部设置成FollowSumLinks

Options FollowSymLinks

4.避免使用AllowOverride,即除非必要,则最好全部设置为None Continue reading »

5月 262012
 

为什么服务器在连续运行多天后或访问峰值后,进程中的一个Apache.exe占用内存几百兆不减少?
用记事本打开apache2\conf\httpd.conf,
我在centos5上 httpd.conf在 /etc/httpd/conf/目录下。
查找MaxRequestsPerChild,将MaxRequestsPerChild 0改成MaxRequestsPerChild 50即可。 Continue reading »

10月 152011
 

话说nginx在大压力的环境中比apache的表现要好,于是下载了一个来折腾一下。

下载并编译安装,我的编译过程有点特别:

1。去除调试信息,修改$nginx_setup_path/auto/cc/gcc这个文件,将 CFLAGS=”$CFLAGS -g”  这一行注释掉。

2。由于仅测试WEB服务器的性能,所以不安装FastCGI。 Continue reading »

11月 292010
 

一、使用mod_gzip 和 mod_deflate对网页内容进行压缩

Gzip是一种流行的文件压缩算法,现在的应用十分广泛,尤其是在Linux平台。当应用Gzip压缩到一个纯文本文件时,效果是非常明显的,大约可以减少70%以上的文件大小。这取决于文件中的内容。

利用Apache中的Gzip模块,我们可以使用Gzip压缩算法来对Apache服务器发布的网页内容进行压缩后再传输到客户端浏览器。这样经过压缩后实际上降低了网络传输的字节数,最明显的好处就是可以加快网页加载的速度。

网页加载速度加快的好处不言而喻,除了节省流量,改善用户的浏览体验外,另一个潜在的好处是Gzip与搜索引擎的抓取工具有着更好的关系。例如 Google就可以通过直接读取gzip文件来比普通手工抓取更快地检索网页。在Google网站管理员工具(Google Webmaster Tools)中你可以看到,sitemap.xml.gz 是直接作为Sitemap被提交的。

而这些好处并不仅仅限于静态内容,PHP动态页面和其他动态生成的内容均可以通过使用Apache压缩模块压缩,加上其他的性能调整机制和相应的服务器端缓存规则,这可以大大提高网站的性能。

因此,对于部署在Linux服务器上的PHP程序,在服务器支持的情况下,我们建议你开启使用Gzip Web压缩。

二、 Gzip Web压缩工作原理

Web服务器处理HTTP压缩的过程如下: Continue reading »

9月 182010
 

最近,帮一个朋友优化一个拥有20万主题,100万帖子,3万多会员,平均在线人数2000人的Discuz!论坛,采用Linux2.6+Apache2+mod_php5+MySQL5,服务器配置为双至强+4G内存,优化前,系统平均负载(load average)基本维持在10以上,MySQL的CPU占用率基本在90%以上,优化后,系统平均负载降到0.5以下,MySQL的CPU占用率很少有超过10%的时候。优化前YSlow得分只有35分,优化后YSlow得分86分。现将优化的过程和经验做一个记录:

首先,对Apache进行优化,编辑httpd.conf,设置HostnameLookups、KeepAlive、 MaxKeepAliveRequests以及KeepAliveTimeout四个参数,调整MaxSpareServers、 ServerLimit、MaxClients以及MaxRequestsPerChild参数,还可以考虑弃用prefork而采用worker MPM。设置mod_deflate及mod_expires模块,不过注意Discuz!不能对PHP文件开启Expires,否则会出现问题。另外还可以考虑开启mod_cache和mod_mem_cache模块。另外利用cronolog按天对日志进行轮循截断,如果日志特别大,也可以按小时截断。另外再加上Awstats对日志进行分析,并用gzip对日志进行压缩,自动删除1个月前的日志。

其次,对PHP进行优化,编辑php.ini,调整output_buffering、zlib.output_compression及 max_execution_time、max_input_time、memory_limit等参数,并安装Xcache和Zend Optimizer。

然后对MySQL进行优化。 Continue reading »

9月 182010
 

Apache 是老牌子的网站服务器,论功能和插件,是无出其右,但是论性能,很多新手已经把 Apache 甩在后面,如 lighttpd, Nginx。在更换Web服务器软件这类激进的动作之前,你应该考虑一下:如果您拥有足够多的内存来应付现有的并发访问的话,建议你还是继续使用 Apache,但是作些性能上的调校是应该的。

关于 ServerLimit  和 MaxClients
MaxClients 参数决定了 Apache 最大并发访问数。

对于 mpm_prefork,如果 MaxClients 设置超过了 256,就需要对 ServerLimit 设置,ServerLimit 应该和 MaxClients 一样,至少不小于 MaxClient。

对于 mpm_worker,如果 MaxClients 除以 ThreadsPerChild  超过了 16,就要对 ServerLimit 进行设置。ServerLimit 乘以 ThreadsPerChild 要大于等于 MaxClients。

MaxClients 不是随意设定的
像 Apache 这样重量级的软件,内存消耗是很大的。MaxClients 的计算规则是 Apache 可占用的物理内存,除以单个 Apache进程的内存占用。

因此并发数直接取决于内存的大小。然而如果内存足够大,而配置文件没有跟上,岂不是吃亏? Continue reading »