help centre
For more info visit status.atmail.com

How can we help?


Search our knowledge base for answers to
common questions and latest updates.



My activities New request

Follow

Increasing PHP Performance using APC

Nathan Salt -

PROBLEM

I would like to increase the performance of PHP on my server.

ENVIRONMENT

  • On-Premise Server + WebMail Installations: Version 7.0 > Current Version
  • Webmail Only Installations: Version 7.0 > Current Version

CAUSE

APC isn't being used. The Alternative PHP Cache (APC) is a free and open opcode cache for PHP, designed to optimize PHP intermediate code.

RESOLUTION

  1. Check that you have PHP PECL and Apache APXS installed on your system.
    If you don't, install them first.
    yum install php5-pear
    yum install httpd-devel
  2. Install APC
    pecl install apc
  3. Enable the APC by adding the following line to your PHP configuration file `/etc/php.ini`
    extension=apc.so
    You can test that the APC module is installed by using the 'php -m' command.

  4. Restart Apache

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 Atmail 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 Atmail 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 Atmail 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 Atmail is also serving other PHP scripts then you may want to limit the caching to Atmail 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 Atmail 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 Atmail's web-root to turn it back on for Atmail 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 Atmail 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 Atmail:

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 Atmail 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.

Have more questions? Submit a request

Comments


Contact our support team


+61 (7) 5357 6605       support@atmail.com