PHP's opcode cache APC and its installation

Source: Internet
Author: User
Tags anonymous apc config garbage collection ini php file php and posix

First talk about the implementation of the PHP program process, which explains this, just to start our optimization trip.

Client (for example, browser)

-"Request Get hello.php

--The CGI server (for example, Apache) receives a request to find a PHP handler (such as mod_php) based on the configuration.

--"Apache loading PHP handler, PHP handler read php.ini initialization PHP interpretation environment

--mod_php Locate hell.php and load it into memory

--"mod_php compiled source code into opcode tree

--"mod_php Executive opcode Tree

--"Generate results to the browser

What are some of the things we can optimize in this process:

1 will mod_php fast-cgi, avoid every time to load this module, this module will be to initialize each time the interpretation of PHP environment

2 cache php file opcode code, so that, avoid every time to compile.

Cache opcode code has a lot of software, APC, and Zend provide cache.

Reprint the configuration and use of an APC:

Alternative PHP cache (APC) is a free and publicly optimized code cache for PHP. It is used to provide a free, open and robust architecture to cache and optimize PHP's intermediate code.

Under Windows, the APC requires a c:/tmp directory, and the directory is writable to the WEB server process.

1. Installation:

Installed in PHP extension form.

2. Configure

Apc.enabled Boolean

Apc.optimization optimization

Options can be changed in the script

APC php.ini configuration Options Detailed


; Alternative PHP cache for caching and optimizing PHP intermediate code

Apc.cache_by_default = On


; Whether to enable buffering for all files by default.

; When set to off and used with a apc.filters instruction that starts with a plus sign, the file is cached only when the filter is matched.



; Whether to enable the APC feature for the CLI version and open this directive only for testing and debugging purposes.

apc.enabled = On

; If APC is enabled, it is the only way to disable it if the APC is statically compiled into PHP.

Apc.file_update_protection = 2


; When you modify a file on a running server, you should perform an atomic operation.

; That is, first write in a temporary file, and then rename the file (MV) to the final name.

; Programs such as text editors and CP, TAR, and so on, do not operate this way, resulting in the possibility of buffering the defective files.

; The default value of 2 indicates that when the file is accessed, it is not buffered if it finds that the modified time distance is less than 2 seconds.

; The unfortunate visitor may get the incomplete content, but the bad effect will not be magnified by the cache.

; If you can make sure that all of the update operations are atomic, you can turn off this feature with 0.

; If your system is slow to update due to a large number of IO operations, you need to increase this value.

Apc.filters =


; A comma-delimited list of POSIX-extended regular expressions.

; If the source file name matches any one of the patterns, the file is not cached.

; Note that the filename used to match is the file name passed to Include/require, not the absolute path.

; If the first character of the regular expression is "+", it means that any file that matches the expression will be cached.

; If the first character is "-", no matches are cached. "-" is the default value and can be omitted.

Apc.ttl = 0


; The number of seconds the cache entry allowed to linger in the buffer. 0 means never times out. The recommended value is 7200~36000.

; A setting of 0 means that the buffer may be filled with old cache entries, resulting in the inability to cache new entries.

Apc.user_ttl = 0


; Similar to Apc.ttl, the recommended value is 7200~36000 for each user.

; A setting of 0 means that the buffer may be filled with old cache entries, resulting in the inability to cache new entries.

Apc.gc_ttl = 3600


; The number of seconds the cache entry can exist in the garbage collection table.

; This value provides a security measure, even if a server process crashes while executing the cached source files.

; And the source file has been modified, and the memory allocated for the old version will not be reclaimed until this TTL value is reached.

; Setting to zero disables this attribute.

Apc.include_once_override = Off


; There is no documented documentation for this directive, see:

; Keep off, or you may cause unexpected results.

Apc.max_file_size = 1M


; Prevents files larger than this size from being cached.

Apc.mmap_file_mask =


; If you use –enable-mmap (default enabled) to compile Mmap support for APC,

; The value here is the Mktemp-style file mask passed to the Mmap module (the recommended value is "/TMP/APC.") XXXXXX ").

; The mask is used to determine whether the memory-mapped area is to be file-backed or shared memory backed.

; For a direct file-backed memory mapping, set to "/TMP/APC." XXXXXX "appearance (exactly 6 x).

; To use POSIX-style shm_open/mmap, you need to set it to "/apc.shm.xxxxxx."

; You can also set the "/dev/zero" to use the kernel's "/dev/zero" interface for anonymous mapped memory.

; Not defining this directive means enforcing the use of anonymous mappings.

Apc.num_files_hint = 1000


; The approximate number of different source files that may be included or requested on the Web server (the recommended value is 1024~4096).

; If you are unsure, set to 0; This setting is primarily used for sites that have thousands of source files.

