serkanalgur / nginx-opcache-manager
WordPress plugin to manage and monitor Nginx cache and PHP Opcache with analytics
Package info
github.com/serkanalgur/nginx-opcache-manager
Type:wordpress-plugin
pkg:composer/serkanalgur/nginx-opcache-manager
Requires
- php: >=7.2
- composer/installers: ^1.0||^2.0
Requires (Dev)
- phpstan/phpstan: ^0.12
- phpunit/phpunit: ^9.6
- squizlabs/php_codesniffer: ^3.5
- wp-coding-standards/wpcs: ^2.3
README
A powerful WordPress plugin to monitor and manage Nginx cache and PHP Opcache directly from the WordPress Dashboard.
Features
- Real-time Monitoring: Display live statistics for both Nginx cache and PHP Opcache
- Nginx Cache Management:
- View cache size and number of cached files
- Clear cache with one click
- Configure cache path
- PHP Opcache Management:
- Monitor hit rate, memory usage, and cached scripts
- Reset opcache with one click
- Track miss rate and performance metrics
- Analytics Dashboard:
- Beautiful charts showing cache performance over time
- Historical data logging
- Memory usage trends
- Hit/Miss ratio visualization
- WordPress Dashboard Widget: Quick cache status overview on WordPress dashboard
- Settings Page: Easy configuration of plugin options
- Performance Metrics: Get insights into cache effectiveness
Requirements
- WordPress 4.7 or higher
- PHP 7.2 or higher
- Nginx web server (for cache management features)
- PHP Opcache extension (for opcache features)
Installation
- Upload the
nginx-opcache-managerfolder to the/wp-content/plugins/directory - Activate the plugin through the 'Plugins' menu in WordPress
- Navigate to 'Cache Manager' in the admin menu
- Configure the plugin settings if needed
Usage
Dashboard
The main dashboard shows:
- Nginx Cache Card: Current cache size, number of cached files, and cache path
- PHP Opcache Card: Hit rate, memory usage, and number of cached scripts
- Quick Actions: Clear cache and reset opcache buttons
Analytics Page
View detailed performance analytics:
- Cache size trends over time
- Opcache hit/miss ratio
- Memory usage visualization
- Historical data table with hourly records
Settings Page
Configure:
- Enable/disable Nginx cache monitoring
- Set Nginx cache path
- Enable/disable admin notifications
- View server information
AJAX Actions
The plugin provides several AJAX endpoints:
nom_get_stats
Get current cache statistics.
Request:
POST /wp-admin/admin-ajax.php
action: nom_get_stats
nonce: {nom_nonce}
Response:
{
"success": true,
"data": {
"nginx": {
"enabled": true,
"cache_size": 1048576,
"cached_files": 42,
"cache_path": "/var/run/nginx-cache"
},
"opcache": {
"enabled": true,
"hit_rate": 98.5,
"used_memory": 67108864,
"cached_scripts": 120
}
}
}
nom_clear_cache
Clear Nginx cache.
Request:
POST /wp-admin/admin-ajax.php
action: nom_clear_cache
nonce: {nom_nonce}
nom_reset_opcache
Reset PHP Opcache.
Request:
POST /wp-admin/admin-ajax.php
action: nom_reset_opcache
nonce: {nom_nonce}
Directory Structure
nginx-opcache-manager/
├── nginx-opcache-manager.php # Main plugin file
├── admin/
│ ├── class-admin.php # Admin menu and settings
│ ├── class-analytics.php # Analytics functionality
│ └── views/
│ ├── dashboard.php # Dashboard page template
│ ├── analytics.php # Analytics page template
│ ├── settings.php # Settings page template
│ └── dashboard-widget.php # Dashboard widget template
├── includes/
│ ├── class-nginx-cache-manager.php # Nginx cache management
│ ├── class-opcache-manager.php # PHP Opcache management
│ └── class-cache-stats.php # Statistics tracking
├── assets/
│ ├── css/
│ │ └── admin.css # Admin styles
│ └── js/
│ └── admin.js # Admin JavaScript
└── README.md
Classes Reference
Nginx_Opcache_Manager_Cache
Manages Nginx cache operations.
Methods:
is_enabled(): Check if nginx cache is enabledget_cache_size(): Get total cache size in bytesget_cached_files_count(): Get number of cached filesclear_cache(): Clear all nginx cacheclear_url_cache($url): Clear cache for specific URLget_cache_stats(): Get cache statisticsformat_bytes($bytes): Format bytes to human readable string
Nginx_Opcache_Manager_Opcache
Manages PHP Opcache operations.
Methods:
is_enabled(): Check if opcache is enabledget_status(): Get opcache statusget_config(): Get opcache configurationget_opcache_stats(): Get comprehensive opcache statisticsreset_opcache(): Reset opcacheinvalidate_file($file): Invalidate specific file cacheget_cached_files(): Get list of cached filesget_memory_percentage(): Get memory usage percentageformat_bytes($bytes): Format bytes to human readable string
Nginx_Opcache_Manager_Stats
Tracks and manages cache statistics.
Methods:
initialize(): Initialize statistics storageget_nginx_stats(): Get current nginx statisticsget_opcache_stats(): Get current opcache statisticsrecord_stats(): Record statistics to historyget_history($limit): Get statistics historyget_current_stats(): Get current statisticsget_dashboard_data(): Get data formatted for dashboardclear_history(): Clear all statistics history
Filter Hooks
None currently, but can be extended.
Action Hooks
nom_record_stats: Scheduled action to record statistics hourlywp_dashboard_setup: Used to register dashboard widget
Nginx Configuration
To ensure proper cache statistics collection, configure your Nginx server with a cache zone:
# Cache zone definition proxy_cache_path /var/run/nginx-cache levels=1:2 keys_zone=my_cache:10m max_size=1g inactive=60m; # Use in server block server { listen 80; server_name example.com; location ~ \.php$ { # PHP configuration proxy_cache my_cache; proxy_cache_valid 200 60m; } }
Update the "Nginx Cache Path" setting in the plugin to match your configuration.
Performance Considerations
- Statistics are recorded hourly to avoid excessive database usage
- Only the last 100 records are retained (approximately 24+ hours of data)
- Use read-only operations when monitoring to minimize server load
Troubleshooting
Nginx Cache Not Showing
- Verify the nginx cache path is correct in settings
- Check file permissions on the cache directory
- Ensure the PHP user has read access to the cache directory
Opcache Not Detected
- Verify opcache is installed:
php -m | grep opcache - Check opcache is enabled in php.ini:
opcache.enable = 1 - Verify opcache.enable_cli is not disabling it
No Statistics Data
- Clear the WordPress cache
- Wait for the hourly cron job to run
- Check WordPress error log for issues
Security
The plugin includes security measures:
- Nonce verification on all AJAX requests
- Capability checking (manage_options required)
- Sanitized and escaped output
- No privilege escalation
License
GPL v2 or later
Support
For issues and feature requests, please visit the plugin's support page.
Changelog
Version 1.1.0
- Added Cache Flush Activity Logging with persistent database storage
- Track all cache deletion operations with success/failure status
- Add post publication and modification tracking
- Display file paths for debugging and audit trails
- Added Customizable fastcgi_cache_key_schema setting
- Support for custom cache key formats
- Tag-based template system: $scheme, $request_method, $host, $request_uri, $query_string
- Added Clear Activity Logs button to manage database storage
- Fixed Fastcgi cache path calculation
- Corrected directory structure to use last 2 digits before final character
- Example:
/5/9f/beda56a8736ae8bc335cdd74983649f5(was/ed/a5/...)
- Fixed JavaScript type safety issue in escapeHtml() function
- Improved Memory optimization by reducing refresh interval from 5s to 30s
- Improved Activity log display with color-coded status indicators
- Added Debug logging for troubleshooting cache operations
Version 1.0.1
- Fixed JavaScript localization error where nomLocalize object was undefined
- Fixed Gauge charts not displaying in Live Performance Metrics section
- Fixed Canvas element sizing for proper Chart.js doughnut chart rendering
- Fixed Chart initialization requiring history data when none exists
- Improved Dashboard initialization to show charts immediately on first load
- Enhanced Confirmation dialogs for cache clearing and opcache reset operations
Version 1.0.0
- Initial release
- Nginx cache management
- PHP Opcache monitoring
- Analytics dashboard
- Statistics tracking