<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>阅微堂 &#187; 加速</title>
	<atom:link href="http://zhiqiang.org/blog/tag/%e5%8a%a0%e9%80%9f/feed" rel="self" type="application/rss+xml" />
	<link>http://zhiqiang.org/blog</link>
	<description>数学、金融、计算机</description>
	<lastBuildDate>Sat, 19 May 2012 13:47:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>加快WordPress的页面生成和载入速度</title>
		<link>http://zhiqiang.org/blog/it/wordpress-accelerate-the-pace-of-pages-printed-in-the-generation-and.html</link>
		<comments>http://zhiqiang.org/blog/it/wordpress-accelerate-the-pace-of-pages-printed-in-the-generation-and.html#comments</comments>
		<pubDate>Sat, 26 Aug 2006 05:31:06 +0000</pubDate>
		<dc:creator>zhiqiang</dc:creator>
				<category><![CDATA[IT技术]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[WP-Cache 2.0]]></category>
		<category><![CDATA[优化]]></category>
		<category><![CDATA[加速]]></category>
		<category><![CDATA[静态化]]></category>

		<guid isPermaLink="false">http://zhiqiang.org/blog/420.html</guid>
		<description><![CDATA[博客 » IT技术 » WordPress，WP-Cache 2.0，优化，加速，静态化 » 前一阵，此blog的页面打开速度慢的要命，我每次打开都需要大约10秒钟的时间。而通过Google Analytics的数据看到，70%+用户在这个blog的停留时间还不到十秒:(。所以我决定加速一下我的blog，后来看起来似乎有些效果。下面是我用的一些方法，希望对后来人有些用处。 安装WP-Cache 2.0 WP-Cache 2.0可以将页面缓存，在短时间内...]]></description>
			<content:encoded><![CDATA[<p id="breadcrumb" class="breadcrumb"><a href="http://zhiqiang.org/blog/">博客</a> » <a href="http://zhiqiang.org/blog/category/it">IT技术</a> » <a href="http://zhiqiang.org/blog/tag/wordpress" rel="tag">WordPress</a>，<a href="http://zhiqiang.org/blog/tag/wp-cache-20" rel="tag">WP-Cache 2.0</a>，<a href="http://zhiqiang.org/blog/tag/%e4%bc%98%e5%8c%96" rel="tag">优化</a>，<a href="http://zhiqiang.org/blog/tag/%e5%8a%a0%e9%80%9f" rel="tag">加速</a>，<a href="http://zhiqiang.org/blog/tag/%e9%9d%99%e6%80%81%e5%8c%96" rel="tag">静态化</a> » </p><p>前一阵，此blog的页面打开速度慢的要命，我每次打开都需要大约10秒钟的时间。而通过Google Analytics的数据看到，70%+用户在这个blog的停留时间还不到十秒:(。所以我决定加速一下我的blog，后来看起来似乎有些效果。下面是我用的一些方法，希望对后来人有些用处。</p>
<h3><font size="3">安装</font><a href="http://mnm.uib.es/gallir/wp-cache-2/">WP-Cache 2.0</a></h3>
<p>WP-Cache 2.0可以将页面缓存，在短时间内(默认为1个小时，可以在后台设置)的再次浏览，将直接发送缓存的页面，相当于将WordPress给静态化了。</p>
<p>好处是显然的。而且这个版本的WP-Cache 2.0在缓存的时候根据页面URL+Cookie进行缓存的，所以它不会影响COOKIE的使用。注意，WP-Cache 2.0只读取Cookie中的email地址，然后根据email地址判定是否属于同一个用户。由于大多数人都没有留下Email，所以我修改成根据Cookie中的用户名来判断，将插件目录下的wp-cache-phase1.php的第67行改成：</p>
<blockquote>
<p align="left">//if (preg_match("/^wordpress|^comment_author_email_/", $key)) {<br />
if (preg_match("/^wordpress|^comment_author_/", $key)) {</p>
</blockquote>
<p align="left">由于大部浏览者的cookie都是空的，所以这个插件会加快这大部分的浏览速度。另外，这个插件还会侦测文章更新，实时更新缓存等。</p>
<p align="left">但是缺点也是显然的。首先，在页面还没有生成的时候，就进行COOKIE的读取和交换，将降低速度，某种程度上来说，它使得头次浏览的速度更慢。再者，这个插件在运行的时候，还是得先把其他插件初始化，所以它只是减少了页面生成的函数调用的时间。</p>
<p align="left"><strong>效果：不明显。</strong>事实上，这个插件主要是用来降低系统CPU占用量的，对页面载入速度影响有限。</p>
<h3>改善插件</h3>
<p>我并没有安装太多插件，而且都是些小型的插件，从来不装像UTW那样的巨无霸插件。但是我还是把那些非插件必须的文件(大部分是我修改插件的时候新加的)都移出插件目录。以免WP错把它们当成插件文件进行初始化处理。</p>
<p><strong>效果不明</strong>。因为经过处理的文件不多也不大，想必影响有限。</p>
<h3>页面静态化</h3>
<p>一个不变的事实是：html页面永远要比php页面快的多，而且几乎不占用CPU资源。所以将页面静态化是一个好主意。但将所有页面静态化，没有这个必要。所以我选择了将blog主页进行静态化处理——各位如果不信的话，直接页面刷新，然后看一下右边的随机文章变了没有:)。</p>
<p>我同学告诉我页面静态化有很多种方法，但我决定采用最土的一种方法：</p>
<p>首先，在根目录下建立一个cron.sh文件，里面的内容是：</p>
<blockquote><p>wget -O /home/aaaa/zhiqiang.org/blog/index.html http://zhiqiang.org/blog/index.php</p></blockquote>
<p>上面的/home/aaaa是我的根目录，其中aaaa是用户名。再使用Fterm SSH登录远程主机，输入</p>
<blockquote><p>crontab -e</p></blockquote>
<p>在出来的编辑界面中输入</p>
<blockquote><p>*/10 * * * * /home/aaaa/cron.sh</p></blockquote>
<p>再保存就OK了。这样，系统会自动地每10分钟一次将blog的动态主页index.php转成静态的index.html，以后访问主页的时候就会直接访问index.html，从而实现了blog主页面的html静态化。</p>
<p>缺点：COOKIE失效。不过山人自有妙计，<a target="_blank" href="http://www.leexuan.com/?f">使用javascript本地读取COOKIE</a>然后填入表格即可。</p>
<p><strong>效果：明显</strong>。</p>
<h3><font size="3">合并JS和CSS文件</font></h3>
<p>浏览页面的时候，把页面保存下来，便能看到这个页面含有的文件。文件太多，会大大降低浏览速度。而WordPress的插件系统使得JS和CSS文件很多，所以，应当合并一下JS和CSS文件。</p>
<p>手动合并是一个方法，但是容易弄错，而且不太好修改。另一个方法是建立一个新的all.js.php文件，内容是：</p>
<blockquote><p><span style="color: #0000ff">&lt;?</span>php<br />
require_once('目录/wp-blog-header.php' ) ;<br />
<a style="color: #0000ff" href="http://cn.php.net/manual/en/function.include.php">include</a> ("<span style="color: #8b0000">/blog/a.js</span>" ) ;<br />
<a style="color: #0000ff" href="http://cn.php.net/manual/en/function.include.php">include</a> ("<span style="color: #8b0000">/b.js</span>" ) ;<br />
<span style="color: #0000ff">?&gt;</span></p></blockquote>
<p>就是将header.php里面的那些js文件都include到all.js.php，然后在header里面包含all.js.php文件即可：</p>
<blockquote><p>&lt;script xsrc="<span style="color: #8b0000">all.js.php</span>" type="<span style="color: #8b0000">text/javascript</span>"/&gt;</p></blockquote>
<p>另外还要做的一件事情是需要把原来那些js文件从head里面去掉。通常这些js文件都是通过add_action('wp_head','fun' ) ;这样的钩子添加到head里面的。在fun函数的定义处去掉对应的代码，或者直接把这条语句删除即可。</p>
<p>对CSS文件也可以用同样的处理方式。CSS文件一般都不多，这时候可以直接手动把它们都贴到一起。</p>
<p>另外js文件的载入位置也很重要，放在越后面越好，让浏览器先下载html代码。像Google Analytics的js代码最好放在文件最后。</p>
<p><strong>效果：非常明显</strong>。</p>
<p>另外，太多的图片也会降低载入速度，而我这个模板的图片就比较多，而且我已经把能不用的图片去掉了。想着是不是该换模板了。</p>
<div><h4>相关文章</h4><ul><li class='currentpost'><a href="http://zhiqiang.org/blog/it/wordpress-accelerate-the-pace-of-pages-printed-in-the-generation-and.html">加快WordPress的页面生成和载入速度</a></li><li ><a href="http://zhiqiang.org/blog/it/blog-fast-download-speeds-to-be-faster-and-the-speed-of-blog.html">blog下载速度多快才算快和blog速度优化</a></li><li ><a href="http://zhiqiang.org/blog/it/a-new-static-wordpress-plug-cos-of-cache-html.html">一个新的WordPress静态化插件cos-html-cache</a></li><li ><a href="http://zhiqiang.org/blog/it/optimization-toolbox-in-matlab.html">Matlab的优化工具箱</a></li><li ><a href="http://zhiqiang.org/blog/it/integer-programming-solving-sudoku.html">整数规划思想求解数独游戏</a></li><li ><a href="http://zhiqiang.org/blog/it/all-articles-into-msn-spaces.html">导入MSN Spaces所有文章</a></li><li ><a href="http://zhiqiang.org/blog/it/my-experiences-and-views-blog.html">我的Blog经历和一些看法</a></li><li ><a href="http://zhiqiang.org/blog/it/bbpress-installation-problems-and-several-plug-ins.html">bbPress的安装问题和几个插件</a></li><li ><a href="http://zhiqiang.org/blog/it/wordpress-blog-has-different-domains.html">同一WordPress的blog可拥有多个域名</a></li><li ><a href="http://zhiqiang.org/blog/science/computer-science/move-blogbus-data-to-wordpress.html">BlogBus搬家文件转WordPress导入文件</a></li></ul></div>    <p></p>
    <hr noshade style="margin:0;height:1px" />
    <p>&copy; zhiqiang for <a href="http://zhiqiang.org/blog">阅微堂</a>, 2006. | <a href="http://zhiqiang.org/blog/it/wordpress-accelerate-the-pace-of-pages-printed-in-the-generation-and.html">&#38142;&#25509;</a> | <a href="http://zhiqiang.org/blog/it/wordpress-accelerate-the-pace-of-pages-printed-in-the-generation-and.html#comments">52 &#26465;&#35780;&#35770;</a></p>]]></content:encoded>
			<wfw:commentRss>http://zhiqiang.org/blog/it/wordpress-accelerate-the-pace-of-pages-printed-in-the-generation-and.html/feed</wfw:commentRss>
		<slash:comments>52</slash:comments>
		</item>
	</channel>
</rss>

