我的整个安装过程:

一 下载安装软件。官方网站:http://pecl.php.net/package/apc
二 传递该软件到Linux CentOS的/usr/local/目录下面
三 解压该软件
#tar -zxvf APC-3.1.11.tar.gz
#cd APC-3.1.11
四 用phpize命令来准备配置php扩展的编译环境。
#/usr/local/php/bin/phpize
提示: 安装完直接运行会报错

Cannot find config.m4. 
Make sure that you run '/usr/bin/phpize' in the top level source directory of the module

说明你编译的东西没有config.m4.文件。如果在apc目录运行,就不会有错了。因为apc下提供了config.m4
五 如果上述过程没有出现错误,则
#./configure --with-php-config=/usr/local/php/bin/php-config #后面为php安装路径
#make && make install
六 最后,还要在/usr/local/php/lib/php.ini配置文件最后加上extensions="apc.so",引入apc
7 测试php及其扩展的信息。在默认路径下/usr/local/apache2/htdocs/建立文件
#vim  phpinfo.php
<?php
phpinfo();
?>
即可打印出php及其扩展的详细信息。
#########################################################

正如PHP官方手册说明:

Alternative PHP Cache (APC) 是一个开放自由的PHP opcode 缓存。它的目标是提供一个自由、 开放,和健全的框架用于缓存和优化PHP的中间代码。

官方下载列表

http://pecl.php.net/package/apc

csdn下载

http://download.csdn.net/detail/mengxiangbaidu/3763918

说明:

phpize

http://php.net/manual/en/install.pecl.phpize.php

http://cn2.php.net/manual/zh/install.pecl.phpize.php

ubuntu下安装 

apt-get install php5-dev

安装完直接运行会报错

Cannot find config.m4. 
Make sure that you run '/usr/bin/phpize' in the top level source directory of the module

说明你编译的东西没有config.m4.文件。如果在apc目录运行,就不会有错了。因为apc下提供了config.m4

用 phpize 编译共享 PECL 扩展库

有时候不能用 pear 安装命令。这可能是因为在防火墙后面,或者是因为想要安装的扩展库还没有 PEAR 兼容的包,例如 CVS 中尚未发布的扩展库。如果要编译这种扩展库,可以用更底层的编译工具来手工进行编译。

phpize 命令是用来准备 PHP 扩展库的编译环境的。下面例子中,扩展库的源程序位于 extname 目录中:

$ cd extname
$ phpize
$ ./configure
$ make
# make install

成功的安装将创建 extname.so 并放置于 PHP 的扩展库目录中。需要调整 php.ini,加入 extension=extname.so 这一行之后才能使用此扩展库。

/usr/local/php/bin/php-config

 是指你的php-config的位置

你可以php-config看一下所在路径

我的是

  ./configure --with-php-config=/usr/bin/php-config

如果出现如下错误

apc fatal error: pcre.h: No such file or directory 

http://stackoverflow.com/questions/4720666/sudo-pecl-install-apc-returns-error

PCRE is a dependency for installing APC. You can install it pretty quick with

yum install pcre-devel or apt-get install libpcre3-dev

once it's installed, re-run

sudo pecl install apc

按照解压压缩包,就看到了install,打开看看就有从安装要求、编译、安装、到配置参数的详细解释。tests目录中提供了详细的php开发demos

Installation Instructions for APC
---------------------------------
This version of APC should work on PHP 4.3.0 - 4.4.x and
5.1.0 - 5.2.x.  Yes, that means PHP 5.0.x is no longer 
supported.  Upgrade to PHP 5.1.x or 5.2.x and you will 
notice all sorts of performance increases.
----------------------------------------------
  extension=apc.so
  apc.enabled=1
  apc.shm_size=128M
  apc.ttl=7200
  apc.user_ttl=7200
  apc.enable_cli=1
