Difference between revisions of "How to Install APC on CentOS 6"
Docs admin (talk | contribs) |
Docs admin (talk | contribs) |
||
Line 5: | Line 5: | ||
==Installing APC== | ==Installing APC== | ||
− | Installation of | + | Installation of APC is straightforward on CentOS 6, there's a readily available RPM for PHP. You can easily install APC with yum: |
<syntaxhighlight lang="bash">yum install php-pecl-apc</syntaxhighlight> | <syntaxhighlight lang="bash">yum install php-pecl-apc</syntaxhighlight> |
Latest revision as of 15:20, 12 December 2012
APC (Alternative PHP Cache) is an opcode cache for PHP. APC provides a caching system for intermediate PHP code and can 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 APC is straightforward on CentOS 6, there's a readily available RPM for PHP. You can easily install APC with yum:
yum install php-pecl-apc
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:
/etc/init.d/httpd restart
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:
/etc/php.d/apc.ini
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:
apc.shm_size=128M
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.
apc.stat=0
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.
apc.num_files_hint=10000
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.
apc.user_entries_hint=10000
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.
apc.max_file_size=5M
Restart httpd
Once again, restart httpd for the settings to take effect.
/etc/init.d/httpd restart
apc.php
Included with the APC RPM is the script apc.php. This script will break down your APC performance and give you statistics regarding cache usage and cache hits/misses. apc.php is located here:
/usr/share/doc/php-pecl-apc-*/apc.php
Optionally, if you have GD installed, this script will display helpful graphs of your APC statistics. If you do not have GD installed, you can view the KB article on How to Install GD for PHP on CentOS 6
To use apc.php, simply copy this script to a web-accessible location. In our example, we'll copy it directly to our Apache server's web root:
mkdir /var/www/html/apc
cp /usr/share/doc/php-pecl-apc-*/apc.php /var/www/html/apc
apc.conf.php
apc.php has an optional configuration file, apc.conf.php. The apc.php script gives you the ability to view user data or to clear your APC cache altogether. For this reason, we suggest setting an administrator username and password and this can be done using the apc.conf.php configuration file. Open this file in your favorite text editor, we'll use nano in our example:
nano /var/www/html/apc/apc.conf.php
Add these lines to the file, replacing MYUSERNAME and MYPASSWORD with your desired username and password:
<?php
defaults('ADMIN_USERNAME','MYUSERNAME');
defaults('ADMIN_PASSWORD','MYPASSWORD');
?>
then save the file. This will password protect the sensitive areas of your apc.php script.
Example Output
If you visit apc.php in a browser, you should see output similar to the following image. Note that we have GD installed in this example so apc.php displays graphs of our cache performance.