apc.optimization = 0

; Optimization level (recommended value is 0).

; A positive integer value indicates that the optimizer is enabled, and the higher the value, the more aggressive optimization is used.

; Higher values may have very limited speed increases, but are still being tested.

Apc.report_autofilter = Off


; Whether to log all scripts that are automatically not cached due to early/late binding reasons.

Apc.shm_segments = 1


; The number of shared memory blocks allocated for the compiler buffer (the recommended value is 1).

; If the APC runs out of shared memory and the Apc.shm_size directive is set to the maximum allowed by the system,

; You can try to increase this value.

Apc.shm_size = 30


; The size of each shared memory block (in MB, the recommended value is 128~256).

; Some systems, including most BSD variants, have a very small default shared memory block size.

Apc.slam_defense = 0

; SYS (against using the directive, it is recommended to use the Apc.write_lock Directive)

; On a very busy server, whether you start a service or modify a file,

; can result in competitive conditions because multiple processes attempt to cache a single file at the same time.

; This directive is used to set the percentage of cache steps that the process skips when processing files that are not cached.

; For example, set to 75 means that when you encounter a file that is not cached, 75% of the probability is not cached, thereby reducing the collision probability.

; Encourage set to 0来 disable this feature.

Apc.stat = On


; Whether to enable script update checking.

; Change this instruction value to be very careful.

; The default value on means that the APC checks whether the script is updated every time the script is requested.

; If updated, the compiled content is automatically recompiled and cached. But doing so has a detrimental effect on performance.

; If set to OFF, the performance is significantly improved by not checking.

; But for the updated content to take effect, you must restart the Web server.

; This instruction is also valid for Include/require files. But it should be noted that

; If you are using a relative path, the APC must check every time it include/require to locate the file.

; Using an absolute path allows you to skip the check, so you are encouraged to use an absolute path for include/require operations.

Apc.user_entries_hint = 100


; Similar to the Num_files_hint directive, just for each individual user.

; If you are unsure, set to 0.

Apc.write_lock = On


; Whether write locks are enabled.

; On a very busy server, whether you start a service or modify a file,

; can result in competitive conditions because multiple processes attempt to cache a single file at the same time.

; Enabling this directive avoids the appearance of competitive conditions.

apc.rfc1867 = Off


; After you open the directive, for each upload file that contains the Apc_upload_progress field just before the file field,

; APC will automatically create a Upload_ user cache entry (that is, the Apc_upload_progress field value).

3. Function:

Apc_cache_info-retrieves cached information (and meta-data) from APC ' s data store

Apc_clear_cache-clears the APC cache

Apc_define_constants-defines a set of constants for later retrieval and mass-definition

Apc_delete-removes a stored variable from the cache

Apc_fetch-fetch a stored variable from the cache

Apc_load_constants-loads a set of constants from the cache

Apc_sma_info-retrieves APC Shared Memory allocation information

Apc_store-cache a variable in the data store

APC is simpler to use, with only a few functions listed below.

Apc_cache_info () returns cached information

Apc_clear_cache () Clears the APC cache contents.

The default (no parameters), only clear the system cache, to clear the user cache, with the ' user ' parameter.

Apc_define_constants (string key, array constants [, BOOL Case_sensitive]) adds the array constants to the cache as constants.

Apc_load_constants (String Key).

Remove the constant cache.

Apc_store (string key, mixed var [, int ttl]).

Save the data in the cache.

Apc_fetch (string key).

Get cached content Saved by Apc_store

Apc_delete (string key).

Deletes the content saved by the Apc_store.

Management of APC:

To download the APC source pack has a apc.php,copy where your Web server can be accessed and browsed.

The functions of the management interface are:

1. Refresh Data

2. View Host Stats

3. System Cache Entries

4. User Cache Entries

5. Version Check

When configuring PHP.ini, be sure to follow the compilation options to place these extensions.

The default is to be placed in the same directory as php.ini, rather than in the legendary extension directory.


Install APC for PHP acceleration

Alternative PHP cache (APC) is a free and publicly optimized code cache for PHP. It is used to provide a free, open and robust architecture to cache and optimize PHP's intermediate code.

The main is to speed up PHP.

WordPress is a large memory footprint, and low execution efficiency.

After I installed the APC, the overall memory of the VPS dropped by around 30M.

The following is the installation method:


Tar zxvf apc-3.1.2.tgz

CD APC-3.1.2/




Make install


Find Extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/"

Add extension = "" below

Configure PHP.ini End Join

apc.enabled = 1

Apc.cache_by_default = On

Apc.shm_segments = 1

Apc.shm_size = 32

Apc.ttl = 600

Apc.user_ttl = 600

Apc.num_files_hint = 0

Apc.write_lock = On

And then reboot the server, OK.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.