【问题标题】:Error while executing node command with php exec function使用 php exec 函数执行节点命令时出错
【发布时间】:2025-11-28 15:05:04
【问题描述】:

我正在尝试在 Centos 服务器上从 php 执行 nodejs 文件。当从终端执行该命令时,它工作正常(在使用以下命令 su -s /bin/bash apache 切换到 apache 用户之后),但是当在浏览文件时执行代码时,它会引发以下异常:

["","","#","# 致命错误,第 0 行","# 检查失败:SetPermissions(protect_start,protect_size, PageAllocator::kReadExecute).","#", "#","#","#FailureMessage 对象:0x7fffd9b37760"]

  • 我也更改为绝对路径,但没有运气。
  • 为 index.js 文件授予 777 权限。
  • 从终端测试并正常工作:

php -r 'echo exec("/usr/bin/node /var/www/html/index.js /var/www/html/source_files/ 2>&1");'

 <?php
 try {
       exec("/usr/bin/node /var/www/html/index.js /var/www/html/source_files/ 2>&1", $out, $err);

        if ($err == 0) {
            return 1;
        } else {           
            return 0;
        }
    } catch (Exception $e) {
        error_log($e);
        return 0;
    }
?>

【问题讨论】:

    标签: php node.js apache centos


    【解决方案1】:

    如果您使用的是 selinux,请尝试暂时禁用它:

    setenforce 0
    

    尝试运行您的 node.js 脚本。如果它运行,您可能需要更改 selinux httpd 设置。执行以下命令列出 httpd 服务的所有 selinux 设置:

    /usr/sbin/getsebool -a | grep httpd
    

    您将看到两个相关设置httpd_ssi_exechttpd_execmem。 设置它们:

    setsebool -P httpd_ssi_exec=1
    setsebool -P httpd_execmem=1
    

    然后尝试再次启用 selinux 并运行您的 node.js 脚本以查看更改是否有帮助:

    setenforce 1
    

    【讨论】: