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

如何在AWS Lambda中为WordPress配置PHP运行时?解决依赖缺失错误

解决Lambda+Bref PHP层缺失libncurses.so.6的问题及无服务器WordPress部署方案

一、当前错误的直接解决办法

错误/opt/bin/php: error while loading shared libraries: libncurses.so.6: cannot open shared object file: No such file or directory是因为Bref的PHP层依赖的libncurses.so.6系统库不在Lambda默认的Amazon Linux 2运行时环境中,需补充包含该库的层:

  • 创建自定义系统依赖层:
    1. 启动一台Amazon Linux 2 EC2实例,安装ncurses库:sudo yum install ncurses-devel
    2. 复制/usr/lib64/libncurses.so.6及关联文件到本地目录的lib文件夹
    3. 将该目录打包为zip文件,上传为ap-south-1区域的Lambda层
    4. 在CloudFormation模板的Lambda函数Layers列表中,将此系统依赖层放在Bref PHP层之前

二、正确配置Bref PHP运行时的完整方案

避免依赖缺失,建议使用Bref专为Web应用设计的PHP层组合:

  1. 替换基础PHP层:
    将当前使用的arn:aws:lambda:ap-south-1:534081306603:layer:php-82:33替换为Web专用FPM层:arn:aws:lambda:ap-south-1:534081306603:layer:php-82-fpm:33(版本号需与Bref版本匹配)
  2. 补充必要层:
    Lambda函数的Layers按顺序配置:
    • 自定义系统依赖层(若仍有库缺失)
    • Bref PHP FPM层
    • Bref Runtime层(可选,ARN格式示例:arn:aws:lambda:ap-south-1:534081306603:layer:runtime:17)
  3. 调整Lambda核心配置:
    • Handler设置为public/index.php(适配WordPress入口)
    • 环境变量添加PHP_BINARY: /opt/bin/php-fpm,指定FPM运行时
    • API Gateway集成类型选择PROXY,确保请求正确转发

三、无服务器WordPress额外配置要点

  1. 数据存储适配:
    • 使用Amazon RDS/Aurora作为数据库,给Lambda配置RDS访问权限
    • 将wp-content/uploads等静态资源迁移到Amazon S3,通过Lambda或CloudFront访问
  2. CloudFormation模板示例调整:
    MyWordPressLambda:
      Type: AWS::Serverless::Function
      Properties:
        Handler: public/index.php
        Runtime: provided.al2
        Layers:
          - !Ref CustomNcursesLayer
          - arn:aws:lambda:ap-south-1:534081306603:layer:php-82-fpm:33
          - arn:aws:lambda:ap-south-1:534081306603:layer:runtime:17
        Environment:
          Variables:
            PHP_BINARY: /opt/bin/php-fpm
            DB_HOST: !Ref RDSHost
            DB_NAME: !Ref DBName
            DB_USER: !Ref DBUser
            DB_PASSWORD: !Ref DBPassword
            WP_HOME: !Sub "https://${ApiGatewayDomain}"
            WP_SITEURL: !Sub "https://${ApiGatewayDomain}"
        Policies:
          - AmazonRDSFullAccess
          - AmazonS3FullAccess
    
  3. WordPress代码适配:
    • 修改wp-config.php添加S3存储适配逻辑
    • 禁用本地文件写入权限,确保静态资源请求指向S3

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 12:42:36