Skip to content

Instantly share code, notes, and snippets.

@Caffe1neAdd1ct
Last active August 25, 2023 21:11
Show Gist options
  • Save Caffe1neAdd1ct/ea28bb49baaea86c203407629b442681 to your computer and use it in GitHub Desktop.
Save Caffe1neAdd1ct/ea28bb49baaea86c203407629b442681 to your computer and use it in GitHub Desktop.
Installation of MailHog on CentOS 7
## Install packages
sudo yum install wget curl vim epel-release
sudo yum install daemonize.x86_64
## Install mailhog
wget https://github.com/mailhog/MailHog/releases/download/v0.2.0/MailHog_linux_amd64
sudo chmod +x MailHog_linux_amd64
sudo chown root:root MailHog_linux_amd64
sudo mv MailHog_linux_amd64 /usr/sbin/mailhog
## Install mailhog initd service
wget https://raw.githubusercontent.com/geerlingguy/ansible-role-mailhog/master/templates/mailhog.init.j2
sudo chown root:root mailhog.init.j2
sudo chmod +x mailhog.init.j2
sudo mv mailhog.init.j2 /etc/init.d/mailhog
### Fix the paths in the mailhog init.d file
sudo vim /etc/init.d/mailhog
## Start mailhog
sudo chkconfig mailhog on
sudo service mailhog start
@Caffe1neAdd1ct
Copy link
Author

Caffe1neAdd1ct commented Sep 1, 2016

example init.d with auth file:

#!/bin/sh
# /etc/init.d/mailhog
#
# MailHog init script.
#
# @author Jeff Geerling

### BEGIN INIT INFO
# Provides:          mailhog
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start MailHog at boot time.
# Description:       Enable MailHog.
### END INIT INFO

PID=/var/run/mailhog.pid
LOCK=/var/lock/mailhog.lock
USER=nobody
BIN=/usr/sbin/mailhog
DAEMONIZE_BIN=/usr/sbin/daemonize
AUTH="-auth-file=/etc/mailhog/passwd"

# Carry out specific functions when asked to by the system
case "$1" in
  start)
    echo "Starting mailhog."
    $DAEMONIZE_BIN -p $PID -l $LOCK -u $USER $BIN $AUTH
    ;;
  stop)
    if [ -f $PID ]; then
      echo "Stopping mailhog.";
      kill -TERM $(cat $PID);
      rm -f $PID;
    else
      echo "MailHog is not running.";
    fi
    ;;
  restart)
    echo "Restarting mailhog."
    if [ -f $PID ]; then
      kill -TERM $(cat $PID);
      rm -f $PID;
    fi
    $DAEMONIZE_BIN -p $PID -l $LOCK -u $USER $BIN $AUTH
    ;;
  status)
    if [ -f $PID ]; then
      echo "MailHog is running.";
    else
      echo "MailHog is not running.";
      exit 3
    fi
    ;;
  *)
    echo "Usage: /etc/init.d/mailhog {start|stop|status|restart}"
    exit 1
    ;;
esac

exit 0

use mailhog bcrypt mylovelypassword to generate the hash
add hash in form username:hash to /etc/mailhog/passwd

@Caffe1neAdd1ct
Copy link
Author

Caffe1neAdd1ct commented Sep 5, 2016

Running on localhost only uncomment AUTH line and add $AUTH into start and restart for mailhog to handle http auth on localhost:

#!/bin/sh
# /etc/init.d/mailhog
#
# MailHog init script.
#
# @author Jeff Geerling

### BEGIN INIT INFO
# Provides:          mailhog
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start MailHog at boot time.
# Description:       Enable MailHog.
### END INIT INFO

PID=/var/run/mailhog.pid
LOCK=/var/lock/mailhog.lock
USER=nobody
BIN=/usr/sbin/mailhog
DAEMONIZE_BIN=/usr/sbin/daemonize
#AUTH="-auth-file=/etc/mailhog/passwd"
BIND="-ui-bind-addr=127.0.0.1:8025 -api-bind-addr=127.0.0.1:8025"

# Carry out specific functions when asked to by the system
case "$1" in
  start)
    echo "Starting mailhog."
    $DAEMONIZE_BIN -p $PID -l $LOCK -u $USER $BIN $BIND
    ;;
  stop)
    if [ -f $PID ]; then
      echo "Stopping mailhog.";
      kill -TERM $(cat $PID);
      rm -f $PID;
    else
      echo "MailHog is not running.";
    fi
    ;;
  restart)
    echo "Restarting mailhog."
    if [ -f $PID ]; then
      kill -TERM $(cat $PID);
      rm -f $PID;
    fi
    $DAEMONIZE_BIN -p $PID -l $LOCK -u $USER $BIN $BIND
    ;;
  status)
    if [ -f $PID ]; then
      echo "MailHog is running.";
    else
      echo "MailHog is not running.";
      exit 3
    fi
    ;;
  *)
    echo "Usage: /etc/init.d/mailhog {start|stop|status|restart}"
    exit 1
    ;;
esac

exit 0

Putting Apache in front with a virtual host proxying to localhost for SSL support:

Listen 123.456.789.123:8025 https

<VirtualHost 123.456.789.123:8025> 

  ProxyPreserveHost On
  ProxyRequests Off
  ServerName my.domain.com
  ProxyPass / http://127.0.0.1:8025/
  ProxyPassReverse / http://127.0.0.1:8025/

  <Proxy http://127.0.0.1:8025/>
    AuthType Basic
    AuthName "Auth Please"
    AuthUserFile "/path/to/passwd"
    require valid-user
  </Proxy>

  SSLEngine on
  SSLCertificateFile /etc/ssl/certs/cert.crt
  SSLCertificateKeyFile /etc/ssl/certs/cert.key
  SSLCACertificateFile /etc/ssl/certs/cert.cabundle

  ErrorLog /var/log/mailhog.error.log
  CustomLog /var/log/mailhog.access.log combined

</VirtualHost>

@richardudovich
Copy link

I had to change #! /bin/sh to #!/bin/sh for it to work correctly

@Caffe1neAdd1ct
Copy link
Author

@richardudovich thanks, fixed my notes above 👍

@iansebryk
Copy link

this doesn't work for CentOS 8. daemonize isn't in the epel-release rpm for it. it's only in the one for CentOS 7. might want to update this script, if it can still work for 8.

@Caffe1neAdd1ct
Copy link
Author

@iansebryk this was put together before CentOS 8 was released (2016), I'll update the title to specify the version.

When trying this on CentOS 8 is there any particular part you got stuck on or any specific errors?

@iansebryk
Copy link

yes, i saw that this was done before v8. i just wanted you to know that it wasn't working for current CentOS release. the problem, as i mentioned, is that the yum install daemonize.x86_64 fails. there is no such beast in the v8 repos. and given all the changes in v8, i wasn't confident that manually adding the rpm from v7 would fix the problem without creating more.

if this is something you can easily fix, i'd be most grateful. if there's something i can easily do on my end, i'm equally open to that as well.

@Caffe1neAdd1ct
Copy link
Author

Caffe1neAdd1ct commented Jul 17, 2020 via email

@Caffe1neAdd1ct
Copy link
Author

Thinking something like the following may work: https://tuttlem.github.io/2018/02/03/create-a-systemd-daemon.html

@iansebryk
Copy link

oooh. nice find! i'll give that a whirl in the next day or two and keep you posted. thank you. :)

if this is something you can easily fix, i'd be most grateful. if there's something i can easily do on my end, i'm equally open to that as well.

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