Skip to content

Instantly share code, notes, and snippets.

@opi
Last active January 27, 2023 23:19
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save opi/2ebea267a8a0435a85a9ac4594e5afcc to your computer and use it in GitHub Desktop.
Save opi/2ebea267a8a0435a85a9ac4594e5afcc to your computer and use it in GitHub Desktop.
Install and configure MailHog (for Drupal) with a nice systemd unit and apache reverse proxy

MailHog (for Drupal) on Debian Stretch

MailHog is a nice mail testing tool for developers.

Website: https://github.com/mailhog/MailHog

Installation

Download the latest release on your local machine

wget https://github.com/mailhog/MailHog/releases/download/v1.0.0/MailHog_linux_amd64 -O /path/to/mailhog

chmod +x /path/to/mailhog

Note: Change /path/to/mailhog according to your needs

Configure PHP to send mail through Mailhog

In your php.ini configuration file (called php.ini), search for the string sendmail_path and replace it with the following:

sendmail_path = "/path/to/mailhog sendmail test@example.org"

Note: Change /path/to/mailhog according to your needs

PHP configuration file path may vary according to your configuration. Here is some common places:

  • /etc/php/7.0/apache2/php.ini
  • /etc/php/7.0/fpm/php.ini
  • /etc/php5/apache2/php.ini
  • /etc/php5/fpm/php.ini

Configure SystemD unit file

Place the following in a custom /etc/systemd/system/mailhog.service file:

[Unit]
Description=MailHog Email Catcher
After=syslog.target network.target

[Service]
Type=simple
ExecStart=/path/to/mailhog
StandardOutput=journal
Restart=on-failure

[Install]
WantedBy=multi-user.target

Note: Change /path/to/mailhog according to your needs

Now you can enable and run your mailhog daemon

systemctl enable mailhog.service
systemctl start mailhog.service

Configure Apache as a proxy

Once started, you can acces MailHog web interface from http://localhost:8025, but who remembers ports number ?

To access MailHog from a nice url, for example http://mailhog.local, we must configure Apache as a reverse proxy.

Some Apache modules are needed, so enable it:

a2enmod vhost_alias proxy proxy_http proxy_wstunnel

In a /etc/apache2/sites-available/mailhog.conf file, place the following configuration:

<VirtualHost 127.0.0.1:80>
    # Name for your virtualhost
    ServerName mailhog.local

    # Proxy config
    ProxyPreserveHost On
    ProxyRequests Off

    # Websocket proxy needs to be defined first
    ProxyPass "/api/v2/websocket" ws://localhost:8025/api/v2/websocket
    ProxyPassReverse "/api/v2/websocket" ws://localhost:8025/api/v2/websocket

    # General proxy
    ProxyPass / http://localhost:8025/
    ProxyPassReverse / http://localhost:8025/
</VirtualHost>

Now enable your vhost and reload Apache

a2ensite mailhog.conf && apache2ctl graceful

Add the following line in your /etc/hosts file:

127.0.0.1 mailhog.local

Sources

@geoffreyvanwyk
Copy link

It appears mod_vhost_alias is not necessary. After I disabled it, maihog still worked.

@uz-bueno
Copy link

@geoffreyvanwyk Thanks for putting this together. Do you know how to serve mailhog from a subpath?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment