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

Linux下Symfony/Panther脚本终端正常浏览器报错求助

问题描述

使用symfony/panther编写的PHP脚本在Linux终端运行正常,但通过XAMPP服务器在浏览器中访问时,抛出错误:Process unexpectedly closed with status 1。脚本代码如下:

<?php 

use Symfony\Component\Panther\Client;

require __DIR__.'/vendor/autoload.php'; // Composer's autoloader

ini_set('display_errors', 1); 
ini_set('display_startup_errors', 1); 
error_reporting(E_ALL);

$client = Client::createFirefoxClient('drivers/geckodriver',[
   '--proxy-server=http://50.201.51.216:8080',
   '--headless'
]);

$client->request('GET', 'https://example.com');

$html = $client->getInternalResponse()->getContent();

echo $html;

?>

完整错误日志:

Uncaught Facebook\WebDriver\Exception\UnknownErrorException: Process unexpectedly closed with status 1 in /opt/lampp/htdocs/testcrawler/vendor/php-webdriver/webdriver/lib/Exception/WebDriverException.php:146 
Stack trace: 
#0 /opt/lampp/htdocs/testcrawler/vendor/php-webdriver/webdriver/lib/Remote/HttpCommandExecutor.php(359): Facebook\WebDriver\Exception\WebDriverException::throwException('unknown error', 'Process unexpec...', Array) 
#1 /opt/lampp/htdocs/testcrawler/vendor/php-webdriver/webdriver/lib/Remote/RemoteWebDriver.php(129): Facebook\WebDriver\Remote\HttpCommandExecutor->execute(Object(Facebook\WebDriver\Remote\WebDriverCommand)) 
#2 /opt/lampp/htdocs/testcrawler/vendor/symfony/panther/src/ProcessManager/FirefoxManager.php(70): Facebook\WebDriver\Remote\RemoteWebDriver::create('http://127.0.0....', Object(Facebook\WebDriver\Remote\DesiredCapabilities), NULL, NULL) 
#3 /opt/lampp/htdocs/testcrawler/vendor/symfony/panther/src/Client.php(117): Symfony\Component\Panther\ProcessManager\FirefoxManager->start() 
#4 /opt/lampp/htdocs/testcrawler/vendor/symfony/panther/src/Client.php(521): Symfony\Component\Panther\Client->start() 
#5 /opt/lampp/htdocs/testcrawler/vendor/symfony/panther/src/Client.php(273): Symfony\Component\Panther\Client->get('https://exampl...') 
#6 /opt/lampp/htdocs/testcrawler/index.php(19): Symfony\Component\Panther\Client->request('GET', 'https://exampl...') 
#7 {main} thrown in /opt/lampp/htdocs/testcrawler/vendor/php-webdriver/webdriver/lib/Exception/WebDriverException.php on line 146

该脚本在Windows环境可正常运行,已尝试将Web服务器用户改为终端使用的用户并重启服务器,但错误依然存在。

解决方案
  • 检查Geckodriver的权限与路径

    • 避免使用相对路径,改用绝对路径确保XAMPP能正确找到驱动:
      $driverPath = realpath(__DIR__.'/drivers/geckodriver');
      $client = Client::createFirefoxClient($driverPath, [...]);
      
    • 给Geckodriver添加可执行权限:
      chmod +x /opt/lampp/htdocs/testcrawler/drivers/geckodriver
      
    • 确认Web服务器用户(如www-data)对驱动文件及上级目录拥有读和执行权限,可通过chown www-data:www-data /opt/lampp/htdocs/testcrawler/drivers/geckodriver调整归属。
  • 配置Firefox运行环境

    • 指定Firefox二进制文件的绝对路径,确保Web用户能访问到Firefox:
      $client = Client::createFirefoxClient(
          realpath(__DIR__.'/drivers/geckodriver'),
          [
              '--proxy-server=http://50.201.51.216:8080',
              '--headless'
          ],
          [],
          '/usr/bin/firefox' // 替换为你的Firefox实际安装路径
      );
      
    • 添加--no-sandbox参数绕过沙箱限制,Web服务器用户通常没有沙箱运行权限:
      $client = Client::createFirefoxClient(
          realpath(__DIR__.'/drivers/geckodriver'),
          [
              '--proxy-server=http://50.201.51.216:8080',
              '--headless',
              '--no-sandbox'
          ]
      );
      
  • 排查代理与网络连通性

    • 在XAMPP的shell中执行以下命令,测试Web服务器用户能否通过指定代理访问目标站点:
      sudo -u www-data curl -x http://50.201.51.216:8080 https://example.com
      
    • 若代理需要认证,需在Firefox启动参数中添加认证信息,或确保Web用户的环境变量包含代理认证配置。
  • 查看详细启动日志

    • 在脚本开头添加日志配置,获取Geckodriver的详细启动日志:
      putenv('MOZ_LOG=geckodriver:trace');
      putenv('MOZ_LOG_FILE='.__DIR__.'/geckodriver.log');
      
    • 执行脚本后查看geckodriver.log文件,根据日志中的具体错误信息定位问题。

内容的提问来源于stack exchange,提问作者D-user

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 03:12:04