CentOS 7下PHP 7.0的Opcache浏览器端启用问题求助
Hey there, let's get this Opcache issue sorted out—no DevOps expertise required, we'll take it slow and steady! The root of your problem is that your CLI PHP (7.1) and web server PHP (7.0) are totally separate installations with their own config files, so the Opcache setup you did for CLI doesn't apply to the browser-facing PHP. Here's how to fix it:
Step 1: Locate PHP 7.0's Configuration Files
First, we need to find where PHP 7.0 (the one Nginx uses, probably via PHP-FPM) stores its configs:
- Run this command to confirm PHP 7.0-FPM's version and config path:
Look for lines likephp-fpm7.0 -tConfiguration file: /etc/php-fpm.confandLoaded configuration file: /etc/php.ini(this will be the PHP 7.0-specific php.ini). - If you can't find it that way, search for PHP 7.0's php.ini directly:
find /etc -name "php.ini" | grep 7.0
Step 2: Enable Opcache in PHP 7.0's Config
Open the PHP 7.0 php.ini file (or the dedicated Opcache config, often in /etc/php.d/10-opcache.ini for CentOS) with a text editor like nano:
nano /etc/php.ini
Add or update these lines to enable Opcache (adjust values if needed, but these are safe defaults):
# Load the Opcache extension (make sure the path matches your system!) zend_extension=opcache.so # Enable Opcache for the web server opcache.enable=1 # Keep this disabled for CLI (since you already set up 7.1 CLI separately) opcache.enable_cli=0 # Basic performance settings opcache.memory_consumption=128 opcache.max_accelerated_files=10000 opcache.revalidate_freq=2
Pro tip: If you're not sure where
opcache.sois for PHP 7.0, run this command to find it:find /usr/lib64/php/modules -name opcache.so | grep 7.0Replace the path in
zend_extensionwith the result.
Step 3: Restart Services to Apply Changes
After saving the config, restart PHP-FPM 7.0 and Nginx to make the changes take effect:
systemctl restart php-fpm7.0 systemctl restart nginx
Step 4: Verify Opcache is Working
To confirm Opcache is enabled for the web server:
- Create a file named
phpinfo.phpin your Nginx website's root directory (usually/usr/share/nginx/html/) with this content:<?php phpinfo(); ?> - Access this file via your browser (e.g.,
http://your-vps-ip/phpinfo.php). - Use your browser's search function to look for Zend OPcache—you should see
opcache.enableset toOn.
That's it! The key here was realizing that CLI and web PHP are separate entities with their own configs. Let me know if you hit any snags along the way.
内容的提问来源于stack exchange,提问作者Joe Remix

