Creating a Free and Open Source Software ecosystem to facilitate government FOSS policy implementation
by Derek Keats
Using the present to create the future: How can we move South Africa from consumer to producer of web technologies
by Derek Keats
An ecosystem approach to building mobile opportunities into a business strategy
by Derek Keats
Your Chisimba production or enterprise site will be much faster and more reliable if you install an opcode cache or accelerator for PHP. Most PHP accelerators work by caching the compiled bytecode of PHP scripts to avoid the overhead of parsing and compiling source code on each request (some or even most of which may never be executed). To further improve performance, the cached code is stored in shared memory and directly executed from there, minimizing the amount of slow disk reads and memory copying at runtime.
We recommend APC (Alternative PHP Cache), a free, open source framework that optimizes PHP intermediate code and caches data and compiled code from the PHP bytecode compiler in shared memory. See http://en.wikipedia.org/wiki/List_of_PHP_accelerators for alternatives, including a proprietary commercial one. See http://www.php.net/manual/en/intro.apc.php for more information about APC.
To install APC, you can use PECL, which should be installed on your server already if you have installed a typical LAMP stack or followed the Chisimba installation instructions or run one of our installer scripts. PECL is a repository of PHP extensions that are made available via the PEAR packaging system. You need build-essential on your system, as well as the Perl-compatible regular expression library (PCRE). If you have not installed it, first install it with:
$ sudo apt-get install build-essential libpcre3 libpcre3-dev
The command
$ sudo pecl install extname
downloads the extension automatically, so in this case there is no need for a separate download. You can therefore install APC by running:
$ sudo pecl channel-update pecl.php.net
$ sudo pecl install apc
You will be asked:
Enable internal debugging in APC [no] :
Enable per request file info about files used from the APC cache [no] :
Enable spin locks (EXPERIMENTAL) [no] :
Enable memory protection (EXPERIMENTAL) [no] :
Enable pthread mutexes (default) [yes] :
Enable pthread read/write locks (EXPERIMENTAL) [no] :
You can select the default for all of them. You will then get a lot of things written to the terminal window, finishing with:
Build process completed successfully
Installing '/usr/include/php5/ext/apc/apc_serializer.h'
Installing '/usr/lib/php5/20090626/apc.so'
install ok: channel://pecl.php.net/APC-3.1.9
configuration option "php_ini" is not set to php.ini location
You should add "extension=apc.so" to php.ini
The important thing here is that if it finishes with errors, you need to do some debugging, perhaps you are missing some of the critical components. An important note here is "You should add "extension=apc.so" to php.ini". To do so,
$ vi /etc/php5/apache2/php.ini
In the future, if new versions of APC are released, you can easily upgrade them using
$sudo pecl upgrade apc
There are two primary decisions to be made configuring APC. First, how much memory is going to be allocated to APC; and second, whether APC will check if a file has been modified on every request. The two ini directives that control these settings are apc.shm_size and apc.stat, respectively. apc.shm_size defaults to "32M" and is changeable in PHP_INI_SYSTEM. apc.stat defaults to "1" and is also changeable in the PHP_INI_SYSTEM. I would leave them at their default values and monitor your installation.
However, James Scoble, who manages Chisimba installations at UWC, which runs http://www.uwc.ac.za as well as their eLearning server. He says
"we use APC with 300Mb shared RAM. Without APC enabled on the PHP level, the Portal server was running out of RAM and starting to disk-thrash."
-- James Scoble, UWC
UWC also runs multiple application servers, with load balancing due to the large volumes of traffic. On a system with 8Mb of RAM, it should be OK to set this to 256Mb or so, but tuning a server settings is really about watching how it behaves, and responding accordingly.
Once the server is running, the apc.php script that is bundled with the extension should be copied somewhere into the docroot and viewed with a browser as it provides a detailed analysis of the internal workings of APC.
$ cp /usr/share/php/apc.php /var/www/
Then open http://yourserver.com/apc.php. If you get
No cache info available. APC does not appear to be running
then APC is not activated, and you need to take the recommended action. Edit your php.ini file as noted above, find the section that says
;;;;;;;;;;;;;;;;;;;;;;
; Dynamic Extensions ;
;;;;;;;;;;;;;;;;;;;;;;
and add
extension=apc.so
immediately after that text, write the file and restart apache using
$ service apache2 restart
Open http://yourserver.com/apc.php again and you should get some information about APC on your system, as well as some graphs. Be sure to move this file outside your web root, putting it back only when you need to use it.