Setting up the Alternative PHP Cache on Ubuntu

As some of you might know, PHP scripts are compiled to bytecode right before they are executed. This process costs time and server resources. Also this is done all over again every single time your script gets executed, even if your script hasn't been changed in years. So what could be more obvious than caching the results of the compilation to reduce load on your server. Today I want to show you one way of achieving this, with a little help from the Alternative PHP Cache (APC).

Installation

Installation of the Bytecache on Apache is pretty simple. Again we will use apt-get to download and install the required packages. To do this we will need access to the Shell of our server. So make sure you have a user with root privileges for your server.

sudo apt-get update
sudo apt-get install php-pear php-apc

Afterwards we will have to restart Apache with the following command:

sudo service apache2 restart

The Alternative PHP Cache should already be running now. You can check this by adding PHP file with a phpinfo() output. In the table you should look for something like the following entries:

Alternative PHP Cache phpinfo()

APC comes with a simple debugging tool. Normally this is located inside the folder /usr/share/doc/php-apc. Before we can actually use the tool, we will need to extract it with the following command:

gzip -d /usr/share/doc/php-apc/apc.php.gz

Now you either need to copy the extracted files to your web folder or just set a symbolic link. But be careful: Setting a link will only work, if your allowed Apache to follow symlinks. If this is not the case, you can just drop a .htaccess file inside your web root with the following contents:

Options FollowSymLinks

After doing that you can set the symlink:

ln -s /usr/share/doc/php-apc/apc.php /your-web-folder/apc.php

Security wise it isn't the best idea to make this file accessible to the entire world. If you don't know how to restrict access to the tool, you might want to check my recent article on access restrictions via .htaccess.

The debugging tool

At this point we already went through all the necessary steps to prepare the configuration of APC. It should be noted that most of the time the default settings are perfectly fine. By accessing apc.php through a web browser we can view the currently active configuration.

The first thing you will notice is the graphic display of the cache utilization. It's important to get a high hit rate and a low fragmentation. Fragmentation will increase over time, so it would be wise to clear the cache from time to time.

Alternative PHP Cache Status 1

Actually the cache is reset on every restart of Apache and when it hits maximum capacity. Data concerning how often the cache has been cleared are located to the left of the graphic.

Alternative PHP Cache Status 2

At the bottom your current configuration is listed in detail. You can change the settings in the apc.ini file. Every change of the configuration requires a restart of the Apache to take effect.

Alternative PHP Cache Config

I don't want to discuss the settings in detail here. More information on the configuration options can be found in the documentation of APC itself. The configuration file is located in /etc/php5/conf.d/apc.ini. Be careful: You can easily break your Apache when you mess up something in the config. E.g. if your shm_size is higher than your server allows, Apache will hang on restart. I found this out the hard way. So make sure you have a backup in place before you change anything.

Using APC in PHP

One of the cooler features of APC is, that you can actually use it as a cache in your PHP scripts. This way you are able to store nearly any kind of data in an easily accessible cache. APC stores data in memory, so retrieving it is pretty fast. Way faster than a file or database cache for example. Most of PHP frameworks or Content-Management-Systems utilize this feature in one way or another. But be careful: APC is by no means a persistent storage solution. When Apache is restarted, your data will be lost.

I collected a few handy functions, which you could use right now. Documentation on this functions can be found on php.net:

apc_store(string $key , mixed $var)

Stores a variable $var behind the key $key.

apc_fetch ( mixed $key [, bool &$success ] )

Returns the variable stored behind the key $key.

apc_clear_cache ([ string $cache_type = "" ] )

Flushes the cache of the type $cache_type. The cache used within PHP has the type user. If you don't define a type the Bytecache will be flushed.

More command and information can be found here:
http://www.php.net/manual/en/ref.apc.php

Alternatives

Of course APC is not the only Bytecache for PHP. eAccelerator, XCache or Zend Guard (previously known as Zend Optimizer) are popular alternatives. Which Bytecache you might want to use, depends on your demands or personal preferences.

More information on PHP accelerators can be found here: https://en.wikipedia.org/wiki/List_of_PHP_accelerators