在 CentOS 系統中,清除 OPcache(Optimizing PHP Cache)可以通過以下幾種方法:
命令行清除:
使用以下命令可以清除整個 OPcache:
echo > /var/run/opcache/opcache.stat
sudo systemctl restart httpd
或者,如果你使用的是 PHP-FPM,重啟 PHP-FPM 服務:
sudo systemctl restart php-fpm
通過 PHP 腳本清除:
創建一個名為 clear_opcache.php 的文件,將以下內容粘貼到文件中:
<?php
if (function_exists('opcache_reset')) {
opcache_reset();
echo "OPcache has been reset.";
} else {
echo "OPcache is not enabled.";
}
?>
然后,通過瀏覽器訪問這個文件(例如:http://your_server_ip/clear_opcache.php),或者使用 curl 命令:
curl http://your_server_ip/clear_opcache.php
修改 PHP 配置文件:
找到你的 PHP 配置文件(php.ini),找到或添加以下行:
zend_extension=opcache.so
opcache.enable=1
opcache.validate_timestamps=0
opcache.revalidate_freq=0
opcache.fast_shutdown=1
其中,opcache.revalidate_freq 的值決定了 OPcache 何時重新驗證 PHP 腳本。將其設置為 0 可以確保每次請求都重新驗證腳本。
修改完配置文件后,重啟 PHP-FPM 或 Apache 服務:
sudo systemctl restart php-fpm
或者
sudo systemctl restart httpd
請注意,頻繁清除 OPcache 可能會影響性能,因此在需要清除緩存時謹慎操作。