OPcache 是一种流行的开源 PHP 扩展,有助于加快 PHP 脚本的性能。它将 PHP 脚本的编译版本缓存在内存中,从而减少了每次执行这些脚本时加载和编译这些脚本的开销。这可以显著提高性能,尤其是在繁忙的网站上。
在 Ubuntu 22.04 LTS Jammy Jellyfish 上安装 PHP OPcache
第 1 步。首先,通过在终端中运行以下命令,确保所有系统软件包都是最新的。apt
<span class="pln">sudo apt update sudo apt upgrade sudo apt install wget apt</span><span class="pun">-</span><span class="pln">transport</span><span class="pun">-</span><span class="pln">https gnupg2 software</span><span class="pun">-</span><span class="pln">properties</span><span class="pun">-</span><span class="pln">common</span>
第 2 步。安装 PHP。
默认情况下,PHP 在 Ubuntu 22.04 基础存储库中不可用。现在运行以下命令以安装较新版本的 PHP 8.2,您必须将此 PPA 添加到您的 Ubuntu 系统中:
<span class="pln">sudo apt install software</span><span class="pun">-</span><span class="pln">properties</span><span class="pun">-</span><span class="pln">common sudo </span><span class="kwd">add</span><span class="pun">-</span><span class="pln">apt</span><span class="pun">-</span><span class="pln">repository ppa</span><span class="pun">:</span><span class="pln">ondrej</span><span class="pun">/</span><span class="pln">php sudo apt update</span>
成功添加 PPA 存储库后,更新软件包列表,现在您可以在 Ubuntu 8.2 上安装 PHP 22.04:
<span class="pln">sudo apt install php8</span><span class="pun">.</span><span class="lit">2</span>
要检查系统上当前安装的PHP版本,请运行以下命令:
<span class="pln">php </span><span class="pun">-</span><span class="pln">v</span>
输出:
<span class="pun">[</span><span class="pln">root@idroot</span><span class="pun">.</span><span class="pln">us</span><span class="pun">]#</span><span class="pln"> php </span><span class="pun">-</span><span class="pln">v PHP </span><span class="lit">8.2</span><span class="pun">.</span><span class="lit">2</span> <span class="pun">(</span><span class="pln">cli</span><span class="pun">)</span> <span class="pun">(</span><span class="pln">built</span><span class="pun">:</span> <span class="typ">Jan</span> <span class="lit">31</span> <span class="lit">2023</span> <span class="lit">13</span><span class="pun">:</span><span class="lit">31</span><span class="pun">:</span><span class="lit">55</span><span class="pun">)</span> <span class="pun">(</span><span class="pln">NTS gcc x86_64</span><span class="pun">)</span> <span class="typ">Copyright</span> <span class="pun">(</span><span class="pln">c</span><span class="pun">)</span> <span class="typ">The</span><span class="pln"> PHP </span><span class="typ">Group</span> <span class="typ">Zend</span> <span class="typ">Engine</span><span class="pln"> v4</span><span class="pun">.</span><span class="lit">2.2</span><span class="pun">,</span> <span class="typ">Copyright</span> <span class="pun">(</span><span class="pln">c</span><span class="pun">)</span> <span class="typ">Zend</span> <span class="typ">Technologies</span>
有关安装 PHP 的其他资源,请阅读下面的帖子:
- 如何在 Ubuntu Linux √ 上安装 PHP
第 3 步。在 Ubuntu 22.04 上安装 PHP OPcache。
现在运行以下命令以安装 OPcache 扩展:
<span class="pln">sudo apt install php8</span><span class="pun">.</span><span class="lit">2</span><span class="pun">-</span><span class="pln">opcache</span>
在 Ubuntu 22.04 LTS 上安装 OPcache 后,您需要配置 PHP 才能使用该扩展。为此,请按照下列步骤操作:
<span class="pln">nano </span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">php</span><span class="pun">/</span><span class="lit">8.2</span><span class="pun">/</span><span class="pln">apache2</span><span class="pun">/</span><span class="pln">php</span><span class="pun">.</span><span class="pln">ini</span>
您需要使用以下建议设置更新文件:php.ini
<span class="pln">opcache</span><span class="pun">.</span><span class="pln">memory_consumption</span><span class="pun">=</span><span class="lit">128</span><span class="pln"> opcache</span><span class="pun">.</span><span class="pln">interned_strings_buffer</span><span class="pun">=</span><span class="lit">8</span><span class="pln"> opcache</span><span class="pun">.</span><span class="pln">max_accelerated_files</span><span class="pun">=</span><span class="lit">4000</span><span class="pln"> opcache</span><span class="pun">.</span><span class="pln">revalidate_freq</span><span class="pun">=</span><span class="lit">60</span><span class="pln"> opcache</span><span class="pun">.</span><span class="pln">fast_shutdown</span><span class="pun">=</span><span class="lit">1</span><span class="pln"> opcache</span><span class="pun">.</span><span class="pln">enable_cli</span><span class="pun">=</span><span class="lit">1</span>
修改PHP设置后,您需要重新启动Apache以使更改生效:
<span class="pln">sudo systemctl restart apache2</span>
最后,使用以下命令检查 PHP 的版本:
<span class="pln">php </span><span class="pun">-</span><span class="pln">v</span>
输出:
<span class="pun">[</span><span class="pln">root@idroot</span><span class="pun">.</span><span class="pln">us</span><span class="pun">]#</span><span class="pln"> php </span><span class="pun">-</span><span class="pln">v PHP </span><span class="lit">8.2</span><span class="pun">.</span><span class="lit">2</span> <span class="pun">(</span><span class="pln">cli</span><span class="pun">)</span> <span class="pun">(</span><span class="pln">built</span><span class="pun">:</span> <span class="typ">Jan</span> <span class="lit">31</span> <span class="lit">2023</span> <span class="lit">13</span><span class="pun">:</span><span class="lit">31</span><span class="pun">:</span><span class="lit">55</span><span class="pun">)</span> <span class="pun">(</span><span class="pln">NTS gcc x86_64</span><span class="pun">)</span> <span class="typ">Copyright</span> <span class="pun">(</span><span class="pln">c</span><span class="pun">)</span> <span class="typ">The</span><span class="pln"> PHP </span><span class="typ">Group</span> <span class="typ">Zend</span> <span class="typ">Engine</span><span class="pln"> v4</span><span class="pun">.</span><span class="lit">2.2</span><span class="pun">,</span> <span class="typ">Copyright</span> <span class="pun">(</span><span class="pln">c</span><span class="pun">)</span> <span class="typ">Zend</span> <span class="typ">Technologies</span><strong><span class="typ">with</span> <span class="typ">Zend</span> <span class="typ">OPcache</span><span class="pln"> v8</span><span class="pun">.</span><span class="lit">2.2</span><span class="pun">,</span> <span class="typ">Copyright</span> <span class="pun">(</span><span class="pln">c</span><span class="pun">),</span> <span class="kwd">by</span> <span class="typ">Zend</span> <span class="typ">Technologies</span></strong>
感谢您使用本教程在 Ubuntu 22.04 LTS Jammy Jellyfish 系统上安装 PHP OPcache。有关其他帮助或有用信息,我们建议您查看官方 OPcache 网站。