Skip to content

Instantly share code, notes, and snippets.



Last active Mar 30, 2021
What would you like to do?
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.



Download the latest release on your local machine

wget -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"

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:

Description=MailHog Email Catcher



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:

    # 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/

Now enable your vhost and reload Apache

a2ensite mailhog.conf && apache2ctl graceful

Add the following line in your /etc/hosts file: mailhog.local


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