# This is not a standalone script. It provides common functions to server-*.sh scripts case "$hostType" in centos7) hostTypeLabel="CentOS 7" redisService=redis apacheConf="/etc/httpd/conf.d/mailtrain.conf" apacheLogsPath="logs" if hash firewall-cmd 2>/dev/null; then firewallCmdExists=yes fi ;; centos8) hostTypeLabel="CentOS 8" redisService=redis apacheConf="/etc/httpd/conf.d/mailtrain.conf" apacheLogsPath="logs" if hash firewall-cmd 2>/dev/null; then firewallCmdExists=true fi ;; ubuntu1804) hostTypeLabel="Ubuntu 18.04 LTS" redisService=redis-server apacheConf="/etc/apache2/conf-available/mailtrain.conf" apacheLogsPath="/var/log/apache2" if hash ufw 2>/dev/null; then firewallCmdExists=yes fi ;; debian10) hostTypeLabel="Debian 10" redisService=redis-server apacheConf="/etc/apache2/conf-available/mailtrain.conf" apacheLogsPath="/var/log/apache2" if hash ufw 2>/dev/null; then firewallCmdExists=yes fi ;; esac function performInstallLocal { local paramCount="$1" if [ $paramCount -ne 0 ]; then echo "Error: incorrect number of parameters." cat < Installs Mailtrain 2 on ${hostTypeLabel}. This performs installation for external use. It installs Mailtrain, sets up a reverse HTTPS proxy using Apache HTTPD, sets up firewall rules, and obtains a certificate from Letsencrypt. You have to allocate three endpoints for Mailtrain - trusted (admin UI), sandbox (editors for templates), public (subscription forms and archive). These endpoints have to differ in hostname. It's fine to host them all from one IP address. The email parameters is needed by certbot. Note, that this will automatically accept the Let's Encrypt's Terms of Service. Thus, by running this script below, you agree with the Let's Encrypt's Terms of Service (https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf). Example: install-${hostType}-https.sh mailtrain.example.com sbox.mailtrain.example.com lists.example.com admin@example.com EOF exit 1 fi installPrerequisities installHttpd 443 443 443 createCertificates "${hostTrusted}" "${hostSandbox}" "${hostPublic}" "${email}" installHttpsProxy "${hostTrusted}" 443 "${hostSandbox}" 443 "${hostPublic}" 443 "/etc/letsencrypt/live/${hostPublic}/cert.pem" "/etc/letsencrypt/live/${hostPublic}/privkey.pem" "/etc/letsencrypt/live/${hostPublic}/chain.pem" installMailtrain "https://${hostTrusted}" "https://${hostSandbox}" "https://${hostPublic}" 127.0.0.1 true installService echo echo "Success!" } function installPrerequisities { # Run as root! if [[ $EUID -ne 0 ]]; then echo "This script must be run as root" 1>&2 exit 1 fi case "$hostType" in centos7) yum -y install epel-release curl --silent --location https://rpm.nodesource.com/setup_10.x | bash - cat > /etc/yum.repos.d/mongodb-org.repo < /etc/yum.repos.d/mongodb-org.repo < server/config/production.yaml <> server/services/workers/reports/config/production.yaml < $apacheConf cat >> $apacheConf < ServerName ${hostTrusted} ServerSignature Off RewriteEngine On RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent] ErrorLog ${apacheLogsPath}/${hostTrusted}_redirect_error.log LogLevel warn ServerName ${hostSandbox} ServerSignature Off RewriteEngine On RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent] ErrorLog ${apacheLogsPath}/${hostSandbox}_redirect_error.log LogLevel warn ServerName ${hostPublic} ServerSignature Off RewriteEngine On RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent] ErrorLog ${apacheLogsPath}/${hostPublic}_redirect_error.log LogLevel warn ServerName ${hostTrusted}:${portTrusted} ErrorLog ${apacheLogsPath}/${hostTrusted}_ssl_error.log TransferLog ${apacheLogsPath}/${hostTrusted}_ssl_access.log LogLevel warn SSLEngine on SSLCertificateFile ${certificateFile} SSLCertificateKeyFile ${certificateKey} SSLCertificateChainFile ${caChainFile} ProxyPreserveHost On ProxyPass "/" "http://127.0.0.1:3000/" ProxyPassReverse "/" "http://127.0.0.1:3000/" ServerName ${hostSandbox}:${portSandbox} ErrorLog ${apacheLogsPath}/${hostSandbox}_ssl_error.log TransferLog ${apacheLogsPath}/${hostSandbox}_ssl_access.log LogLevel warn SSLEngine on SSLCertificateFile ${certificateFile} SSLCertificateKeyFile ${certificateKey} SSLCertificateChainFile ${caChainFile} ProxyPreserveHost On ProxyPass "/" "http://127.0.0.1:3003/" ProxyPassReverse "/" "http://127.0.0.1:3003/" ServerName ${hostPublic}:${portPublic} ErrorLog ${apacheLogsPath}/${hostPublic}_ssl_error.log TransferLog ${apacheLogsPath}/${hostPublic}_ssl_access.log LogLevel warn SSLEngine on SSLCertificateFile ${certificateFile} SSLCertificateKeyFile ${certificateKey} SSLCertificateChainFile ${caChainFile} ProxyPreserveHost On ProxyPass "/" "http://127.0.0.1:3004/" ProxyPassReverse "/" "http://127.0.0.1:3004/" EOT case "$hostType" in centos7|centos8) systemctl restart httpd ;; ubuntu1804|debian10) a2enmod ssl a2enmod rewrite a2enmod proxy a2enmod proxy_http a2enconf mailtrain systemctl restart apache2 ;; esac } function createCertificates { # This assumes that HTTPD is already running local hostTrusted="$1" local hostSandbox="$2" local hostPublic="$3" local email="$4" case "$hostType" in centos7) yum install -y certbot python2-certbot-apache ;; centos8) dnf install -y certbot ;; ubuntu1804|debian10) apt-get install -y certbot python3-certbot-apache ;; esac certbot certonly --agree-tos --email "${email}" --webroot --webroot-path /var/www/html -n -d "${hostPublic}" -d "${hostTrusted}" -d "${hostSandbox}" # Install cron echo "0 3 * * * /usr/bin/certbot certonly --apache -n -d \"${hostPublic}\" -d \"${hostTrusted}\" -d \"${hostSandbox}\"" > crontab crontab crontab rm -rf crontab } function installService { cat > /etc/systemd/system/mailtrain.service < server/config/test.yaml <> server/services/workers/reports/config/test.yaml <