My Virtualmin CentOS 5.5 webserver runs several virtual domains with PHP under FCGId using APC for opcode caching: Joomla, PHPbb, Wordpress, etc . APC is somewhat challenging to configure properly with fcgid-enabled virtual domains, but it is quite helpful to optimize system resources and prevent traffic surges on one site from affecting overall server performance. Joomla and PHPbb have code that is pre-optimized with hooks for APC, but Wordpress requires a plugin called W3 Total Cache.
Installation Suggestions and Modification of Defaults
Be sure to install the newest version of APC, 3.1.4 as of this writing (I assume you know how to install and configure all the packages mentioned in this post):
pecl install apc-beta
For W3 Total Cache (Wordpress plugin):
Do not choose APC for the W3TC’s Page Cache, choose Enhanced Disk.
Do not choose APC for Minify, use Disk.
For APC running on virtual servers for opcode caching:
Remove apc.ini from /etc/php.d. Do not add the APC configuration into /etc/php.ini (main php.ini which is probably a template used for creation of new virtual servers).
Instead, enable APC on a per-domain basis by modifying the respective /home/domainname.com/etc/php5/php.ini
You must “babysit” the configuration: use the APC monitor (apc.php) to see how many files are being cached, and how much memory (apc.shm_size) is needed to avoid fragmentation, which will cause the 500 errors. If fragmentation rises quickly to 100%, memory usage is full, and cache full count rises above 0, you will almost surely see “500” errors.
If Wordpress is the only PHP script software package on the domain, the following configuration will likely work for you; if you have more PHP software packages, you will need to raise apc.shm_size and raise the apc.user_entries_hint and apc.user_entries_hint settings, and possibly lower TTL values to allow cached files to expire and be replaced.
In general, you must double apc.shm_size in relation to APC’s highest reported Memory Usage to avoid fragmentation completely.
Configuration Suggestion for php.ini
in the virtual server's /home/domainname.com/etc/php5/php.ini, add the following configuration:
(you should also install memcached and fileinfo for full use of W3 Total Cache)
[memcached]
extension=memcache.so
[fileinfo]
extension=fileinfo.so
[APC]
extension = apc.so
apc.enabled = 1
apc.shm_segments = 1
apc.shm_size = 32M
apc.optimization = 0
apc.num_files_hint = 512
apc.user_entries_hint = 1024
apc.ttl = 0
apc.user_ttl = 0
apc.gc_ttl = 600
apc.cache_by_default = 1
apc.filters = “apc.php$”
apc.slam_defense = 0
apc.use_request_time = 1
apc.mmap_file_mask = /dev/zero
apc.file_update_protection = 2
apc.enable_cli = 0
apc.max_file_size = 2M
apc.stat = 0
apc.write_lock = 1
apc.report_autofilter = 0
apc.include_once_override = 0
apc.rfc1867 = 0
apc.rfc1867_prefix = “upload_”
apc.rfc1867_name = “APC_UPLOAD_PROGRESS”
apc.rfc1867_freq = 0
apc.localcache = 1
apc.localcache.size = 512
apc.coredump_unmap = 0
apc.stat_ctime = 0
Save php.ini, restart apache and then watch the APC Monitor to ensure fragmentation stays below 50% (it should remain less than 10% most of the time… if fragmentation rises quickly, you should add a few more MB’s of memory to apc.shm_size and restart apache).
Monitoring APC Performance and Detecting Errors
You should run apc.php in a password-protected directory in the domain's public_html. Edit the apc.php code to allow passwordless access.
defaults('USE_AUTHENTICATION',0);
You can monitor errors with these commands:
tail -f -n 50 /var/log/httpd/error_log
tail -f -n 50 /home/domainname.com/logs/error_log
In the logs, you should see messages like the following:
mod_fcgid: process /home/username/public_html/scriptname.php(21272) exit(lifetime expired), terminated by calling exit(), return code: 0
… exit(server exited) …
… exit(idle timeout) …
if the cache is working well. If you see exit(communication error)
then you have issues, probably cache full and fragmented.