PHP Opcode Cache Comparison: Optimal Choice for WordPress on VPS (APC vs XCache vs Memcache vs eAccelerator)


2 views

When running WordPress on a VPS with limited resources (like your 512MB RAM + 400MB swap setup), opcode caching isn't just about performance - it's about survival. The major players today are:

  • APC (Alternative PHP Cache): The community favorite, now included in PHP core as APCu (user cache only)
  • OPcache: Bundled with PHP 5.5+ as the new standard
  • XCache: Lightweight alternative with good WordPress compatibility
  • eAccelerator: Older solution with declining support

For your WordPress-focused VPS, here's why OPcache stands out:

; Sample OPcache configuration for WordPress in php.ini
[opcache]
opcache.enable=1
opcache.memory_consumption=128
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60
opcache.fast_shutdown=1
opcache.enable_cli=0 ; Important for WP-CLI compatibility

Key advantages:

  • Zero configuration for basic WordPress operation
  • Lower memory overhead (crucial for your 512MB VPS)
  • Automatic cache invalidation when files change
  • No separate daemon required

Despite OPcache's dominance, other options might fit specific scenarios:

// Example checking if APC is available in WordPress
if (function_exists('apc_store')) {
    apc_store('wp_cache_key', $expensive_data, 3600);
    $data = apc_fetch('wp_cache_key');
}

APCu becomes relevant when:

  • You need userland caching (object cache for WordPress)
  • Running older PHP versions without OPcache
  • Want integration with WordPress plugins like "APC Object Cache Backend"

With your limited RAM, proper sizing is critical:

; XCache configuration example balancing memory usage
xcache.size  = 64M
xcache.count = 1         # Single process for low-memory systems
xcache.slots = 8K        # Lower than default to conserve RAM
xcache.ttl   = 3600      # Shorter TTL to prevent cache bloat

Monitoring tools you should implement:

  • opcache_get_status() for OPcache memory usage
  • XCache admin panel for real-time statistics
  • APC cache info page (accessible via browser)

For BuddyPress/WP-MU installations, add these tweaks:

// In wp-config.php
define('WP_CACHE', true); // Enable object caching
define('WP_MEMORY_LIMIT', '128M'); // Adjust for multisite

// For OPcache specifically
if (extension_loaded('Zend OPcache')) {
    ini_set('opcache.revalidate_freq', '0'); // More aggressive caching
    ini_set('opcache.enable_file_override', '1'); // Better for plugins
}

For your Mediatemple VPS:

  1. Install OPcache: sudo apt-get install php-opcache
  2. Configure /etc/php/7.x/fpm/php.ini with above settings
  3. Restart PHP: sudo service php7.x-fpm restart
  4. Verify with php -i | grep opcache
  5. Install cache plugin like "OPcache Dashboard"

When dealing with PHP performance optimization for WordPress installations, opcode caching should be your first line of defense. PHP executes code by:

1. Lexing - Breaking code into tokens
2. Parsing - Creating Abstract Syntax Tree
3. Compilation - Generating opcodes
4. Execution - Running the opcodes

Opcode caches eliminate the need to repeat steps 1-3 on each request by storing precompiled script bytecode in shared memory.

For your VPS environment running WordPress (including MU and BuddyPress), these are your primary options:

  • OPcache (bundled with PHP 5.5+)
  • APC (Alternative PHP Cache, now legacy)
  • XCache (Lightweight alternative)

Beyond raw performance metrics, consider these factors:

Feature OPcache APC XCache
WordPress Compatibility Excellent Good (legacy) Good
Memory Management Sophisticated Basic Advanced
Configuration Simplicity Minimal Moderate Complex

For your 512MB RAM VPS running WordPress, this OPcache configuration offers optimal balance:

zend_extension=opcache.so
opcache.enable=1
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60
opcache.fast_shutdown=1
opcache.enable_cli=0

When dealing with multisite installations, pay attention to:

; Additional settings for multisite
opcache.validate_permission=1
opcache.validate_root=1

This prevents cache corruption when different sites modify the same underlying files.

Create a simple monitoring script (save as opcache_status.php):

<?php
$status = opcache_get_status();
echo 'Memory Usage: ' . round($status['memory_usage']['used_memory']/1024/1024,2) . 'MB';
echo ' of ' . round($status['memory_usage']['total_memory']/1024/1024,2) . 'MB';
?>

This helps track whether your allocated memory is sufficient.

If transitioning from another cache, use this compatibility check:

<?php
if (function_exists('opcache_reset')) {
    // OPcache available
} elseif (function_exists('apc_clear_cache')) {
    // APC available
} elseif (function_exists('xcache_clear_cache')) {
    // XCache available
}
?>