如何在 CentOS 上安装和启用备用 PHP 缓存 (APC)

在本教程中,我们将向您展示如何在 CentOS 上安装和启用备用 PHP 缓存 (APC)。 对于那些不知道的人,The Alternative PHP Cache (APC) 是一个免费、开放且强大的框架,用于缓存和优化 PHP 中间代码。 这是一个 PECL 扩展,与它的姊妹 PEAR 共享包装和分发系统。 在这篇文章中,我将展示如何启用 APC(替代 PHP 缓存)并使 PHP 变得更快。

本文假设您至少具备 Linux 的基本知识,知道如何使用 shell,最重要的是,您将网站托管在自己的 VPS 上。 安装非常简单,假设您在 root 帐户下运行,如果不是,您可能需要添加 ‘sudo‘ 到命令以获取 root 权限。 我将向您展示如何在 CentOS 系统上逐步安装 Alternative PHP Cache (APC)。

先决条件

  • 运行以下操作系统之一的服务器:CentOS Linux。
  • 建议您使用全新的操作系统安装来防止任何潜在问题。
  • 对服务器的 SSH 访问(或者如果您在桌面上,则只需打开终端)。
  • 一种 non-root sudo user或访问 root user. 我们建议充当 non-root sudo user,但是,如果您在充当 root 时不小心,可能会损害您的系统。

在 CentOS 上安装替代 PHP 缓存 (APC)

步骤 1. 安装依赖项。

yum install php-pear php-devel httpd-devel pcre-devel gcc make

步骤 2. 在 CentOS 上安装替代 PHP 缓存 (APC)。

pecl install apc

步骤 3. 配置 APC。

你可以把你的配置放在 php.ini 文件,但我更喜欢像上面那样有一个单独的文件进行配置。 下面提到的值用于演示目的,可以为 APC 设置不同的值,这取决于 PHP 页面的数量、服务器中的内存大小、页面点击次数等

#nano /etc/php.d/apc.ini  ; Enable the extension module extension = apc.so   ; Options for the APC module version >= 3.1.3 ; See https://www.php.net/manual/en/apc.configuration.php   ; This can be set to 0 to disable APC. apc.enabled=1 ; The number of shared memory segments to allocate for the compiler cache. apc.shm_segments=1 ; The size of each shared memory segment, with M/G suffixe apc.shm_size=512M ; A "hint" about the number of distinct source files that will be included or ; requested on your web server. Set to zero or omit if you're not sure; apc.num_files_hint=1024 ; Just like num_files_hint, a "hint" about the number of distinct user cache ; variables to store.  Set to zero or omit if you're not sure; apc.user_entries_hint=4096 ; The number of seconds a cache entry is allowed to idle in a slot in case this ; cache entry slot is needed by another entry. apc.ttl=7200 ; use the SAPI request start time for TTL apc.use_request_time=1 ; The number of seconds a user cache entry is allowed to idle in a slot in case ; this cache entry slot is needed by another entry. apc.user_ttl=7200 ; The number of seconds that a cache entry may remain on the garbage-collection list. apc.gc_ttl=3600 ; On by default, but can be set to off and used in conjunction with positive ; apc.filters so that files are only cached if matched by a positive filter. apc.cache_by_default=1 ; A comma-separated list of POSIX extended regular expressions. apc.filters ; The mktemp-style file_mask to pass to the mmap module apc.mmap_file_mask=/tmp/apc.XXXXXX ; This file_update_protection setting puts a delay on caching brand new files. apc.file_update_protection=2 ; Setting this enables APC for the CLI version of PHP (Mostly for testing and debugging). apc.enable_cli=0 ; Prevents large files from being cached apc.max_file_size=1M ; Whether to stat the main script file and the fullpath includes. apc.stat=1 ; Vertification with ctime will avoid problems caused by programs such as svn or rsync by making ; sure inodes havn't changed since the last stat. APC will normally only check mtime. apc.stat_ctime=0 ; Whether to canonicalize paths in stat=0 mode or fall back to stat behaviour apc.canonicalize=0 ; With write_lock enabled, only one process at a time will try to compile an ; uncached script while the other processes will run uncached apc.write_lock=1 ; Logs any scripts that were automatically excluded from being cached due to early/late binding issues. apc.report_autofilter=0 ;This setting is deprecated, and replaced with apc.write_lock, so let's set it to zero. apc.slam_defense=0

步骤 4. 启用 APC PHP 扩展

完成后,运行以下命令以启用 APC 扩展 Apache 配置。

echo "extension=apc.so" > /etc/php.d/apc.ini

第 4 步。重新启动 Apache.

现在我们重新开始 Apache 使用以下命令:

service httpd restart

APC 提供了一个 Web 界面,其中包含有关缓存的详细信息(内存使用情况、命中和未命中缓存条目)。 默认情况下,它不可访问,因此您需要复制文件 /usr/share/php/apc.php 到可以浏览的地方。 现在从浏览器,你可以去 https://your-domain.com/apc.php. 我宁愿等一天看看 APC 的性能,这样我们就可以清楚地了解我们的配置有多好。

恭喜! 您已成功安装 Alternative PHP Cache (APC)。 感谢您使用本教程在 CentOS 系统上安装 Alternative PHP Cache (APC)。 如需更多帮助或有用信息,我们建议您查看 PHP 官方网站.

Save

Save