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

Arm架构Ubuntu20.04+PHP8.2下Laravel Browsershot与Puppeteer适配方案

在AWS EC2 Arm架构Ubuntu 20.04上配置Laravel 9 Browsershot的解决方案

我在AWS EC2的Arm架构Ubuntu 20.04任务队列服务器(运行PHP 8.2)中配置Laravel 9 Browsershot时遇到了核心问题:无法安装Puppeteer自带的local-chromium,最终通过Snap发行版的Chromium解决了问题。

Snap的限制在于它只能在普通用户的/home目录下运行,但默认的www-data用户并没有这样的配置。因此需要将www-data转换为完整用户,并让Nginx从/home/www-data而非/var/www提供服务。

注意:假设您已安装PHP 8.2且Laravel项目已配置Browsershot。

  1. 升级系统并安装Node.js
sudo apt-get -y upgrade
sudo curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
sudo apt-get install -y nodejs gconf-service libasound2 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgbm1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 ca-certificates fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils wget libgbm-dev libxshmfence-dev
  1. 配置真实的www-data用户
sudo mkdir /home/www-data
sudo usermod -d /home/www-data www-data -s /bin/bash
  1. 安装Snap版Chromium
sudo apt-get -y install chromium-browser
  1. 创建Browsershot与Chromium所需的临时写入目录
sudo mkdir /home/www-data/browsershot-html
sudo mkdir /home/www-data/user-data
sudo mkdir /home/www-data/user-data/Default
sudo mkdir /home/www-data/user-data/Default/Cache
sudo chown -R www-data:www-data /home/www-data
  1. 将项目部署到/home/www-data/your-laravel-project,并配置Nginx从此目录提供服务

  2. 安装Puppeteer

cd /home/www-data/your-laravel-project
sudo npm install --location=global --unsafe-perm puppeteer --ignore-scripts
  1. 在Laravel项目中创建测试控制台脚本
    创建app/Console/Commands/TestBrowsershot.php,内容如下:
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Spatie\Browsershot\Browsershot;

class TestBrowsershot extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'test:browsershot';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Test browsershot';

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        Browsershot::html('<h1>Hello world</h1>')
        ->setOption('args', ['--disable-web-security'])
        ->ignoreHttpsErrors()
        ->noSandbox()
        ->showBackground()
        ->setOption('scale', 0.9)
        ->emulateMedia('print')
        ->setNodeBinary('/usr/bin/node')
        ->setNpmBinary('/usr/bin/npm')
        ->setChromePath('chromium-browser')
        ->setCustomTempPath('/home/www-data/browsershot-html')
        ->addChromiumArguments([
            'lang' => "en-US,en;q=0.9",
            'hide-scrollbars',
            'enable-font-antialiasing',
            'force-device-scale-factor' => 1,
            'font-render-hinting' => 'none',
            'user-data-dir' => '/home/www-data/user-data',
            'disk-cache-dir' => '/home/www-data/user-data/Default/Cache',
        ])
        ->save('hello.pdf');
    }
}

注意:如果需要访问外部站点,上述配置中的noSandbox()和--disable-web-security会带来安全风险。本文仅针对自定义HTML内容的场景,因此无影响。若需抓取外部站点,建议使用隔离沙箱环境,或移除这些不安全配置。

  1. 以www-data用户身份测试(Nginx运行身份)
su www-data
cd /home/www-data/your-laravel-project
php artisan test:browsershot

祝您好运!


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.03 13:01:05