Ubuntu 16.04服务器:Apache启动后自动运行Chrome无头模式方法
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:
Method 1: Use a Systemd Service (Recommended)
Systemd makes it straightforward to define service dependencies, so we can guarantee Chrome only starts once Apache is fully up and running.
First, confirm Chrome's installation path
Run this command to get the absolute path to Google Chrome:which google-chromeYou'll likely get something like
/usr/bin/google-chrome—note this down for later.Create a systemd service file
Open a new service file with your preferred editor:sudo nano /etc/systemd/system/chrome-headless.servicePaste the following content, replacing the
ExecStartpath 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.targetEnable 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
- Enable the service to start automatically on boot:
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.
Edit the rc.local file
Open it with your editor:sudo nano /etc/rc.localAdd this code before the
exit 0line:# 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 &Ensure rc.local is executable
Run this command to set proper permissions:sudo chmod +x /etc/rc.localReboot 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 withsudo apt install xvfband modify the ExecStart command toxvfb-run /usr/bin/google-chrome --headless --remote-debugging-port=9222.
内容的提问来源于stack exchange,提问作者Charles Jeremy Colnet

