You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Mac OSX High Sierra中PHP 7.1.14的APCu无法正常工作求助

在Mac OSX High Sierra + PHP 7.1.14环境下APCu无法正常工作的排查方案

从你给出的信息来看,问题很典型:命令行检测到APCu配置但有加载警告,Web环境里phpinfo显示扩展启用,但实际调用apc_store时报函数未定义。下面是针对性的排查和解决步骤:

先梳理你的问题细节

1. 命令行检测的异常信号

执行php -i | grep apc时出现的警告是核心问题:

PHP Warning: Method apc_store() cannot be a NULL function in Unknown on line 0
PHP Warning: apc: Unable to register functions, unable to load in Unknown on line 0

这说明APCu扩展在加载阶段就出现了函数注册失败的问题,虽然后续显示了apcu的配置项,但扩展实际上并没有正常初始化。

2. 运行时致命错误

Web环境下的报错直接指向函数未定义:

[Fri May 25 18:43:30.523133 2018] [php7:error] [pid 1693] [client 10.146.5.127:54542] PHP Fatal error: Uncaught Error: Call to undefined function apc_store()

结合phpinfo显示启用的情况,大概率是Web服务器使用的PHP配置和命令行配置不一致,或者扩展在Web环境下根本没加载成功。

触发问题的代码片段

你的调用逻辑本身没有问题,只要APCu正常加载就能执行:

public static function add(LoggedInUser $loggedInUser) {
    if (!is_null($loggedInUser) && !is_null($loggedInUser->getUserData()) && !is_null($loggedInUser->getUserData()->userId)) {
        apc_store($loggedInUser->getUserData()->userId, $loggedInUser, 60);
    }
}

具体解决步骤

1. 重装适配PHP 7.1的APCu版本

PHP 7.1需要对应版本的APCu,建议卸载现有版本后安装适配的稳定版:

  • 如果用pecl安装:
    pecl uninstall apcu
    pecl install apcu-5.1.11  # 这个版本是专门适配PHP 7.1的稳定版本
    
  • 如果用Homebrew管理PHP:
    brew reinstall php@7.1 --with-apcu
    

2. 检查并统一PHP配置文件

Mac上常出现命令行PHP和Web服务器(Apache/Nginx)使用不同php.ini的情况:

  1. 执行php --ini找到命令行的配置文件路径
  2. 在Web环境的phpinfo页面中找到Loaded Configuration File路径
  3. 确保两个配置文件中都添加了正确的扩展加载指令,并且没有旧的APC扩展(apc.so)加载:
    ; 确保这一行存在且未被注释
    extension=apcu.so
    ; 如果有下面这行,注释掉(PHP7+已用APCu替代旧APC)
    ; extension=apc.so
    

3. 验证Web环境下的扩展加载情况

创建一个简单的测试脚本test_apcu.php,放在Web根目录访问:

<?php
echo "apc_store函数是否存在:" . var_export(function_exists('apc_store'), true) . "<br>";
echo "APCu扩展是否加载:" . var_export(extension_loaded('apcu'), true) . "<br>";
phpinfo();
?>

如果输出都是true,说明扩展加载正常;如果是false,则重点排查Web环境的PHP配置和扩展加载问题。

4. 修复扩展加载的权限问题

APCu需要读写/tmp目录,确保Web服务器的运行用户(比如_www)对/tmp有足够权限:

# 查看/tmp权限
ls -ld /tmp
# 正常权限应为drwxrwxrwt,若不是则修复
sudo chmod 1777 /tmp

5. 重启相关服务生效配置

修改配置后一定要重启服务:

  • 若使用Apache:
    sudo apachectl restart
    
  • 若使用PHP-FPM:
    sudo brew services restart php@7.1
    

内容的提问来源于stack exchange,提问作者Bhaskar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 06:51:09