On GameSpot: And the best games of E3 were...

Cache PHP scripts with APC module

Tags: Scripting languages, Vincent Danen, American Power Conversion Corp., PHP, Linux Tips Newsletter

  • Save
  • Print
  • Recommend
  • 16

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!

  • Save
  • Print
  • Recommend
  • 16

What do you think?

White Papers, Webcasts, and Downloads

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

Popular IT Dojo Videos

advertisement
Click Here