Agnivo Roy RSS Feed

Cache PHP Scripts With APC PECL Module

1 06 2007

PHP Caching with APCWant to squeeze out every drop of performance from your Web server for PHP parsed dynamic pages? 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 the scripts every time they are executed.

The APC PECL extension ( Alternative PHP Cache ) is a free, open, and robust framework for caching and optimizing PHP intermediate code. APC has proved to be a better solution than similar ones like eAccelerator and xCache.

Requirements

  • PHP 4.3.0 +
  • PEAR 1.4.0 +
  • Root login to server (SSH)

On a PHP5 system with PEAR support, installation of APC is as easy as executing, as root:
# pecl install apc

For some distributions, there maybe binary packages available for you to use :
# urpmi php-apc
OR for Debian based distros :
# apt-get install php-apc

You can also compile APC from source, but automatic methods should work well for most of you. APC works best on *NIX based distros and on Windows platform too; but Linux is highly recommended.

After APC is installed, you’ve to edit /etc/php.ini as shown below :
# nano /etc/php.ini

extension = "apc.so"
[apc]
apc.enabled = 1
apc.shm_size = 30
apc.ttl = 7200
apc.user_ttl = 7200
apc.mmap_file_mask = /tmp/apc.XXXXXX

Some common, optional APC flags you can customize based on your needs :
apc.shm_segments = 1
apc.optimization = 0
apc.num_files_hint = 1000

Complete documentation on APC and APC flags is available here : [ PHP APC PECL Extension ]. Please read it and you’ve to set them correctly based on your needs and implementation at your server.

The above configuration should be enough for most VPSs or dedicated servers. APC is web server independent (sort of). You need to restart your web server after configuration is done. For Apache web server, issue the following command :
# /sbin/service httpd restart

You can also find a file called apc.php in the source package of APC. Edit it to customize a username and password and upload it to your server. Open it in your browser and it provides a nice graphical ( if php-gd is available ) frontend to monitor what’s being cached.

I’ve been using APC for months on my VPSs and the result has been beyond my expectations : very fast dynamic PHP parsed page loads and less CPU loads as PHP files need not to be interpreted at every hit; only one time and subsequently all requests are served from memory cache.

However, don’t use it along with Zend Optimizer (not zend extension manager) , your sites may stop working completely. I heard APC will be an integral part of PHP6 when it comes out. Till then, APC rocks ! :)


Disclosure Policy rss Comments RSS   trackback Trackback
Posted on June 1, 2007 and Last Updated on June 2, 2007 in Internet, SEO & Tech.
Tags : , , , , , ,

Other Interesting Posts

Leave a comment

You can use these tags : <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>