How to Install APC on CentOS 6

From Acenet Knowledgebase
Revision as of 23:16, 30 November 2012 by Docs admin (Talk | contribs) (Created page with "APC (Alternative PHP Cache) is an opcode cache for PHP. APC provides a caching system for intermediate PHP code and gain greatly increase PHP performance. PHP is an interpre...")

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

APC (Alternative PHP Cache) is an opcode cache for PHP. APC provides a caching system for intermediate PHP code and gain greatly increase PHP performance. PHP is an interpreted language, meaning, the code is interpreted on each request for a file. When a visitor requests a PHP script, the PHP code is read, interpreted into bytecode and then executed. PHP's Zend Engine does the interpreting very quickly, but when you are serving hundreds or thousands of requests per seconds, every CPU cycle counts.

This is where APC comes in. APC allows your webserver to cache the interpreted bytecode directly, there's no need to re-read the PHP script and re-interpret it. Knowing that the PHP source code of your website rarely changes, this can greatly speed up your web applications.

Installing APC

Installation of PHP is straightforward on CentOS 6, there's a readily available RPM for PHP. You can easily install APC with yum:

php-pecl-apc</syntax>

As always, be sure to restart your web server after installation for the new module to take effect.  If you're using Apache, you can run:

<syntaxhighlight lang="bash">/etc/init.d/httpd restart</syntax>

==Configuring APC==

Installing APC via yum will generate an apc.ini file which can be modified to tweak your APC installation.  The apc.ini file is located here:

<syntaxhighlight lang="bash">/etc/php.d/apc.ini</syntax>

The popular eCommerce software Magento specifically suggests using APC to speed up performance.  We're going to provide our recommended APC settings for use with Magento.  You may find that other settings provide better performance for other applications, but these recommendations should give you a good starting point.

===apc.shm_size===

This ini directive is one of the most important ones.  This controls how much memory APC will use to cache PHP opcode.  By default, the value is set at 64M.  While this may work for smaller sites, we generally recommend increasing this to get the best performance from APC.  We suggest setting it to 128M, but if you have more free RAM available, you may find increasing it beyond this will further speed up your site:

<syntaxhighlight lang="bash">apc.shm_size=128M</syntax>

Take note that the value must be followed by an M or G to denote megabytes or gigabytes.

===apc.stat===

This directive defines whether APC will check a requested PHP file for modifications since the last time it was cached.  Leaving this variable set to 1 is often useful in a development environment where you may be frequently modifying code.  If you're using APC in a production environment, you can gain additional performance by setting this to 0.  When upgrades are necessary, you can update your code and restart your webserver to clear the APC cache. 

<syntaxhighlight lang="bash">apc.stat=0</syntax>

===apc.num_files_hint===

The apc.num_files_hint directive helps APC know how many unique files you plan to cache and can help APC better manage the memory cache.  We suggest setting this value to 10000. 

<syntaxhighlight lang="bash">apc.num_files_hint=10000</syntax>

===apc.user_entries_hint===

apc.user_entries_hint gives APC a hint about how many distinct user cache variables you plan to store.  We recommend a setting of 10000 for this directive as well.

<syntaxhighlight lang="bash">apc.user_entries_hint=10000</syntax>

===apc.max_file_size===

This directive defines the largest single file you want to cache.  Files larger than this size will not be cached by APC.  We suggest a value of 5M.

<syntaxhighlight lang="bash">apc.max_file_size=5M</syntax>

===Restart httpd===

Once again, restart httpd for the settings to take effect.

<syntaxhighlight lang="bash">/etc/init.d/httpd restart</syntax>