如何以 root 身份 运行 Ubuntu 服务器上的节点 Puppeteer 应用程序

How to run a node Puppeteer app on Ubuntu Server as root

我正在尝试 运行 使用 npm 包 Puppeteer 的节点应用程序来抓取网站,使用 VPS,使用 root 作为我登录的默认用户。

从来没有用过。我总是不得不花半个小时谷歌搜索来找到解决方案……好吧,不再多了。

这样做:

0。 运行sudo apt update

1.安装 libx 东西

sudo apt-get install libpangocairo-1.0-0 libx11-xcb1 libxcomposite1 libxcursor1 libxdamage1 libxi6 libxtst6 libnss3 libcups2 libxss1 libxrandr2 libgconf2-4 libasound2 libatk1.0-0 libgtk-3-0

2。在 Puppeteer 应用程序的 launch() 部分设置 --no-sandbox 参数(仅当您 运行 作为 root/root 的 cron 时才需要)

const browser = await puppeteer.launch({headless: true, args: ['--no-sandbox']})

E:正如 Niko 指出的那样,--no-sandbox 会使您的服务器受到来自爬虫访问的网站的恶意攻击。看起来其他地方有一些 SO 问题 post 实施起来并不糟糕的替代方案。我喜欢大卫的回答 ,这似乎表明即使在 2021 年 --no-sandbox 在某些情况下仍然是必要的邪恶。

E2:请参阅下面的答案。看来 2021 年我们还必须安装 libgbm-dev

通过 SSH 连接到服务器

  1. 打开终端
  2. 类型:ssh root@[服务器 ip]
  3. 如果这是您第一次连接到服务器,您将收到安全提示。输入 'yes'
  4. 填写您的密码:[密码]
  5. 您现在应该已连接到您的服务器

安装应用

sudo apt-get 更新

sudo apt-get install -y libgbm-dev

sudo apt install -y gconf-service libasound2 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 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 证书字体解放 libappindicator1 libbnsreleases3 ls[g =11=]

sudo apt install curl

curl-sLhttps://deb.nodesource.com/setup_14.x | sudo -E bash -

sudo apt 安装 nodejs

git config --global user.name “你的名字”

git config --global user.email “你的邮箱”

git克隆“你的项目giturl”

cd "你的项目文件夹"

npm i

npm 启动


如果您需要在一台 VPS 服务器上 运行 多个应用程序 - google 工具“屏幕”

安装 libgbm 后,一切正常! 希望对你有所帮助

    sudo apt-get update
    sudo apt-get install -y libgbm-dev