Friday 04 August 2006 1:41:00 pm
APC is an Open Source PHP accelerator for caching intermediate code from scripts. Intermediate code is the internal memory structures produced during compilation that are fed into the executor. APC increases the performance of PHP scripts by caching them in their compiled state, so that the overhead of compiling is almost completely eliminated. APC stores and executes compiled PHP scripts from shared memory.
APC is available as an extension for PHP from the PECL repository. You can download the latest version from the APC home page ( http://pecl.php.net/package/APC).
Before you start installing APC, make sure that the development tools listed in the APC prerequisites (such as autoconf and libtool) are installed on your system. Also, you may require access to the root account.
The basic commands you must execute to compile and install APC from source are:
APC is available as an extension for PHP from the PECL repository. You can download the latest version from the APC home page ( http://pecl.php.net/package/APC).
Before you start installing APC, make sure that the development tools listed in the APC prerequisites (such as autoconf and libtool) are installed on your system. Also, you may require access to the root account.
The basic commands you must execute to compile and install APC from source are:
shell> cd /usr/local/src shell> gunzip < /PATH/TO/APC-x.x.x.tgz | tar xvf - shell> cd APC-x.x.x shell> phpize shell> ./configure --enable-apc-mmap shell> make shell> su - shell> make install
These commands are interpreted as follows:
1. Choose the directory under which you want to unpack the distribution, and then change location into it. In the following example, we unpack the distribution under /usr/local/src.
shell> cd /usr/local/src</span>
2. Obtain an APC distribution from http://pecl.php.net/package/APC.
3. Unpack the source file, which creates the installation directory. This example shows how to unpack the distribution using gunzip:
shell> gunzip < /PATH/TO/APC-x.x.x.tgz | tar xvf -
The tar command creates a directory called "APX-x.x.x" (where "x.x.x" is the APC version number). If you are using GNU tar, no separate invocation of gunzip is necessary. Instead, you can use the following command to uncompress and extract the distribution:
shell> tar zxvf /PATH/TO/APC-x.x.x.tgz
4. Change location into the installation directory:
shell> cd APC-x.x.x
5. Run phpize. phpize is a script that is included with the PHP distribution, and is by default located in /usr/local/php/bin (assuming you installed PHP in /usr/local/php).
shell> /usr/local/php/bin/phpize
The output from running phpize should look like the following:
Configuring for: PHP Api Version: 20020918 Zend Module Api No: 20020429 Zend Extension Api No: 20050606
6. Run the configuration script with the following parameters:
shell> ./configure --enable-apc-mmap
The --enable-apc-mmap option tells the APC configuration script to use mmap instead of the default IPC shared memory support.
The configure script will take several minutes to run, as it tests for the availability of packages and prerequisites on your system and builds the Makefiles that will later be used to compile APC.
7. Now you can build the APC components by running the command:
shell > make
8. Switch to the root user to execute the following commands:
shell> su -
If you don't have access to the root account, ask your system administrator for help.
9. Next, install the package by running:
shell> make install
10. When the installation is done, make install should print the path to the extension.
Edit your php.ini and set the path to the apc.so extension:
extension="/path/to/extension/apc.so"
Now you can restart your web server (using the command /usr/local/apache/bin apachectl restart) and create a small test PHP file in your document root (by default /usr/local/apache/htdocs). The file should contain just the following line:
<?php phpinfo(); ?>
Put the test.php file in the Apache documents directory (/usr/local/apache/htdocs) and access it in the web browser via the URL http://localhost/test.php. APC should be listed in the extensions list. If APC is not shown, check the Apache error log (/usr/local/apache/logs/error.log) for possible errors.
Another way to install APC is to use the PECL ("PHP Extension Community Library") installer. Run sudo peclinstall APC from the shell prompt.
Add the following lines to your php.ini file (located by default in /usr/local/lib/php.ini):
; Load APC extension extension=/usr/local/lib/php/extensions/no-debug-non-zts-20020429/apc.so ; This can be set to 0 to disable APC. This is ; primarily useful when APC is statically compiled ; into PHP, since there is no other way to disable ; it (when compiled as a DSO, the zend_extension ; line can just be commented-out) ; (Default: 1) apc.enabled = 1 ; The size of each shared memory segment in MB. ; By default, some systems (including most BSD ; variants) have very low limits on the size of a ; shared memory segment. ; (Default: 30) apc.shm_size=128
APC comes with useful graphical monitoring tool. It provides information about the APC state, cached files, amount of used and free memory, etc. Using this APC application, you can also clear all the caches without restarting Apache. This tool is also useful for tuning memory usage.
To enable the APC application, copy apc.php from the APC-x.x.x directory to the Apache document directory (/usr/local/apache/htdocs). Access the application from a web browser via the URL http://localhost/apc.php.
The screenshot below shows the APC application:
APC application