Apache自动优化前端页面的模块mod_pagespeed安装
mod_pagespeed是一个适用于apache httpd的模块,由google发布的开源代码,目前只支持apache2.2版本,可以自动完成优化工作,包括优化缓存,减少客户端服务器之间的通讯,降低负载等等。部署mod_pagespeed不用修改任何程序,安装完成后重启apache,一切前端问题自动优化。CDN服务商Cotendo已在其CDN服务器上部署mod_pagespeed以加速客户网站访问,图片文件大小经自动压缩可减少20%-30%,页面加载时间最多可缩短50%。GoDaddy也宣布将在其客户网站服务器上广泛部署mod_pagespeed。mod_pagespeed功能:1.Optimize Caching优化缓存 Extend Cache扩展缓存 Outline CSS Outline JavaScript 2.Minimize Round Trip Times最大限度地减少往返时间Combine CSS 合并CSSInline CSS 内嵌CSSInline JavaScript 内嵌JavaScript3.Minimize Payload Size最小化有效载荷尺寸Collapse Whitespace 压缩空白Combine Heads 合并头信息Elide Attributes 省略属性Minify Javascript 缩小JavascriptOptimize Images 优化图片Remove Comments 删除注释Remove Quotes 删除引用Rewrite CSS 重写CSSMove CSS to HEAD 加载CSS到headAdd Head Add Instrumentation 从源码安装mod_pagespeed需要的环境:apache版本2.2以及以上g++版本4.2以及以上subversionmake依赖关系:安装depot_tools,用来建立多个开源项目与其它开源项目的依赖关系。# mkdir ~/bin# cd ~/bin# svn co http://src.chromium.org/svn/trunk/tools/depot_tools将depot_tools加入到PATH路径中# exportPATH=$PATH:~/bin/depot_tools下载mod_pagespeed并解除依赖关系:# mkdir ~/mod_pagespeed# cd ~/mod_pagespeed #gclient config http://modpagespeed.googlecode.com/svn/tags/0.9.11.5/src #gclient sync --force //下载mod_pagespeed源码文件编译mod_pagespeed:# cd ~/mod_pagespeed/src # make V=1安装mod_pagespeed:# cd install consolas# vim Makefile //更改有关apache路径APACHE_ROOT = /usr/local/apache2.2.15APACHE_MODULES = $(APACHE_ROOT)/modulesAPACHE_DOC_ROOT = /wwwAPACHE_CONTROL_PROGRAM = /usr/local/apache2.2.15/bin/apachectlSTAGING_DIR = /tmp/mod_pagespeed.installMOD_PAGESPEED_FILE_ROOT = /usr/local/apache2.2.15/mod_pagespeedAPACHE_USER = nobodyAPACHE_CONF = $(APACHE_ROOT)/conf/extra# make stagingrm -rf /tmp/mod_pagespeed.installmkdir -p /tmp/mod_pagespeed.installsed -e s@APACHE_DOC_ROOT@/www@g \ -e s!@@MODPAGESPEED_CACHE_ROOT \@@!/usr/local/apache2.2.15/mod_pagespeed!g \-e "s@# ModPagespeedSlurpDirectory ... \@#ModPagespeedSlurpDirectory ...@g" -e "s@# ModPagespeedSlurpReadOnly on@#ModPagespeedSlurpReadOnly on@g" \-e "s@^#STRESS.*@@" common/pagespeed.conf.template \debug.conf.template > /tmp/mod_pagespeed.install/pagespeed.confsed -e s@HOSTNAME@132file@g -e s@APACHE_MODULES \@/usr/local/apache2.2.15/modules@g proxy.conf.template > \ /tmp/mod_pagespeed.install/proxy.confecho "LoadModule \ pagespeed_module /usr/local/apache2.2.15/modules/mod_pagespeed.so" \ > /tmp/mod_pagespeed.install/pagespeed.loadecho "LoadModule deflate_module /usr/local/apache2.2.15/modules/mod_deflate.so" \ >> /tmp/mod_pagespeed.install/pagespeed.loadcp -rp mod_pagespeed_example /tmp/mod_pagespeed.installcp ../out/Release/libmod_pagespeed.so /tmp/mod_pagespeed.install/mod_pagespeed.so# make install mkdir -p /usr/local/apache2.2.15/mod_pagespeed/cachemkdir -p /usr/local/apache2.2.15/mod_pagespeed/fileschown -R nobody /usr/local/apache2.2.15/mod_pagespeedcat /tmp/mod_pagespeed.install/pagespeed.load \ /tmp/mod_pagespeed.install/pagespeed.conf \ > /usr/local/apache2.2.15/conf/extra/pagespeed.confrm -rf /www/mod_pagespeed_examplecp -r /tmp/mod_pagespeed.install/mod_pagespeed_example /wwwchown -R nobody /www/mod_pagespeed_examplecp /tmp/mod_pagespeed.install/mod_pagespeed.so /usr/local/apache2.2.15/modules# vim httpd.confInclude conf/extra/pagespeed.conf# make stop startsudo /usr/local/apache2.2.15/bin/apachectl stopsudo /usr/local/apache2.2.15/bin/apachectl start 检查mod_pagespeed模块是否加载成功:# ./apachectl -t -D DUMP_MODULES | grep pagespeed Syntax OK pagespeed_module (shared) 监视mod_pagespeed状态信息: http://ip/mod_pagespeed_statistics