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

PHP WebDriver在EC2中通过API调用无法启动Chrome的问题求助

解决方案:Apache调用ChromeDriver停滞,出现僵死进程问题

针对你遇到的命令行执行正常,但Apache通过shell_exec调用时卡在ChromeDriver::start()、出现僵死Chrome进程的问题,以下是具体解决步骤:

1. 先捕获错误输出,定位根本问题

shell_exec默认只返回标准输出(stdout),错误信息(stderr)会被丢弃,这是你看不到报错的核心原因。修改调用命令,把错误输出重定向到标准输出:

$result = shell_exec("cd /path/to/root-of-site/; php index.php 2>&1");
echo $result;

执行后就能看到具体报错(比如Chrome二进制文件找不到、权限不足、临时目录无法写入等),这是排查的第一步。

2. 确保Apache用户拥有足够权限

  • Apache运行的用户(通常是apache或www-data)需要:
    • 拥有chromedriver和Chrome浏览器二进制文件的执行权限,可以用chmod +x /path/to/chromedriver /path/to/chrome设置,或者调整文件所属组为apache组:chgrp apache /path/to/chromedriver
    • 拥有临时目录读写权限,Headless Chrome需要向/tmp写入临时文件,执行chmod 777 /tmp(或更严格的权限,确保apache用户能读写)
  • 如果EC2实例是Amazon Linux/CentOS,检查SELinux设置:
    执行getsebool -a | grep httpd,确保httpd_execmem和httpd_can_network_connect为on,如果不是,执行:
    setsebool -P httpd_execmem on
    setsebool -P httpd_can_network_connect on
    

3. 补充必要的Chrome启动参数

在你的ChromeOptions中添加更多Headless模式的稳定参数,避免资源不足或进程异常:

$chromeOptions->addArguments(
    array_merge([
        '--headless',
        '--disable-gpu',
        '--no-sandbox',
        '--disable-dev-shm-usage', // 解决EC2 /dev/shm空间不足问题
        '--remote-debugging-port=9222',
        '--single-process', // 避免多进程僵死
        '--disable-extensions',
        '--disable-plugins',
        '--binary=/path/to/chrome', // 明确指定Chrome二进制文件路径,避免apache找不到
        'user-agent=' . $userAgent,
    ], $headers));

注意:确保$headers变量中的参数都是有效的Chrome启动参数,无效参数会导致Chrome启动失败。

4. 改用RemoteWebDriver连接独立的ChromeDriver服务

放弃ChromeDriver::start(),改为手动启动ChromeDriver服务并连接,这样更稳定且便于排查:

use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\Chrome\ChromeDriverService;

// 启动ChromeDriver服务
$service = ChromeDriverService::create(
    '/path/to/chromedriver',
    9515, // 指定端口
    [],
    null,
    ['--log-path=/tmp/chromedriver.log'] // 生成日志,方便排查
);
$service->start();

// 连接到服务
$driver = RemoteWebDriver::create($service->getUrl(), $desiredCapabilities);

// 后续操作不变
$driver->get('https://www.example.com');
sleep(5);
$currentURL = $driver->getCurrentURL();
echo $currentURL;

// 记得关闭服务
$driver->quit();
$service->stop();

这种方式可以通过/tmp/chromedriver.log查看ChromeDriver的详细日志,定位启动失败原因。

5. 调整Apache的环境变量

Apache的环境变量和命令行用户不同,可能找不到Chrome或相关依赖。可以在shell_exec中明确指定环境变量,比如:

$result = shell_exec("PATH=/usr/bin:/usr/local/bin cd /path/to/root-of-site/; php index.php 2>&1");

或者在Apache的配置文件(比如httpd.conf或php.ini)中添加环境变量,确保Chrome和chromedriver在PATH中。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.05 15:03:24