APC Cache opcode (RPM)

Source: Internet
Author: User
Tags apc apc configuration apc homepage pear

1. PHP execution

The running phase of PHP is also divided into three stages:

      • Parse. Syntax Analysis phase.
      • Compile. Compile output opcode middle code.
      • Execute. Run, and the output is run dynamically.

Since PHP is an interpreted language, it is time to read the program and compile it into opcode by the Zend engine. Finally, the Zend virtual machine executes these opcode (directives) in sequence to complete the operation. So we can cache this opcode, and next time we can avoid recompiling.

The APC cache function is as follows:

2, APC Introduction

Alternative PHP cache (APC) is an open and free php opcode cache. Its goal is to provide a free, open, and robust framework for caching and optimizing PHP's intermediate code.

PHP APC provides two kinds of caching functions, namely cache opcode (target file), which we call Apc_compiler_cache. It also provides some interfaces for PHP developers.

The APC homepage is: PHP APC. The current version is 3.1.13, installed via pecl, requires php4.3.0 or later

3. Installation Configuration

Generally download the source code and then phpize to compile the installation, after installation in addition to php.ini add
Extension=apc.so
This is the line. The steps are as follows:

Download: http://pecl.php.net/get/APC-3.1.5.tgz (HTTP://PECL.PHP.NET/PACKAGE/APC)

Note: The latest stable version is apc-3.1.9.tgz, but it is problematic to compile the APC in either php5.4 or version 5.5.

Apc-3.1.9.tgz Compile Error:

  1. /tmp/pear/temp/apc/apc_zend.c:in funktion»apc_get_zval_ptr«:
  2. /tmp/pear/temp/apc/apc_zend.c:62:fehler:»zend_execute_data«hat kein Element namens»ts«
  3. /tmp/pear/temp/apc/apc_zend.c:64:fehler:»zend_execute_data«hat kein Element namens»ts«
  4. /tmp/pear/temp/apc/apc_zend.c:67:fehler:»zend_execute_data«hat kein Element namens»cvs«
  5. /tmp/pear/temp/apc/apc_zend.c:in funktion»apc_op_zend_include_or_eval«:
  6. /tmp/pear/temp/apc/apc_zend.c:170:fehler:»zend_execute_data«hat kein Element namens»ts«
  7. /tmp/pear/temp/apc/apc_zend.c:170:fehler:»zend_execute_data«hat kein Element namens»ts«
  8. /tmp/pear/temp/apc/apc_zend.c:170:fehler:»zend_execute_data«hat kein Element namens»ts«
  9. /tmp/pear/temp/apc/apc_zend.c:171:fehler:»zend_execute_data«hat kein Element namens»ts«

php5.4 or php5.5 should use XCache or Zend opcache instead of APC.

http://www.litespeedtech.com/support/forum/threads/solved-php-5-5-6-install-fails-on-apc.7809/

wget http://centminmod.com/centminmodparts/apc/php550/APC-3.1.13.tgz

# TAR-XZVF  apc-3.1.5.tgz

#cd  APC-3.1.5

#/usr/bin/phpize

#./configure--ENABLE-APC--enable-mmap--enable-apc-spinlocks--disable-apc-pthreadmutex

#make

#make Install

Note: We support mmap here, while using spinlocks spin lock. Spinlocks is recommended by Facebook and is also a locking mechanism recommended by APC developers. Maximum performance with spinlocks (spin) lock mechanism

Ini

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

Add extension = "apc.so" below

Configuring APC for PHP.ini

apc.rfc1867 = On

; max upload File

Apc.max_file_size = 800M

apc.enable_cli = off ; Whether APC functionality is enabled for the CLI version, which is only opened for testing and debugging purposes.

apc.enabled = 1 ; allow apc,apc.enabled The default value is 1, you can set 0 to disable APC. If you set it to 0, also comment out the extension=apc.so (which saves memory resources). Once the APC feature is enabled, opcodes to shared memory is cached.

apc.shm_segments = 1 ; Number of shared memory blocks to be allocated to the compiler cache

apc.shm_size = Apc.shm_size is the size of the cache size, in megabytes, of each shared memory block that was opened to APC. By default, some systems (including most BSD variants) have very low block sizes of shared memory.

Apc.shm_segments = 1
Apc.shm_size = 30
Since APC caches data in memory, it is necessary for us to limit it to memory resources. These two configurations allow you to limit the amount of memory space an APC can use. APC.SHM_SEGMENTS Specifies the number of shared memory blocks, while apc.shm_size specifies a chunk of shared memory space, in M. Therefore, the amount of memory allowed to be used by APC should be apc.shm_segments * apc.shm_size = 30M. You can adjust the size space of a piece of shared memory. Of course, the maximum value of a piece of shared memory is limited by the operating system, i.e. it cannot exceed the/proc/sys/kernel/shmmax size. Otherwise, the APC will fail when it creates the shared memory. When the apc.shm_size reaches the upper limit, you can set the apc.shm_segments to allow APC to use more memory space. We recommend that if you call APC to use memory space, the first filter apc.shm_size, after the filter apc.shm_segments. The specific value can be planned and adjusted according to the apc.php monitoring situation. It is worth noting that each adjustment requires restarting the httpd daemon so that the Apc.so module can be reloaded. Following the httpd daemon boot, the apc.so module will be loaded. apc.so load initialization, the memory specified by the MMAP request is allocated, that is, apc.shm_size * apc.shm_segments. Moreover, the use of anonymous memory mapping, by mapping a special device/dev/zero, provides a "large", filled with 0 of the memory for APC management.
To verify the above statement, we commented out the Apc.ini configuration and wrote the following PHP script to observe the allocated memory space of the Apc.so module initialization.

