MailHog is an essential tool for intercepting emails sent by a development application. But I don't want to manage its startup and shutdown manually; it needs to be always available at system boot, especially in my dev environment. Here's how to make MailHog a proper service under Debian 12.
π¦ 1. Prerequisites
Check if you already have MailHog installed on your non-Dockerized Debian (Docker can also be a solution):
which mailhog |
If not, refer to my article:
PHP / WEB DEV β Sending Emails Without SMTP
π― 2. Objective
Launch MailHog automatically at each system boot without manual intervention, just like a good classic Debian service.
π€ 3. Step 1: Create a Dedicated User
Avoid running MailHog as root. Let's create a system user with no shell or home directory:
sudo useradd --system --no-create-home --shell /usr/sbin/nologin mailhog |
βοΈ 4. Step 2: Create the systemd Service File
Create a mailhog.service file in the dedicated services folder:
sudo nano /etc/systemd/system/mailhog.service |
Add the following content for our service:
[Unit] |
Description=MailHog service |
After=network.target |
|
[Service] |
|
# or your path, e.g. the one from `go install`: ~/go/bin/mailhog |
ExecStart=/usr/local/bin/mailhog |
Restart=always |
User=mailhog |
Group=mailhog |
Environment="MH_API_BIND_ADDR=0.0.0.0:8025" "MH_UI_BIND_ADDR=0.0.0.0:8025" "MH_SMTP_BIND_ADDR=0.0.0.0:1025" |
|
[Install] |
WantedBy=multi-user.target |
π‘ Here, we expose:
MailHog's web/API interface on port 8025 |
The SMTP server on port 1025 |
π 5. Step 3: Enable and Start the Service
Reload systemd and enable the service to start at boot:
sudo systemctl daemon-reexec |
sudo systemctl daemon-reload |
sudo systemctl enable mailhog |
sudo systemctl start mailhog |
β Step 4: Check That Itβs Working
A quick check to verify if our service is running:
systemctl status mailhog |
You should see an active status.
If needed, view the logs in real-time:
|
journalctl -u mailhog -f |
π Result
MailHog starts automatically at each reboot. You can access the web interface at:
Next, configure your application to send emails via localhost:1025. π‘οΈ Security Bonus
If needed, you can restrict access to the web interface if your server is exposed. MailHog does not include built-in authentication, so using ufw, a password-protected NGINX proxy, or a firewall is recommended.
Comments
No approved comments yet.
Sign in with a commenter account to post a comment. Sign in.