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

PHP远程调用ChromeDriver爬取网页报错:DevToolsActivePort文件不存在

远程调用Ubuntu VPS上ChromeDriver出现DevToolsActivePort不存在的解决方案

问题描述

我用PHP结合Facebook WebDriver做网页爬取,本地运行正常,但远程调用Ubuntu VPS上的ChromeDriver时,报错:Chrome failed to start: exited abnormally. (unknown error: DevToolsActivePort file doesn't exist)。已经确认VPS上Chromium/Chrome和ChromeDriver路径正确,执行chromedriver --port=9516 --whitelisted-ips=""能正常启动,代码部署在本地主机而非VPS,这是第一次用PHP的ChromeDriver,求详细解决办法。

报错信息

Fatal error: Uncaught Facebook\WebDriver\Exception\UnknownServerException: unknown error:
Chrome failed to start: exited abnormally. (unknown error: DevToolsActivePort file doesn't
exist) (The process started from chrome location /usr/bin/chromium-browser is no longer
running, so ChromeDriver is assuming that Chrome has crashed.) (Driver info:
chromedriver=114.0.5735.90 (386bc09e8f4f2e025eddae123f36f6263096ae49-refs/branch-
heads/5735@{#1052}),platform=Linux 5.15.0-75-generic x86_64) in
/home/k/user/user.domain/public_html/app/Http/Controllers/getName/vendor/facebook/webdriver/
lib/Exception/WebDriverException.php:121 Stack trace: #0
/home/kuser/user.domain/public_html/app/Http/Controllers/getName/vendor/facebook/webdriver/l
ib/Remote/HttpCommandExecutor.php(353):
Facebook\WebDriver\Exception\WebDriverException::throwException(13, 'unknown error: ...',
Array) #1
/home/k/user/user.domain/public_html/app/Http/Controllers/getName/vendor/facebook/webdriver/
lib/Remote/RemoteWebDriver.php(100): Facebook\WebDriver\Remote\HttpCommandExecutor-

execute(Object(Facebook\WebDriver\Remote\WebDriverCommand)) #2
/home/k/user/user.domain/public_html/app/Http/Controllers/getName/index.php(22):
Facebook\WebDriver\Remote\RemoteWebDriver::create('http://', Array, 60000,
'/usr/bin/chrome...') #3 {main} thrown in
/home/k/user/user.domain/public_html/app/Http/Controllers/getName/vendor/facebook/webdriver/
lib/Exception/WebDriverException.php on line 121

当前代码

<?php
    ini_set('display_errors', 1);
    require_once 'vendor/autoload.php';
    
    use Facebook\WebDriver\Remote\{DesiredCapabilities, RemoteWebDriver};
    use Facebook\WebDriver\WebDriverBy;
    use Facebook\WebDriver\WebDriverExpectedCondition;
    use Facebook\WebDriver\Exception\NoSuchElementException;
    use Facebook\WebDriver\Exception\TimeOutException;
    
    // Set up ChromeDriver options
    $options = new \Facebook\WebDriver\Chrome\ChromeOptions();
    $options->setBinary('/usr/bin/chromium-browser'); // Update with the correct path
    
    $options->addArguments(['--headless']); // Run ChromeDriver in headless mode (optional)
    
    // Start ChromeDriver with the desired capabilities
    $capabilities = DesiredCapabilities::chrome();
    $capabilities->setCapability(\Facebook\WebDriver\Chrome\ChromeOptions::CAPABILITY, $options);
    
    // Create a new instance of RemoteWebDriver with increased timeout
    $driver = RemoteWebDriver::create('http://<vps-ip>:9516/', $capabilities, 60000, '/usr/bin/chromedriver');
    
    // Navigate to the desired website
    $driver->get('<url-for-parsing>');
    
    try {
        // Wait until the element is visible
        $wait = new \Facebook\WebDriver\WebDriverWait($driver, 30);
        $element = $wait->until(
            WebDriverExpectedCondition::visibilityOfElementLocated(WebDriverBy::xpath('//div[contains(@data-testid, "profile-snippet-name")]'))
        );
        
        // Extract the text content of the div element
        $divText = $element->getText();
        
        // Print the extracted text
        echo $divText;
    } catch (NoSuchElementException $e) {
        echo "Element not found: " . $e->getMessage();
    } catch (TimeOutException $e) {
        echo "Timed out waiting for element: " . $e->getMessage();
    } finally {
        // Quit the driver
        $driver->quit();
    }
?>

ChromeDriver启动输出

root@**:~# chromedriver --port=9516 --whitelisted-ips=""
Starting ChromeDriver 114.0.5735.90 (386bc09e8f4f2e025eddae123f36f6263096ae49-refs/branch-heads/5735@{#1052}) on port 9516
All remote connections are allowed. Use an allowlist instead!
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.


解决方案

1. 补充Chrome无头模式必要参数

VPS无图形界面环境下,仅--headless参数不足以让Chrome正常启动,需补充以下参数:

$options->addArguments([
    '--headless=new', // 新版无头模式,兼容性更强
    '--no-sandbox', // 禁用沙箱,非图形环境必填
    '--disable-dev-shm-usage', // 避免/dev/shm空间不足导致崩溃
    '--remote-debugging-port=9222', // 指定调试端口,解决DevToolsActivePort缺失问题
    '--disable-gpu', // 禁用GPU加速,无头模式无需
    '--window-size=1920,1080' // 固定窗口大小,避免页面布局异常
]);

2. 修正RemoteWebDriver创建参数

代码中RemoteWebDriver::create的第四个参数是本地ChromeDriver路径,这是错误的——ChromeDriver运行在VPS上,本地代码无需指定该路径,直接移除:

$driver = RemoteWebDriver::create('http://<vps-ip>:9516/', $capabilities, 60000);

3. 确认Chrome与ChromeDriver版本完全匹配

主版本号必须一致(如均为114.x),执行以下命令验证:

chromium-browser --version
chromedriver --version

若版本不匹配,下载对应版本的ChromeDriver替换现有文件。

4. 检查VPS用户权限

Chrome默认限制root用户运行,可通过两种方式解决:

  • 保留--no-sandbox参数(已在第一步添加)
  • 创建普通用户运行ChromeDriver:
useradd -m chromedriver-user
su - chromedriver-user -c "chromedriver --port=9516 --whitelisted-ips=''"

5. 验证VPS端口连通性

确保本地主机能访问VPS的9516端口:

telnet <vps-ip> 9516
# 或
curl http://<vps-ip>:9516/status

若无法连通,开放VPS防火墙端口:

ufw allow 9516/tcp

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.17 16:37:01