How to Check PHP OpCache Status on A Linux Server Make it Rain Paper

  • OpCache, PHP
  • 0

 

Need to check your OpCache status? We intend to guide you through three proven methods, from quick command-line checks to detailed visual monitoring. This guide covers both server-wide and website-specific approaches.

Understanding Key Terms:

  • OpCache: A PHP caching system that stores precompiled script bytecode in memory, significantly improving PHP performance.
  • CLI: Command Line Interface, a text-based way to interact with your server.
  • phpinfo(): A PHP function that displays comprehensive information about your PHP configuration.

 

Before proceeding with OpCache configuration:

  • Test these steps in a non-production environment first
  • Backup your current OpCache configuration
  • Check OpCache's official documentation for the most up-to-date instructions
  • Ensure you have selected the appropriate maintenance window

 

Quick Method Overview

  • Method 1: Fast CLI checks - perfect for quick status verification
  • Method 2: Detailed configuration review - ideal for debugging
  • Method 3: Visual interface - best for ongoing monitoring

Method 1: Using PHP CLI Commands

Server-wide Check


# Check if OpCache is enabled in PHP configuration
php -i | grep opcache

# Alternative using php -m
php -m | grep -i opcache

# Check OpCache configuration file
php --ini | grep opcache
        

Website-specific Check

Create a file named opcache-check.php:


<?php
if (function_exists('opcache_get_status')) {
    $status = opcache_get_status(false);
    echo "OpCache Status:\n";
    print_r($status);
} else {
    echo "OpCache is not available\n";
}
        

Run it using:

php opcache-check.php

Method 2: Using phpinfo()

Server Check

Create a file named check-opcache.php:


<?php
phpinfo(INFO_MODULES);
?>
        

Access via CLI:

php check-opcache.php | grep -i opcache

Website Check

Create a secure, password-protected file named opcache-info.php:


<?php
if (!isset($_SERVER['PHP_AUTH_USER']) || $_SERVER['PHP_AUTH_USER'] !== 'admin' || $_SERVER['PHP_AUTH_PW'] !== '$super_secure_password') {
    header('WWW-Authenticate: Basic realm="OpCache Status"');
    header('HTTP/1.0 401 Unauthorized');
    exit('Unauthorized');
}

if (function_exists('opcache_get_configuration')) {
    echo "<h2>OpCache Configuration</h2>";
    print_r(opcache_get_configuration());
    
    echo "<h2>OpCache Status</h2>";
    print_r(opcache_get_status());
} else {
    echo "OpCache is not available";
}
?>
        

Method 3: Using OpCache GUI Tools

For a more user-friendly approach, you can use opcache-gui, a clean and responsive interface that shows statistics, settings, and cached files in real-time.

Server-wide Installation


# Install via Composer
composer require amnuts/opcache-gui

# Or clone directly
git clone https://github.com/amnuts/opcache-gui.git /var/www/opcache-gui
        

Website Implementation

Create a secure file named opcache-status.php:


<?php
require_once 'vendor/autoload.php';

// Basic authentication
if (!isset($_SERVER['PHP_AUTH_USER']) || $_SERVER['PHP_AUTH_USER'] !== 'admin' || $_SERVER['PHP_AUTH_PW'] !== 'your_secure_password') {
    header('WWW-Authenticate: Basic realm="OpCache Status"');
    header('HTTP/1.0 401 Unauthorized');
    exit('Unauthorized');
}

// Initialize OpCache GUI
$opcache = new Opcache\OpCacheGUI();
echo $opcache->getGUI();
?>
        

Security Best Practices

  • Always protect OpCache status pages with strong authentication
  • Store status files outside the web root when possible
  • Implement IP restrictions for added security

Apache Configuration Example


# Apache configuration for secure access
<Files "opcache-*.php">
    Require ip 127.0.0.1
    # Or specific IPs
    Require ip 192.168.1.0/24
</Files>
        

Troubleshooting Common Issues

If OpCache Appears Disabled


# Check PHP version
php -v

# Verify OpCache installation
php -m | grep -i opcache

# Check PHP configuration directory
php --ini
        

If OpCache Is Enabled But Not Working


# Check permissions
ls -l /etc/php/*/mods-available/opcache.ini

# Verify configuration loading
php --ini | grep -i loaded
        

For Memory Issues


# Check current memory usage
echo '<?php print_r(opcache_get_status());' | php
        

Did this answer help?

« Back