Home >Backend Development >PHP Tutorial >Detailed explanation of the configuration and usage steps of PHP accelerator eAccelerator
This time I will bring you a detailed explanation of the configuration and use steps of the PHP accelerator eAccelerator. What are the precautions for the configuration and use of the PHP accelerator eAccelerator? The following is a practical case, let's take a look.
In order to better improve the performance of the system, consider optimizing PHP. I have been exposed to MMCache and eAccelerator in the past two years, and I especially like eAccelerator very much. I also chose it for this optimization.
Share some eAccelerator configuration and optimization documents.
1. Introduction to eAccelerator
1. Background
eAccelerator is a free and open source PHP It is a project of acceleration, optimization, compilation and dynamic caching, which can improve the performance of PHP scripts by caching the results of PHP code compilation, completely solving the PHP script compilation problem that has always been complex and far away from us. By using eAccelerator, you can optimize the execution speed of your PHP code, reduce the server load, and increase the execution speed of PHP applications up to 10 times.
The eAccelerator project was born in 2004, when it was proposed and developed as a branch of the Turck MMCache project. Turck MMCache was developed by Dmitry Stogov. It is a very excellent PHP memory cache acceleration system. Today, a large part of the eAccelerator code is still applied to this project. Currently, the project has not been updated for a long time. For the latest PHP5.x Support is not yet available.
2. Principle
eAccelerator caches the compiled PHP code into shared memory and calls it directly when the user accesses it to achieve efficient acceleration. effect. It is very efficient. From creating shared memory to searching for compiled code, it is completed in a very short time. For files and codes that cannot be cached in shared memory, eAccelerator can also cache them on the system disk.
eAccelerator also supports the compilation and interpretation of PHP code. You can compile the PHP code through the encoder.php script to protect the code. The compiled code must be run in an environment where eAccelerator is installed. Down. The code compiled by eAccelerator cannot be decompiled, unlike some other compilation tools that can be decompiled, which will make the code safe and efficient.
2. eAccelerator installation and configuration
1. Support platform
Because aAccelerator provides a large Part of the API is based on shared memory, so it will be better supported on the *nix platform. Although a binary version based on the windows platform has also been released, I will only provide configuration and instructions based on the *nix platform here. Currently, it can Supported platforms include Linux, FreeBSD, OpenBSD, Mac OS X, Solaris, AIX en HP-UX.
2. System requirements
php4 or php5
3, install
First go to eAccelerator official website to download the latest version of the source code package: eaccelerator-0.9.5-beta.tar.bz2#tar -zxvf ./eaccelerator-0.9.5-beta2.tar.bz2 #cd eaccelerator-0.9.5-beta2 #export PHP_PREFIX="/usr/local" (把PHP安装目录导入到环境变量,FreeBSD默认是/usr/local) #$PHP_PREFIX/bin/phpize #./configure --enable-eaccelerator=shared --with-php-config=$PHP_PREFIX/bin/php-config #make #make instal
4. Ini file configuration
After the installation is complete, let’s start configuring the php.ini file. eAccelerator provides two configuration and calling methods, as follows. Installation as Zend extension mode:zend_extension="/usr/local/lib/php/20050922/eaccelerator.so" eaccelerator.shm_size="16" eaccelerator.cache_dir="/tmp/eaccelerator" eaccelerator.enable="1" eaccelerator.optimizer="1" eaccelerator.check_mtime="1" eaccelerator.debug="0" eaccelerator.log_file = "/var/log/httpd/eaccelerator_log" eaccelerator.filter="" eaccelerator.shm_max="0" eaccelerator.shm_ttl="0" eaccelerator.shm_prune_period="0" eaccelerator.shm_only="0" eaccelerator.compress="1" eaccelerator.compress_level="9"If you use PHP installed in thread safe mode, you must use "zend_extension_ts" to replace "zend_extension" in the first line.Installation as PHP extension mode: (This is the method used by most)
extension="eaccelerator.so" eaccelerator.shm_size="16" eaccelerator.cache_dir="/tmp/eaccelerator" eaccelerator.enable="1" eaccelerator.optimizer="1" eaccelerator.check_mtime="1" eaccelerator.debug="0" eaccelerator.log_file = "/var/log/httpd/eaccelerator_log" eaccelerator.filter="" eaccelerator.shm_max="0" eaccelerator.shm_ttl="0" eaccelerator.shm_prune_period="0" eaccelerator.shm_only="0" eaccelerator.compress="1" eaccelerator.compress_level="9"For detailed configuration instructions of the php.ini file, please refer to the README document in the source code directory or visit the official document: ini setting
After completing the installation configuration, we finally need to create the cache directory
#mkdir /tmp/eaccelerator #chmod 777 /tmp/eaccelerator
5. Verify the installation results
Access your phpinfo() through the browser Page or run php -i to get the PHP configuration information. If you see information similar to the following, the installation is successful.This program makes use of the Zend Scripting Language Engine: Zend Engine v2.1.0, Copyright (c) 1998-2006 Zend Technologies with eAccelerator v0.9.5-beta2, Copyright (c) 2004-2006 eAccelerator, by eAcceleratorZend Optimizer3.0.1 is also installed on my machine, so the information I see is as follows:
This program makes use of the Zend Scripting Language Engine: Zend Engine v2.1.0, Copyright (c) 1998-2006 Zend Technologies with eAccelerator v0.9.5-beta2, Copyright (c) 2004-2006 eAccelerator, by eAccelerator with Zend Extension Manager v1.0.10, Copyright (c) 2003-2006, by Zend Technologies with Zend Optimizer v3.0.1, Copyright (c) 1998-2006, by Zend TechnologiesIf you turn on the debug option of eAccelerator, you can see something like this from the log Information below
#tail /var/log/httpd/eAccelerator_log EACCELERATOR hit: "/var/www/toplee.com/blog/index.php" EACCELERATOR hit: "/var/www/toplee.com/blog/wp-blog-header.php" EACCELERATOR hit: "/var/www/toplee.com/blog/wp-config.php" EACCELERATOR hit: "/var/www/toplee.com/blog/wp-settings.php" EACCELERATOR hit: "/var/www/toplee.com/blog/wp-content/plugins/wp-cache/wp-cache-phase1.php"
以上信息表示文件都得到了缓存和命中。
至此,完成了全部的安装和配置,好好享受eAccelerator带给你的惊喜吧,根据Michael的测试,效果的确相当的好。
三、在PHP中可以使用eAccelerator的API开发
1、API和文档说明:
eAccelerator提供了便捷便捷而又稳定的本机缓存实现方式,由于大部分代码实现基于共享内存,所以只能在*nix平台中使用,Windows平台Michael就暂时不知道何时有这方面的支持了。
eAccelerator提供如下的API接口和文件:(下述文件均在源码包的doc/php/目录下)
文件列表:
cache.php dasm.php encoder.php info.php loader.php session.php shared_memory.php
接口列表
array eaccelerator_cached_scripts () void eaccelerator_cache_output (string $key, string $eval_code, [int $ttl = 0]) void eaccelerator_cache_page (string $key, [int $ttl = 0]) void eaccelerator_cache_result (string $key, string $code, [int $ttl = 0]) void eaccelerator_caching (boolean $flag) void eaccelerator_clean () void eaccelerator_clear () array eaccelerator_dasm_file (mixed $filename) mixed eaccelerator_encode (mixed $src, [mixed $prefix = ''], [string $pre_content = ''], [string $post_content = '']) void eaccelerator_gc () mixed eaccelerator_get (string $key) array eaccelerator_info () array eaccelerator_list_keys () void eaccelerator_load () boolean eaccelerator_lock (string $key) void eaccelerator_optimizer (boolean $flag) void eaccelerator_purge () boolean eaccelerator_put (string $key, mixed $value, [int $ttl = 0]) array eaccelerator_removed_scripts () boolean eaccelerator_rm (string $key) void eaccelerator_rm_page (string $key) boolean eaccelerator_set_session_handlers () boolean eaccelerator_unlock (string $key)
有关上述文档详细说明请参考官方文档:API Documents(http://bart.eaccelerator.net/doc/phpdoc/)
下面有部分网友翻译后的接口说明:
eaccelerator_put($key, $value, $ttl=0)
将 $value 以 $key 为键名存进缓存(php4下支持对像类型,看源码好像zend2里不支持了),$ttl 是这个缓存的生命周期,单位是秒,省略该参数或指定为 0 表示不限时,直到服务器重启清空为止。
eaccelerator_get($key) 根据 $key 从缓存中返回相应的 eaccelerator_put() 存进去的数据,如果这项缓存已经过期或不存在那么返回值是 NULL eaccelerator_rm($key) 根据 $key 移除缓存 eaccelerator_gc() 移除清理所有已过期的 key eaccelerator_lock($key) 为 $key 加上锁定操作,以保证多进程多线程操作时数据的同步。需要调用 eaccelerator_unlock($key) 来释放这个锁或等待程序请求结束时自动释放这个锁。
<?php eaccelerator_lock("count"); eaccelerator_put("count",eaccelerator_get("count")+1)); ?> eaccelerator_unlock($key)
根据 $key 释放锁
eaccelerator_cache_output($key, $eval_code, $ttl=0)
将 $eval_code 代码的输出缓存 $ttl 秒,($ttl参数同 eacclerator_put)
例如:
<?php eaccelerator_cache_output('test', 'echo time(); phpinfo();', 30); ?> eaccelerator_cache_result($key, $eval_code, $ttl=0)
将 $eval_code 代码的执行结果缓存 $ttl 秒,($ttl参数同 eacclerator_put),类似 cache_output
例如:
<?php eaccelerator_cache_result('test', ' time() . "Hello";', 30); ?> eaccelerator_cache_page($key, $ttl=0)
将当前整页缓存 $ttl 秒。
例如:
<?php eaccelerator_cache_page($_SERVER['PHP_SELF'].'?GET='.serialize($_GET),30); echo time(); phpinfo(); ?> eaccelerator_rm_page($key)
删除由 eaccelerator_cache_page() 执行的缓存,参数也是 $key
2、PHP代码中使用eAccelerator加速
测试下eAccelerator强大的威力:(该代码在 cli 模式下可能无效)
abe162531fa68c29917dbd7eb948bd26\n"; } function func() { echo ', the world!'; } function now($t) { echo date('Y-m-d H:i:s', $t); } } $tt = eaccelerator_get("test_tt"); if (!$tt) { $tt = new test_cache; eaccelerator_put("test_tt", $tt); echo "no cached!0c6dc11e160d3b678d68754cc175188a\n"; } else { echo "cached0c6dc11e160d3b678d68754cc175188a\n"; } echo $tt->pro; $tt->func(); $tt->now(time() + 86400); ?>
另外,据说在著名的vBulletin 3.60Beta版里面已经集成了对eAccelerator的支持。
一段来自vBulletin里面的代码
a1b4def77a7d2348d09dbd178988d01ddefaultitems AS $item) { $this->do_fetch($item); } if (is_array($itemarray)) { foreach ($itemarray AS $item) { $this->do_fetch($item); } } $this->check_options(); // set the version number variable $this->registry->versionnumber =& $this->registry->options['templateversion']; } /** * Fetches the data from shared memory and detects errors * * @param string title of the datastore item * * @return void */ function do_fetch($title) { $data = eaccelerator_get($title); if ($data === null) { // appears its not there, lets grab the data, lock the shared memory and put it in $data = ''; $dataitem = $this->dbobject->query_first(" SELECT title, data FROM " . TABLE_PREFIX . "datastore WHERE title = '" . $this->dbobject->escape_string($title) ."' "); if (!empty($dataitem['title'])) { $data =& $dataitem['data']; $this->build($dataitem['title'], $dataitem['data']); } } $this->register($title, $data); } /** * Updates the appropriate cache file * * @param string title of the datastore item * * @return void */ function build($title, $data) { if (!function_exists('eaccelerator_put')) { trigger_error("eAccelerator not installed", E_USER_ERROR); } eaccelerator_lock($title); eaccelerator_put($title, $data); eaccelerator_unlock($title); } } ?>
相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
推荐阅读:
The above is the detailed content of Detailed explanation of the configuration and usage steps of PHP accelerator eAccelerator. For more information, please follow other related articles on the PHP Chinese website!