----------------------------------------------
Although the default APC settings are fine for many installations, serious
users should consider tuning the following parameters:


    OPTION                  DESCRIPTION
    ------------------      --------------------------------------------------
    apc.enabled             This can be set to 0 to disable APC. This is
                            primarily useful when APC is statically compiled
                            into PHP, since there is no other way to disable
                            it (when compiled as a DSO, the zend_extension
                            line can just be commented-out).
                            (Default: 1)
                            
    apc.shm_segments        The number of shared memory segments to allocate
                            for the compiler cache. If APC is running out of
                            shared memory but you have already set
                            apc.shm_size as high as your system allows, you
                            can try raising this value.  Setting this to a
                            value other than 1 has no effect in mmap mode
                            since mmap'ed shm segments don't have size limits.
                            (Default: 1)
                            
    apc.shm_size            The size of each shared memory segment in MB.
                            By default, some systems (including most BSD
                            variants) have very low limits on the size of a
                            shared memory segment. M/G suffixes must be used.
                            (Default: 30)


                            
    apc.optimization        This option has been deprecated.
                            (Default: 0)
    
    apc.num_files_hint      A "hint" about the number of distinct source files
                            that will be included or requested on your web
                            server. Set to zero or omit if you're not sure;
                            this setting is mainly useful for sites that have
                            many thousands of source files.
                            (Default: 1000)


    apc.user_entries_hint   Just like num_files_hint, a "hint" about the number
                            of distinct user cache variables to store. 
                            Set to zero or omit if you're not sure;
                            (Default: 4096)


    apc.ttl                 The number of seconds a cache entry is allowed to
                            idle in a slot in case this cache entry slot is 
                            needed by another entry.  Leaving this at zero
                            means that your cache could potentially fill up
                            with stale entries while newer entries won't be
                            cached.  
                            (Default: 0)


    apc.user_ttl            The number of seconds a user cache entry is allowed 
                            to idle in a slot in case this cache entry slot is 
                            needed by another entry.  Leaving this at zero
                            means that your cache could potentially fill up
                            with stale entries while newer entries won't be
                            cached.  
                            (Default: 0)




    apc.gc_ttl              The number of seconds that a cache entry may
                            remain on the garbage-collection list. This value
                            provides a failsafe in the event that a server
                            process dies while executing a cached source file;
                            if that source file is modified, the memory
                            allocated for the old version will not be
                            reclaimed until this TTL reached. Set to zero to
                            disable this feature.
                            (Default: 3600)


    apc.cache_by_default    On by default, but can be set to off and used in
                            conjunction with positive apc.filters so that files
                            are only cached if matched by a positive filter.
                            (Default: On)


    apc.filters             A comma-separated list of POSIX extended regular
                            expressions. If any pattern matches the source
                            filename, the file will not be cached. Note that
                            the filename used for matching is the one passed
                            to include/require, not the absolute path.  If the
                            first character of the expression is a + then the
                            expression will be additive in the sense that any
                            files matched by the expression will be cached, and
                            if the first character is a - then anything matched
                            will not be cached.  The - case is the default, so
                            it can be left off.
                            (Default: "")


    apc.mmap_file_mask      If compiled with MMAP support by using --enable-mmap
                            this is the mktemp-style file_mask to pass to the
                            mmap module for determing whether your mmap'ed memory
                            region is going to be file-backed or shared memory
                            backed.  For straight file-backed mmap, set it to
                            something like /tmp/apc.XXXXXX (exactly 6 X's).
                            To use POSIX-style shm_open/mmap put a ".shm"
                            somewhere in your mask.  eg.  "/apc.shm.XXXXXX"
                            You can also set it to "/dev/zero" to use your 
                            kernel's /dev/zero interface to anonymous mmap'ed 
                            memory.  Leaving it undefined will force an 
                            anonymous mmap.
                            (Default: "")


    apc.slam_defense        ** DEPRECATED - Use apc.write_lock instead **
                            On very busy servers whenever you start the server or
                            modify files you can create a race of many processes
                            all trying to cache the same file at the same time.
                            This option sets the percentage of processes that will
                            skip trying to cache an uncached file.  Or think of it
                            as the probability of a single process to skip caching.
                            For example, setting this to 75 would mean that there is
                            a 75% chance that the process will not cache an uncached
                            file.  So the higher the setting the greater the defense
                            against cache slams.  Setting this to 0 disables this
                            feature.
                            (Default: 0)


    apc.file_update_protection
                            When you modify a file on a live web server you really
                            should do so in an atomic manner.  That is, write to a
                            temporary file and rename (mv) the file into its permanent
                            position when it is ready.  Many text editors, cp, tar and
                            other such programs don't do this.  This means that there
                            is a chance that a file is accessed (and cached) while it
                            is still being written to.  This file_update_protection
                            setting puts a delay on caching brand new files.  The
                            default is 2 seconds which means that if the modification
                            timestamp (mtime) on a file shows that it is less than 2
                            seconds old when it is accessed, it will not be cached.  
                            The unfortunate person who accessed this half-written file
                            will still see weirdness, but at least it won't persist.
                            If you are certain you always atomically update your files
                            by using something like rsync which does this correctly, you
                            can turn this protection off by setting it to 0.  If you
                            have a system that is flooded with io causing some update
                            procedure to take longer than 2 seconds, you may want to
                            increase this a bit.
                            (Default: 2)


    apc.enable_cli          Mostly for testing and debugging.  Setting this enables APC
                            for the CLI version of PHP.  Normally you wouldn't want to
                            create, populate and tear down the APC cache on every CLI
                            request, but for various test scenarios it is handy to be
                            able to enable APC for the CLI version of APC easily.
                            (Default: 0)


    apc.max_file_size       Prevents large files from being cached.  
                            (Default: 1M)


    apc.stat                Whether to stat the main script file and the fullpath
                            includes.  If you turn this off you will need to restart
                            your server in order to update scripts.
                            (Default: 1)


    apc.canonicalize        Whether to canonicalize paths in stat=0 mode or
                            fall back to stat behaviour if set to 0
                            (Default: 0)


    apc.write_lock          On busy servers when you first start up the server, or when
                            many files are modified, you can end up with all your processes
                            trying to compile and cache the same files.  With write_lock 
                            enabled, only one process at a time will try to compile an
                            uncached script while the other processes will run uncached
                            instead of sitting around waiting on a lock.
                            (Default: 1)


    apc.report_autofilter   Logs any scripts that were automatically excluded from being
                            cached due to early/late binding issues.
                            (Default: 0)


    apc.rfc1867             RFC1867 File Upload Progress hook handler is only available
                            if you compiled APC against PHP 5.2.0 or later.  When enabled
                            any file uploads which includes a field called 
                            APC_UPLOAD_PROGRESS before the file field in an upload form
                            will cause APC to automatically create an upload_<key>
                            user cache entry where <key> is the value of the 
                            APC_UPLOAD_PROGRESS form entry.


                            Note that the file upload tracking is not threadsafe at this
                            point, so new uploads that happen while a previous one is 
                            still going will disable the tracking for the previous.
                            (Default: 0)


    apc.rfc1867_prefix      Key prefix to use for the user cache entry generated by
                            rfc1867 upload progress functionality.  
                            (Default: "upload_")


    apc.rfc1867_name        Specify the hidden form entry name that activates APC upload
                            progress and specifies the user cache key suffix.
                            (Default: "APC_UPLOAD_PROGRESS")


    apc.rfc1867_freq        The frequency that updates should be made to the user cache
                            entry for upload progress.  This can take the form of a 
                            percentage of the total file size or a size in bytes 
                            optionally suffixed with 'k', 'm', or 'g' for kilobytes, 
                            megabytes, or gigabytes respectively (case insensitive).  
                            A setting of 0 updates as often as possible, which may cause 
                            slower uploads.
                            (Default: 0)


    apc.localcache         ** REMOVED 
    apc.localcache.size    ** REMOVED
    
    apc.include_once_override 
                            Optimize include_once and require_once calls and avoid the 
                            expensive system calls used.
                            (Default: 0)


    apc.serializer 
                            Defines which serializer should be used. Default is the 
                            standard PHP serializer. Other can be used without having
                            to re compile apc, like igbinary for example.
                            (apc.serializer=igbinary)




运行phpinfo看到如下信息

表示安装成功

apc

APC Support enabled
Version 3.1.9
APC Debugging Disabled
MMAP Support Enabled
MMAP File Mask no value
Locking type pthread mutex Locks
Serialization Support php
Revision $Revision: 308812 $
Build Date Nov 7 2011 10:14:57

Directive Local Value Master Value
apc.cache_by_default On On
apc.canonicalize On On
apc.coredump_unmap Off Off
apc.enable_cli Off Off
apc.enabled On On
apc.file_md5 Off Off
apc.file_update_protection 2 2
apc.filters no value no value
apc.gc_ttl 3600 3600
apc.include_once_override Off Off
apc.lazy_classes Off Off
apc.lazy_functions Off Off
apc.max_file_size 1M 1M
apc.mmap_file_mask no value no value
apc.num_files_hint 1000 1000
apc.preload_path no value no value
apc.report_autofilter Off Off
apc.rfc1867 Off Off
apc.rfc1867_freq 0 0
apc.rfc1867_name APC_UPLOAD_PROGRESS APC_UPLOAD_PROGRESS
apc.rfc1867_prefix upload_ upload_
apc.rfc1867_ttl 3600 3600
apc.serializer default default
apc.shm_segments 1 1
apc.shm_size 32M 32M
apc.slam_defense On On
apc.stat On On
apc.stat_ctime Off Off
apc.ttl 0 0
apc.use_request_time On On
apc.user_entries_hint 4096 4096
apc.user_ttl 0 0
apc.write_lock On On
详细的配置你可以在php.ini中加入

加载.so成功,基本就可以使用了

#############################
启用APC的时候,用apache ab测某个页面。然后关闭APC后再测一下就可以看到差距了。
我在linux下的参数就是 ab -n 10000 http://localhost/index.php 。windows我不知道,你搜一下用法介绍很多。或者你自己靠刷新浏览器,输出一下程序开始和结束时间差,内存占用memory_get_usage()等。
我把我的数据说一下吧
  php-5.2.17 fastcgi + php-fpm 使用CodeIgniter框架写的一个小页面
  开启APC 内存占用 0.13MB 耗时约 0.0014 秒
  关闭APC 内存占用 0.46MB 耗时月 0.0055 秒
#######################################
PHP APC简介   
PHP官方简介
Alternative PHP Cache (APC)是一个开放自由的PHP opcode缓存。它的目标是提供一个自由、开放和健全的框架用于缓存和优化PHP中间代码。

APC有两个主要的配置选项:
第一,多少内存将被分配给APC,ini选项apc.shm_size (integer)控制这个设置,默认值是30M。
第二,每次请求APC是否检查文件修改,ini选项apc.stat控制这个设置,默认值是1,表示每次请求脚本时都检查脚本是否被更新,如果更新则自动重新编译和缓存编译后的内容,但这样做对性能有不利影响。如果设为0则表示不进行检查,从而使性能的到大幅提高。

使用apc.php可以监控apc工作状态。如果apc运行了,缓存完全统计数目将显示缓存的命中率并且清除在最后apc.ttl秒内没有被访问的缓存。这个数字使缓存的最小化的很好配置。如果缓存不断的被填充和清除,这将影响缓存的效果和脚本的性能。减少这个数字的最好方式就是给APC分配足够多的内存。除此之外,可以通过apc.filters缓存更少的脚本。

APC的常用函数
bool   apc_add  (  string $key  ,  mixed $var  [,  int $ttl = 0  ] ) —— 缓存一个变量到数据存储(只在这个变量之前没有被存储的情况)
bool   apc_store  (  string $key  ,  mixed $var  [,  int $ttl = 0  ] ) —— 缓存一个变量到数据存储
bool   apc_clear_cache  ([  string $cache_type  ] ) —— 清除APC缓存
mixed   apc_fetch  (  mixed $key  [,  bool &$success  ] ) ——  从缓存中取出存储的变量
mixed   apc_exists  (  mixed $keys  ) ——  检查是否有一个或者多个APC键名存在
mixed   apc_delete  (  string $key  ) —— 从缓存中删除一个已存储的变量
mixed   apc_delete_file  (  mixed $keys  ) —— 从opcode缓存中删除给定文件的缓存
mixed   apc_compile_file  (  string $filename  [,  bool $atomic = true  ] ) —— 绕过filters的限制,缓存文件
bool   apc_define_constants  (  string $key  ,  array $constants  [,  bool $case_sensitive = true  ] ) —— 定义一组常量
bool   apc_load_constants  (  string $key  [,  bool $case_sensitive = true  ] ) —— 读取一组常量
array   apc_cache_info  ([  string $cache_type  [,  bool $limited = false  ]] ) —— 获取缓存信息

要注意的是 apc_clear_cache ,这个函数不会清除变量的缓存,用apc_delete(变量名),才把变量从缓存中清除掉。apc_clear_cache只会把缓存的opcode文件清除掉。

APC默认通过mmap匿名映射创建共享内存,缓存对象都存放在这块“大型”的内存空间。由APC自行管理该共享内存。
PHP预定义变量,可以使用apc_define_constants()函数。
函数apc_store(),对于系统设置等PHP变量,生命周期是整个应用(从httpd守护进程直到httpd守护进行关闭),使用apc比memcached会更好。毕竟不要经过网络传输协议tcp。
APC不适用于通过函数apc_store()缓存频繁变更的用户数据,会出现一些奇异现象。
APC本身不支持分布式
安装方式:
wget http://pecl.php.net/get/APC-3.1.2.tgz
 tar zxvf APC-3.1.2.tgz
 cd APC-3.1.2/
 /usr/local/php/bin/phpize
 ./configure --enable-apc --with-php-config=/usr/bin/php-config
 make
 make install

make的时候出现了一点意外

/usr/include/php5/ext/pcre/php_pcre.h:29: fatal error: pcre.h: 没有那个文件或目录
sudo apt-get install libpcre3-dev 安装了这个包就解决了
###################################################
关于其应用:
<?php
$fruit  
'apple' ;
$veggie  'carrot' ;

apc_store ( 'foo' $fruit );
apc_store ( 'bar' $veggie );

if (
apc_exists ( 'foo' )) {
    echo 
"Foo exists: " ;
    echo 
apc_fetch ( 'foo' );
} else {
    echo 
"Foo does not exist" ;
}

echo 
PHP_EOL ;
if (
apc_exists ( 'baz' )) {
    echo 
"Baz exists." ;
} else {
    echo 
"Baz does not exist" ;
}

echo 
PHP_EOL ;

$ret  apc_exists (array( 'foo' 'donotexist' 'bar' ));
var_dump ( $ret );

?>
Foo exists: apple Baz does not exist array(2) { ["foo"]=> bool(true) ["bar"]=> bool(true) }
<?php
$fruit  'apple';
$veggie 'carrot';

apc_store('foo'$fruit);
apc_store('bar'$veggie);

if (
apc_exists('foo')) {
    echo 
"Foo exists: ";
    echo 
apc_fetch('foo');
} else {
    echo 
"Foo does not exist";
}

echo 
PHP_EOL;
if (
apc_exists('baz')) {
    echo 
"Baz exists.";
} else {
    echo 
"Baz does not exist";
}

echo 
PHP_EOL;

$ret apc_exists(array('foo''donotexist''bar'));
var_dump($ret);

?>
42 43 53 63 bool(true) Now, let's fail bool(false) bool(false)


关于其在PHP中的扩展应用:http://www.php.net/manual/en/function.apc-delete.php




Logo

更多推荐