Cache PHP scripts with APC module
Monday, September 11, 2006 11:42 AM
Learn how to boost your server performance by using APC to cache PHP scripts.
Looking to get every ounce of performance from your Web server? If you're using PHP scripts, you can easily accomplish this using PHP's APC module, which caches php opcode instead of forcing php to reinterpret every script every time it is executed. On a PHP5 system with PEAR support, installation of APC is as easy as executing, as root:
# pecl install APC
Some distributions likely provide binary packages of APC, so you may be able to install it using urpmi php-apc or apt-get install php-apc.
Once APC is installed, edit /etc/php.ini to add the following:
extension=apc.so
[apc]
apc.enabled = 1
apc.shm_segments = 1
apc.shm_size = 30
apc.optimization = 0
apc.ttl = 7200
apc.user_ttl = 7200
apc.num_files_hint = 1000
apc.mmap_file_mask = /tmp/apc.XXXXXX
What this does is enable APC caching, but not the (currently experimental) optimizer. The important options to note, besides enabling APC, are the apc.ttl and apc.user_ttl options, which define the time for a script to remain in the cache, in seconds. This is really important for a server that serves a lot of files; this will prevent the cache from filling up with stale entries that could prevent newer entries from being cached. You could also tweak the apc.num_files_hint, which gives APC an approximation of the number of distinct PHP source files that will be requested or included on your system. The default here is 1000, but if you know you serve many more files than that, increase the number to a best-guess estimate. Likewise, if you have a lot less files than that, reduce the number accordingly.
Finally, in the source package for APC is a script called apc.php, which can be used to obtain detailed statistics from the APC cache to allow you to further tweak the APC settings. You will need to edit the script and change the ADMIN_PASSWORD option in order to use it. By tweaking the options and using this script to determine how well your changes are taking, you can really give your PHP scripts a nice performance boost.



There are currently no comments for this post.