apc.optimization = 0 ; optimization level. Set to 0 disables the optimizer, and higher values use more aggressive optimizations.

apc.num_files_hint = +; Approximate estimate of the number of different source files that are included or requested on the WEB server

apc.ttl = 0 ; Number of seconds of idle time allowed by cache entry in buffer

Apc.gc_ttl = 3600 ; The number of seconds the cache entry can exist in the garbage collection table

apc.cache_by_default = on; default is on, but can be set to off and used with apc.filters at the beginning of the plus sign

;/* The file is cached only when matching the filter. */

Apc.slam_defense = 0

Apc.file_update_protection = 2

APC.ENABLE_CLI = 0

apc.stat=0; debug

Whether to enable the script update check. Be very careful to change this command value. The default value on indicates that APC checks that the script is updated every time the script is requested, and automatically recompile and cache the compiled content if it is updated. But doing so has a detrimental effect on performance. If set to Off, no check is made, resulting in a significant performance gain. However, in order for the updated content to take effect, you must restart the Web server (translator Note: If you use cgi/fcgi similar, you need to restart the cgi/fcgi process). The script files on the production server are rarely changed and can achieve significant performance gains by disabling this option.

This command is also valid for Include/require files. However, it should be noted that if you are using a relative path, APC must check each time Include/require to locate the file. You can skip checking using absolute paths, so encourage you to use absolute paths for include/require operations

Detailed configuration:

http://www.php.net/manual/zh/apc.configuration.php

          Restart the Apache server to see if there are any APC configuration items in the Phpinfo, and some of them are configured successfully.
4. APC Management interface

To pecl.php.net download the APC source package there is a apc.php,copy where your Web server can access it and browse to it.

The management interface features are:

1. Refresh Data

2. View Host Stats

3. System cache Entries (cache opcode code)

4. User cache Entries (custom cached data)

5. Version Check

5. Use of APC

The use of APC is not a real thing. APC is an optimizer that silently serves your PHP application in the background from the date of installation. All your PHP code will be cached.

In addition, APC provides a certain amount of memory caching. However, this feature is not perfect, and there are reports of unpredictable errors that can occur if the use of the APC cache write function is frequent. If you want to use this feature, look at the APC_FETCH,APC_ Store and several other functions related to APC cache.
Starting with PHP5.2, APC introduced a cookie that solves the problem of a long-overdue progress bar for uploading large files.

6. Advanced use of APC 1. Cache Duration:

The APC cache is divided into two parts: the system cache and the user data cache.

System cache: is automatically used, refers to the PHP file source code compiled by APC cache, and then on the second call to compare the time stamp. If it does not expire, it is run with cached code. The default cache is 3600s (one hour). But that still wastes a lot of CPU time. Therefore, you can set the system cache to never expire (apc.ttl=0) in php.ini. However, if you do this, you need to restart your Web server after changing the PHP code ( such as Apache ...). The current performance test for APC is generally referred to as this layer of cache;


User data caching: Read and write by the user when writing PHP code with Apc_store and Apc_fetch functions. If the amount is not small, I suggest you can use it. If the volume is large, I recommend using memcache better.
If you want to enjoy the characteristics of the upload progress of large cache files by APC, you need to set apc.rfc1867 to 1 in php.ini, and add a hidden field apc_upload_progress in the form, the value of this field can randomly generate a hash. To be the only one. See the links given above for specific examples.

6. Test efficiency

From the Ethna framework to test:

First time execution:

Time:260.53491973877ms
Memory:6.2379684448242mb

Second execution:

Time:199.43404197693ms
Memory:1.1883926391602mb

Improved performance by 30%.

7, APC and memcache contrast

1, use Apc_fetch to get data, about 1-2 microseconds each time. (100,000 Times 170ms)
2, use Memcache::get to get the data of this server through localhost, about 41 microseconds each time. (100,000 Times 4160ms)
3, use Memcache::get to obtain the data of this server through the native IP address, about 42 microseconds each time. (100,000 Times 4268ms)
4, use Memcache::get to obtain data from other machines of the same network segment (Gigabit Ethernet) by IP address, about 110 microseconds each time. (100,000 Times 11268ms)

5, use Redis::get to obtain data from other machines of the same network segment (Gigabit Ethernet) by IP address, about 220 microseconds each time. (100,000 Times 21700ms)

8. APC of php5.5

APC is the best-performing code cache in PHP5.4 and below. However, when PHP is upgraded to 5.5 and above, APC is no longer valid. Need to use Zend's opcache extension. PHP 5.5 has integrated Zend Opcache feature cache faster than APC, Eaccelerator, XCache

To enable the Opcache extension, there are two steps:

1. Installing Opcache

Compile and install PHP5.5 add –enable-opcache

2. Add the following configuration to the php.ini file :

Zend_extension=opcache.so
[Opcache]
opcache.memory_consumption=128
Opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
Opcache.revalidate_freq=60
Opcache.fast_shutdown=1
Opcache.enable=1
Opcache.enable_cli=1

APC Cache opcode (RPM)

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: info-contact@alibabacloud.com 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.