Home > Web Front-end > JS Tutorial > Running Puppeteer on a Server: A Complete Tutorial

Running Puppeteer on a Server: A Complete Tutorial

Mary-Kate Olsen
Release: 2025-01-01 03:02:10
Original
840 people have browsed it

Running Puppeteer on a Server: A Complete Tutorial

Puppeteer is a Node.js library that provides a high-level API to control Chrome or Chromium browsers over the DevTools Protocol. It's a powerful tool for web scraping, automated testing, capturing screenshots etc. While using Puppeteer locally is straightforward, running it on a server requires additional considerations. This guide will walk you through the steps to get Puppeteer up and running on a server.


Preparing Server for Puppeteer

  • Update Server

This step is crucial for the successful execution of Puppeteer. Execute the following commands.

sudo apt update -y
sudo apt upgrade -y
Copy after login
  • Install Dependencies

Install the following dependencies to ensure Puppeteer runs smoothly.

sudo apt-get install libpangocairo-1.0-0 libx11-xcb1 libxcomposite1 libxcursor1 libxdamage1 libxi6 libxtst6 libnss3 libcups2 libxss1 libxrandr2 libatk1.0-0 libgtk-3-0 libasound2t64
Copy after login
  • Install Puppeteer

Execute the following command to install the latest version of Puppeteer, which is always recommended for optimal performance.

npm i puppeteer
Copy after login

Using Puppeteer

You can use the following code snippet to verify that Puppeteer is functioning correctly by invoking this function at your desired route.

const puppeteer = require("puppeteer");

/**
 * Launches a Puppeteer browser, navigates to a webpage, and then closes the browser.
 *
 * Launch Options:
 * - headless: Run the browser in headless mode (no GUI).
 * - args:
 *   - "--no-sandbox": Required if running as the root user.
 *   - "--disable-setuid-sandbox": Optional, try if you encounter sandbox errors.
 */

const runPuppeteer = async () => {
  try {
    // Launch a Puppeteer browser instance with custom arguments
    const browser = await puppeteer.launch({
      headless: true,
      args: [
        "--no-sandbox",
        "--disable-setuid-sandbox",
      ],
    });

    // Open a new page in the browser
    const page = await browser.newPage();

    // Navigate to the specified URL
    await page.goto("https://www.google.com");

    console.log("Navigation to Google completed.");

    // Close the browser
    await browser.close();
    console.log("Browser closed successfully.");
  } catch (error) {
    console.error("An error occurred:", error);
  }
};

// Execute the function
runPuppeteer();
Copy after login

Conclusion

Running Puppeteer on a server requires careful setup to handle dependencies, permissions, and resources. By following this guide, you can effectively deploy Puppeteer for tasks such as web scraping or automated testing in a server environment. For more advanced use cases, consider using tools like PM2 for process management and Docker for containerization.

Feel free to share this guide with others, and let us know in the comments if you encounter any issues after following the instructions.

The above is the detailed content of Running Puppeteer on a Server: A Complete Tutorial. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template