原来弄好的LNMP环境,因为php版本太新,而原来使用的eAccelerator因为很久没有更新了,所以无法支持php的新版本。所以想到使用xcache来进行php的加速。首先我用到的是XCache 2.0.0,可以支持最新的php 5.4。

1wget http://xcache.lighttpd.net/pub/Releases/2.0.0/xcache-2.0.0.tar.gz
2tar -zxvf xcache-2.0.0.tar.gz
3cd xcache-2.0.0
4/usr/local/php/bin/phpize
5./configure --enable-xcache --with-php-config=/usr/local/php/bin/php-config
6make
7make test

不过在安装的时候遇到了下面的问题:

1> +————————————————————————+
2> 
3>  | ! ERROR ! |
4> 
5>  | The test-suite requires that proc\_open() is available. |
6> 
7>  | Please check if you disabled it in php.ini. |
8> 
9>  +———————————————————————+

通过上面的提示,不难发现是因为我在php的安全设置中禁用了proc_open函数的调用。我在php.ini中禁用的调用函数如下:

1disable_functions=passthru,exec,system,chroot,scandir,chgrp,chown,ini_alter,proc_open,shell_exec,proc_get_status,ini_alter,ini_restore,dl,pfsockopen,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server,fsocket,fsockopen

在取消了proc_open函数的调用后,再进行make test的过程中,发现还有下面的报错:

 1PHP Warning:  shell_exec() has been disabled for security reasons in /usr/local/src/site/xcache-2.0.0/run-tests.php on line 284
 2PHP Warning:  shell_exec() has been disabled for security reasons in /usr/local/src/site/xcache-2.0.0/run-tests.php on line 285
 3PHP Warning:  shell_exec() has been disabled for security reasons in /usr/local/src/site/xcache-2.0.0/run-tests.php on line 299
 4=====================================================================
 5PHP Warning:  proc_get_status() has been disabled for security reasons in /usr/local/src/site/xcache-2.0.0/run-tests.php on line 1128
 6PHP Warning:  proc_get_status() has been disabled for security reasons in /usr/local/src/site/xcache-2.0.0/run-tests.php on line 1128
 7PASS include absolute path [tests/xcache_include_absolute.phpt]
 8PHP Warning:  proc_get_status() has been disabled for security reasons in /usr/local/src/site/xcache-2.0.0/run-tests.php on line 1128
 9PHP Warning:  proc_get_status() has been disabled for security reasons in /usr/local/src/site/xcache-2.0.0/run-tests.php on line 1128
10PASS include relative to current working dir [tests/xcache_include_relative_cwd.phpt]
11PHP Warning:  proc_get_status() has been disabled for security reasons in /usr/local/src/site/xcache-2.0.0/run-tests.php on line 1128
12PHP Warning:  proc_get_status() has been disabled for security reasons in /usr/local/src/site/xcache-2.0.0/run-tests.php on line 1128
13PASS include relative to current file [tests/xcache_include_relative_file.phpt]
14PHP Warning:  proc_get_status() has been disabled for security reasons in /usr/local/src/site/xcache-2.0.0/run-tests.php on line 1128
15PASS xcache_is_autoglobal [tests/xcache_is_autoglobal.phpt]
16PHP Warning:  proc_get_status() has been disabled for security reasons in /usr/local/src/site/xcache-2.0.0/run-tests.php on line 1128
17FAIL xcache_set/get test [tests/xcache_var.phpt]
18=====================================================================

该问题是因为另外两个函数shell_exec、proc_get_status也被禁用了 ,于是把这两个函数的禁用也取消了。再重新make test的时候发现还是有一项测试未通过:FAIL xcache_set/get test [tests/xcache_var.phpt],无奈之下,换成老版本的XCache 1.3.2进行测试。测试通过,接着make install安成安装。

完成后,提示生成了xcache.so在/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/目录下。接着在php.ini中加入下面的内容(具体可以参看xcache安装包中的xcache.ini文件并进行修改):

 1[xcache-common]
 2zend_extension      = /usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/xcache.so
 3[xcache.admin]
 4; Change xcache.admin.user to your preferred login name
 5xcache.admin.user   = "admin"
 6; Change xcache.admin.pass to the MD5 fingerprint of your password
 7; Use md5 -s "your_secret_password" to find the fingerprint
 8xcache.admin.pass   = "8a488a9ed9dc3634f70b421b06d835e5"
 9[xcache]
10; Change xcache.size to tune the size of the opcode cache
11xcache.size         = 64M
12xcache.shm_scheme   = "mmap"
13xcache.count        = 2
14xcache.slots        = 8K
15xcache.ttl          = 0
16xcache.gc_interval  = 0
17; Change xcache.var_size to adjust the size of variable cache
18xcache.var_size     = 8M
19xcache.var_count    = 4
20xcache.var_slots    = 8K
21xcache.var_ttl      = 0
22xcache.var_maxttl   = 0
23xcache.var_gc_interval =     300
24xcache.test         = Off
25xcache.readonly_protection = On
26xcache.mmap_path    = "/tmp/xcache"
27xcache.coredump_directory =   ""
28xcache.cacher       = On
29xcache.stat         = On
30xcache.optimizer    = Off
31xcache.coverager    = Off

接着重新加载nginx,配置生效。发现速度确实快了很多。上面一些主要参数的介绍如下:

1>  xcache.admin.enable_auth XCache后台是否需要验证(这个后台在刚才下载的XCache的admin目录中,可以自由处理)
2>  xcache.admin.user XCache后台登陆用户名
3>  xcache.admin.pass XCache后台登陆密码
4>  xcache.size XCache的内存缓存大小,普通网站建议设置为64MB,如果是IDC服务器,可以设置成256MB
5>  xcache.count 设置为CPU的数量(双核算2个,比如我的是两个双核Xeon 3.0,就设置为4)
6>  xcache.var_size
7>  xcache.var_count 跟上两条一样
8>  xcache.mmap_path 共享内存标识名,尽量用xcache,避免跟其它软件冲突
9>  xcache.optimizer 优化器,如果没安装Zend可以开启。

需要进入web管理界面的,还需要把安装目录下的admin目录复制到站点的相应访问目录,并用上面设置的用户名密码访问即可。

以上内容为本人自己总结,主要参考下面几个站点的页面: