#!/bin/bash # This installation script works on Ubuntu 14.04 and 16.04 # Run as root! if [[ $EUID -ne 0 ]]; then echo "This script must be run as root" 1>&2 exit 1 fi set -e export DEBIAN_FRONTEND=noninteractive MYSQL_ROOT_PASSWORD=`pwgen 12 -1` debconf-set-selections <<< 'mariadb-server-5.5 mysql-server/root_password password $MYSQL_ROOT_PASSWORD' debconf-set-selections <<< 'mariadb-server-5.5 mysql-server/root_password_again password $MYSQL_ROOT_PASSWORD' curl -sL https://deb.nodesource.com/setup_7.x | bash - apt-get -q -y install mariadb-server pwgen nodejs imagemagick git ufw build-essential dnsutils python software-properties-common apt-add-repository -y ppa:chris-lea/redis-server apt-get update apt-get -q -y install redis-server apt-get clean PUBLIC_IP=`curl -s https://api.ipify.org` if [ ! -z "$PUBLIC_IP" ]; then HOSTNAME=`dig +short -x $PUBLIC_IP | sed 's/\.$//'` HOSTNAME="${HOSTNAME:-$PUBLIC_IP}" fi HOSTNAME="${HOSTNAME:-`hostname`}" MYSQL_PASSWORD=`pwgen 12 -1` MYSQL_RO_PASSWORD=`pwgen 12 -1` DKIM_API_KEY=`pwgen 12 -1` SMTP_PASS=`pwgen 12 -1` # Setup MySQL user for Mailtrain mysql -u root -e "CREATE USER 'mailtrain'@'localhost' IDENTIFIED BY '$MYSQL_PASSWORD';" -p$MYSQL_ROOT_PASSWORD mysql -u root -e "GRANT ALL PRIVILEGES ON mailtrain.* TO 'mailtrain'@'localhost';" -p$MYSQL_ROOT_PASSWORD mysql -u root -e "CREATE USER 'mailtrain_ro'@'localhost' IDENTIFIED BY '$MYSQL_RO_PASSWORD';" -p$MYSQL_ROOT_PASSWORD mysql -u root -e "GRANT SELECT ON mailtrain.* TO 'mailtrain_ro'@'localhost';" -p$MYSQL_ROOT_PASSWORD mysql -u mailtrain --password="$MYSQL_PASSWORD" -e "CREATE database mailtrain;" # Enable firewall, allow connections to SSH, HTTP, HTTPS and SMTP ufw allow 22/tcp ufw allow 80/tcp ufw allow 443/tcp ufw allow 25/tcp ufw --force enable # Fetch Mailtrain files mkdir -p /opt/mailtrain cd /opt/mailtrain git clone git://github.com/Mailtrain-org/mailtrain.git . # Normally we would let Mailtrain itself to import the initial SQL data but in this case # we need to modify it, before we start Mailtrain mysql -u mailtrain -p"$MYSQL_PASSWORD" mailtrain < setup/sql/mailtrain.sql mysql -u mailtrain -p"$MYSQL_PASSWORD" mailtrain <> config/production.toml <> workers/reports/config/production.toml < /etc/logrotate.d/mailtrain /var/log/mailtrain.log { daily rotate 12 compress delaycompress missingok notifempty copytruncate nomail } EOM if [ -d "/run/systemd/system" ]; then # Set up systemd service script cp setup/mailtrain.service /etc/systemd/system/ systemctl enable mailtrain.service else # Set up upstart service script cp setup/mailtrain.conf /etc/init/ fi # Fetch ZoneMTA files mkdir -p /opt/zone-mta cd /opt/zone-mta git clone git://github.com/zone-eu/zone-mta.git . git checkout 6964091273 # Ensure queue folder mkdir -p /var/data/zone-mta/mailtrain # Setup installation configuration cat >> config/production.json < ~/mysql_root_password echo "MySQL root password: $MYSQL_ROOT_PASSWORD" echo "Success! Open http://$HOSTNAME/ and log in as admin:test";