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

Ubuntu 16.04服务器:Apache启动后自动运行Chrome无头模式方法

Automatically Run Headless Chrome After Apache Starts on Ubuntu 16.04

Got it, let's figure out how to get google-chrome --headless --remote-debugging-port=9222 to launch automatically right after Apache finishes starting on your Ubuntu 16.04 server. Here are two solid methods you can use, with the first being the most reliable thanks to systemd's dependency management:

Systemd makes it straightforward to define service dependencies, so we can guarantee Chrome only starts once Apache is fully up and running.

  1. First, confirm Chrome's installation path
    Run this command to get the absolute path to Google Chrome:

    which google-chrome
    

    You'll likely get something like /usr/bin/google-chrome—note this down for later.

  2. Create a systemd service file
    Open a new service file with your preferred editor:

    sudo nano /etc/systemd/system/chrome-headless.service
    

    Paste the following content, replacing the ExecStart path if your Chrome location differs:

    [Unit]
    Description=Headless Google Chrome with remote debugging
    After=apache2.service
    Requires=apache2.service
    
    [Service]
    ExecStart=/usr/bin/google-chrome --headless --remote-debugging-port=9222
    Restart=on-failure
    User=www-data  # Optional: Run as Apache's user for better permission alignment
    # Uncomment below if you hit display-related issues
    # Environment=DISPLAY=:0
    
    [Install]
    WantedBy=multi-user.target
    
  3. Enable and test the service

    • Enable the service to start automatically on boot:
      sudo systemctl enable chrome-headless.service
      
    • Start it manually to test immediately:
      sudo systemctl start chrome-headless.service
      
    • Verify it's running properly:
      sudo systemctl status chrome-headless.service
      
  4. If you don't have Chrome installed yet
    Run these commands to set up the stable version:

    sudo apt update
    sudo apt install libxss1 libappindicator1 libindicator7 -y
    wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
    sudo dpkg -i google-chrome-stable_current_amd64.deb
    sudo apt -f install -y  # Fix any missing dependencies
    

Method 2: Use rc.local (Legacy but Simple)

Ubuntu 16.04 still includes rc.local, which runs scripts at the end of system boot. We'll add a check to wait for Apache before launching Chrome.

  1. Edit the rc.local file
    Open it with your editor:

    sudo nano /etc/rc.local
    

    Add this code before the exit 0 line:

    # Wait for Apache to fully start
    while ! systemctl is-active --quiet apache2.service; do
        sleep 2  # Check every 2 seconds
    done
    
    # Launch headless Chrome in the background
    /usr/bin/google-chrome --headless --remote-debugging-port=9222 &
    
  2. Ensure rc.local is executable
    Run this command to set proper permissions:

    sudo chmod +x /etc/rc.local
    
  3. Reboot to test
    Restart your server and verify Chrome launches after Apache is ready.

Quick Troubleshooting Tips

  • If Chrome fails to start, check systemd logs for errors with journalctl -u chrome-headless.service.
  • Some server environments might need xvfb (a virtual display) for headless Chrome. If you see display-related errors, install it with sudo apt install xvfb and modify the ExecStart command to xvfb-run /usr/bin/google-chrome --headless --remote-debugging-port=9222.

内容的提问来源于stack exchange,提问作者Charles Jeremy Colnet

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 07:50:05