This article will detail the procedures required to install and configure the APC opcode cache on your server. This article does not cover setting up APC on Windows servers.
What is APC?
From php.net:
"The Alternative PHP Cache (APC) is a free and open opcode cache for PHP. It was conceived of to provide a free, open, and robust framework for caching and optimizing PHP intermediate code."
So basically we can use APC to squeeze even better performance out of @Mail.
Installing APC
APC is a PHP PECL extension so I will cover using the pecl command to insall APC. If you find that pecl is not available on your system performing the following command for your system should install it:
Ubuntu/Debian: apt-get install pear
FreeBSD: pkg_add -r php5-pear
RedHat Linux (inc Centos, Fedora etc): yum install php5-pear
You will also need the apache apxs tool. If apxs is not on your system you can install it as such:
Ubuntu/Debian: apt-get install apache2-threaded-dev
FreeBSD:
RedHat Linux (inc Centos, Fedora etc):
Once you have pecl and apxs installed perform the following command:
# pecl install apc
Once the compile and installation is complete you will need to enable the extension by adding the following line to your php.ini:
extension=apc.so
You can test that the APC module is installed by using the 'php -m' command. This will list the installed PHP modules and APC should be near the top:
# php -m
[PHP Modules]
apc
bcmath
bz2
calendar
ctype
date
dba
...etc
Once you have enabled APC in php.ini, in order to load APC for the Apache PHP module simply restart Apache, usually
apachectl restart
or
apache2ctl restart
Now APC is ready and waiting to spice up performance.
Configuring APC
APC has several settings you can tweak according to your setup and desired functionality. The first two settings you should consider are
apc.shm_size
and
apc.stat
These two settings adjust how much shared memory you want to set aside for APC and whether you want APC to check for file modification on every request, respectively.
First lets consider
apc.shm_size
How large a memory segment you can assign to APC depends on amount of available RAM and your OS type and configuration. The amount is specified in MB and the default is 30. In my testing with APC and @Mail I maxed out at 8.895MB of memory usage for the APC cache so I'd suggest the recommended minimum setting for a server running @Mail is about 10MB. If the server is running other php scripts via apache then you will need to either increase this value or use the technique described in the "Conditional Caching" section.
This setting determines whether APC checks for file modification on every request. The default is 'On', and APC will check each script for modification upon each request. If the script has been modified then it will not use the cached version, but recompile and re-cache the new version. Also with apc.stat
on, with every request made APC will have to find absolute paths for all files included or required with a relative path. This obviously adds some overhead.
Changing apc.stat to 'Off' can produce a significant performance gain and simply means if you modify any @Mail files you will need to restart Apache before the changes will come into effect. Not a big deal on a production server where files stay static for long periods.
Conditional Caching
If the server running @Mail is also serving other PHP scripts then you may want to limit the caching to @Mail scripts only in order to reduce the chance of running out of cache slots or allocated RAM. You can do this by using the following settings: apc.cache_by_default
apc.cache_by_default is On by default and means all php scripts served by Apache are cached. If you want to cache only @Mail scripts then you will need to set apc.cache_by_default = Off
in your php.ini or apc.ini file.
Once you have turned apc.cache_by_default off then you can use a .htaccess file in @Mail's web-root to turn it back on for @Mail only. If you have other scripts/apps you would like to enable caching for then simply add the .htaccess file to their web-root also. The .htaccess file should contain just this line:
php_value apc.cache_by_default On
Once you have the .htaccess file in place all @Mail scripts will be cached. You will also need to restart Apache for the main apc.cache_by_default = Off setting to take.
How Can I Tell What is Currently Cached?
A simple solution is to create a short php script with this in it:
print_r(apc_cache_info());
Save it to a file that is web readable and load it within your web browser. This will give you some information on the current state of your APC cache, including what files are cached. Here is an excerpt of the output given after having used @Mail:
Array
(
[num_slots] => 2000
[ttl] => 0
[num_hits] => 11
[num_misses] => 35
[start_time] => 1214447228
[expunges] => 0
[mem_size] => 4645714
[num_entries] => 35
[num_inserts] => 35
[file_upload_progress] => 1
[memory_type] => mmap
[locking_type] => pthread mutex
[cache_list] => Array
(
[0] => Array
(
[filename] => /usr/local/atmailphp/webmail/libs/PEAR/Mail/mime.php
[device] => 2051
[inode] => 261912
[type] => file
[num_hits] => 0
[mtime] => 1203282990
[creation_time] => 1214447477
[deletion_time] => 0
[access_time] => 1214447477
[ref_count] => 0
[mem_size] => 125640
)
[1] => Array
(
[filename] => /usr/local/atmailphp/webmail/libs/PEAR/Mail/RFC822.php
[device] => 2051
[inode] => 261911
[type] => file
[num_hits] => 0
[mtime] => 1207549212
[creation_time] => 1214447477
[deletion_time] => 0
[access_time] => 1214447477
[ref_count] => 0
[mem_size] => 120242
)
[2] => Array
(
[filename] => /usr/local/atmailphp/webmail/libs/PEAR/Mail/mimePart.php
[device] => 2051
[inode] => 261908
[type] => file
[num_hits] => 0
[mtime] => 1203282990
[creation_time] => 1214447477
[deletion_time] => 0
[access_time] => 1214447477
[ref_count] => 0
[mem_size] => 58013
)
Final Words
There are several other APC settings that you may wish to tweak for your system. More information on these settings can be found here: http://php.net/manual/en/apc.configuration.php
You should place all these setting in your php.ini, or alternatively if you have a directory on your system that php will read .ini files from then create a file in there called apc.ini and place the APC settings in it.
You should find that @Mail and APC work just fine together and you should see improved performance, especially on busier servers - of course if you have any feedback or experience problems let us know.