Cache PHP scripts with APC module
Takeaway: 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.
Delivered each Tuesday, TechRepublic's free Linux NetNote provides tips, articles, and other resources to help you hone your Linux skills. Automatically sign up today!
White Papers, Webcasts, and Downloads
- Five Steps to Determine When to Virtualize YourServers VMware Thinking of virtualizing the servers at your company? Use this step-by-step guide to determine when's the best time to make your big move. Download Now
- Why Isn't Server Virtualization Saving Us More? A Few Small Changes May Dramatically Increase Your Efficiency VMware Ever wonder why your company isn't saving more from its server virtualization? Making a few small changes could dramatically increase your efficiency. Download Now
- The Scalable Enterprise: VMware ESX Server on the Dell PowerEdge 6650 Dell This paper introduces the server virtualization software, VMware ESX ... Download Now
- The Impact of Virtualization Software on Operating Environments VMware Today's use of virtualization technology allows IT professionals to ... Download Now
- Building the Virtualized Enterprise with VMware Iinfrastructure VMware VMware virtualization software has been adopted by over 120,000 enterprise ... Download Now
Article Categories
- Security
- Security Solutions, IT Locksmith
- Networking and Communications
- E-mail Administration NetNote, Cisco Routers and Switches
- CIO and IT Management
- Project Management, CIO Issues, Strategies that Scale
- Desktops, Laptops & OS
- Windows 2000 Professional, Microsoft Word, Microsoft Excel, Microsoft Access, Windows XP,
- Data Management
- Oracle, SQL Server
- Servers
- Windows NT, Linux NetNote, Windows Server 2003
- Career Development
- Geek Trivia
- Software/Web Development
- Web Development Zone, Visual Basic, .NET





