寻求Ubuntu16.04下Nginx-WordPress站点宕机Gmail告警工具
Hey there! Let's skip the headache of building a tool from scratch—you don't need to mess with custom SMTP setups or DNS records for this. Below are two lightweight, straightforward solutions that'll alert you via Gmail the second any of your WordPress sites go down.
Option 1: Curl + Crontab + sSMTP (Ultra-Lightweight, No Extra Services)
This is perfect if you want something dead simple with zero overhead. It uses basic command-line tools to check site statuses on a schedule and fire off alerts when things go wrong.
Step 1: Install Required Tools
First, grab curl (for checking site status) and ssmtp (for sending emails via Gmail):
sudo apt-get update && sudo apt-get install curl ssmtp -y
Step 2: Configure sSMTP for Gmail
Edit the sSMTP config file to hook up your Gmail account:
sudo nano /etc/ssmtp/ssmtp.conf
Replace the contents with this (fill in your details):
root=your-gmail@gmail.com mailhub=smtp.gmail.com:587 AuthUser=your-gmail@gmail.com AuthPass=your-gmail-app-password # Important: Use an App Password if you have 2FA enabled UseTLS=YES UseSTARTTLS=YES FromLineOverride=YES
Then edit the revaliases file to map your Ubuntu user to your Gmail:
sudo nano /etc/ssmtp/revaliases
Add these lines (replace with your username and Gmail):
root:your-gmail@gmail.com:smtp.gmail.com:587 your-ubuntu-username:your-gmail@gmail.com:smtp.gmail.com:587
Step 3: Create the Site Check Script
Make a new script file (e.g., ~/check_wp_sites.sh) with this content:
#!/bin/bash # List all your WordPress site URLs here SITES=( "https://your-first-site.com" "https://your-second-site.com" # Add more sites as needed ) ALERT_EMAIL="your-alert-recipient@gmail.com" # Where you want alerts sent for SITE in "${SITES[@]}" do # Check if the site returns a 200 OK status (timeout after 10 seconds) HTTP_STATUS=$(curl -s -o /dev/null -w "%{http_code}" --max-time 10 "$SITE") if [ "$HTTP_STATUS" != "200" ]; then # Send alert email echo "Subject: ALERT - Site $SITE is Down (HTTP Status: $HTTP_STATUS)" | ssmtp "$ALERT_EMAIL" fi done
Step 4: Make the Script Executable
chmod +x ~/check_wp_sites.sh
Step 5: Schedule It with Crontab
Set the script to run every 10 minutes (adjust the interval as needed):
crontab -e
Add this line at the end (replace the path with your script's location):
*/10 * * * * /home/your-ubuntu-username/check_wp_sites.sh
Option 2: Monit (More Robust, Still Lightweight)
If you want a bit more control (like monitoring Nginx itself too), Monit is a tiny open-source monitoring tool that can handle site checks and email alerts out of the box.
Step 1: Install Monit
sudo apt-get update && sudo apt-get install monit -y
Step 2: Configure Monit for Gmail Alerts
Edit the main Monit config file:
sudo nano /etc/monit/monitrc
Find the set mailserver and set alert sections, uncomment them, and update with your Gmail details:
set mailserver smtp.gmail.com port 587 username "your-gmail@gmail.com" password "your-gmail-app-password" using tlsv12 with timeout 30 seconds set alert your-alert-recipient@gmail.com # Alert recipient email
Step 3: Add Site Monitoring Rules
Create a new config file for your WordPress sites:
sudo nano /etc/monit/conf.d/wp_sites.conf
Add a check for each site (repeat for all your sites):
check host site1 with address your-first-site.com if failed url https://your-first-site.com status = 200 then alert check host site2 with address your-second-site.com if failed url https://your-second-site.com status = 200 then alert
Step 4: Restart Monit
sudo service monit restart
You can check the status of your monitors anytime with:
monit status
Important Note About Gmail
If you have 2-Step Verification enabled on your Gmail account (which you should!), you can't use your regular password. Instead, create an App Password:
- Go to your Google Account > Security
- Under "Signing in to Google", select "App Passwords" (you'll need to verify your identity first)
- Select "Mail" as the app, "Other (custom name)" as the device, then generate the password
- Use this generated password in the
AuthPassfield of your sSMTP or Monit config
内容的提问来源于stack exchange,提问作者Arcticooling

