###=========================================================== ## Ubuntu 18.04 Mailserver installer ###=========================================================== ##----------## # Menu # ##----------## #echo "Menu" #echo "Ubuntu 18.04 Mailserver installatie script." #echo "Domein zonder www en mail.:" #read domain #echo "Algemeen wachtwoord:" #read password #echo "Administrator email:" #read email ##-----------------## # Static-Vars # ##-----------------## echo "Static-Vars" domain=ongz.nl password=JW9t9ipdgLrWvMqHq7hX email=admin@ictdagbesteding.nl phpver=7.3 domonly=${domain} domain=mail.${domain} branch=alpha dhparam=1024 ##----------------## # Pre-Config # ##----------------## echo "Pre-Config" hostnamectl set-hostname $domain apt update add-apt-repository universe -y add-apt-repository ppa:ondrej/php -y apt install software-properties-common -y apt upgrade -y apt autoremove -y mkdir -p /etc/nginx mkdir -p /var/www/"$domain"/html chmod -R 755 /var/www ##-----------------------## # Html Folder Perms # ##-----------------------## echo "Html Folder Perms" chown -R www-data:www-data /var/www/"$domain"/html ##-----------## # NGINX # ##-----------## echo "NGINX" #installing nginx from apt apt install -y nginx wget -q -t7 https://git.ictmaatwerk.com/VPS-scripts/Ubuntu-Web/raw/branch/master/config/nginx/nginx-default.conf -O /etc/nginx/nginx.conf cat < /etc/nginx/sites-available/"$domain" #fastcgi_cache_path /etc/nginx/cache levels=1:2 keys_zone=MYAPP:100m max_size=10g inactive=1440m; server { listen 80; server_name www.$domain; return 301 http://$domain\$request_uri; } server { listen 80; listen [::]:80; root /var/www/$domain/html; index index.php index.html index.htm index.nginx-debian.html; server_name $domain; #return 301 \$scheme:/\$domain\$request_uri; Redirect to non-www #return 301 https://domein.nl$request_uri; Redirect to other domain #add_header X-Cache "\$upstream_cache_status"; #netdata here gzip on; gzip_proxied any; gzip_types text/plain text/css text/xml text/javascript application/javascript application/x-javascript image/svg image/svg+xml application/xml image/x-icon; gzip_comp_level 2; gzip_disable "msie6"; gzip_buffers 16 8k; # location /rspamd { # proxy_pass http://127.0.0.1:11334/; # proxy_set_header Host \$host; # proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for; #} location / { #try_files \$uri \$uri/ =404; try_files \$uri \$uri/ /index.php\$is_args\$args; #try_files \$uri \$uri/ \$uri.html \$uri.php\$is_args\$query_string; } location = /favicon.ico { log_not_found off; access_log off; } location = /robots.txt { log_not_found off; access_log off; allow all; } location ~* \.(css|gif|ico|jpeg|jpg|js|png|svg|webp|eot|otf|woff|woff2|ttf|ogg)\$ { expires max; log_not_found off; add_header Cache-Control "public, no-transform"; } location ~ \.php\$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/var/run/php/php${phpver}-fpm.sock; #fastcgi_cache MYAPP; #fastcgi_cache_valid 200 302 301 1m; #fastcgi_cache_valid 404 1m; #fastcgi_cache_bypass \$no_cache; #fastcgi_no_cache \$no_cache; #fastcgi_cache_revalidate on; #fastcgi_cache_background_update on; #fastcgi_cache_lock on; #fastcgi_cache_use_stale updating; #fastcgi_buffer_size 128k; #fastcgi_buffers 256 16k; #fastcgi_busy_buffers_size 256k; #fastcgi_temp_file_write_size 256k; } location ~ /\.ht { deny all; } location /phpmyadmin { index index.php; } #Cache everything by default set \$no_cache 0; #Don't cache POST requests if (\$request_method = POST) { set \$no_cache 1; } #Don't cache if the URL contains a query string if (\$query_string != "") { set \$no_cache 1; } #Don't cache the following URLs if (\$request_uri ~* "/(administrator/|login.php)") { set \$no_cache 1; } #Don't cache if there is a cookie called PHPSESSID if (\$http_cookie = "PHPSESSID") { set \$no_cache 1; } } EOF ln -s /etc/nginx/sites-available/"$domain" /etc/nginx/sites-enabled/ ##-------------------------------## # NGINX Single core bug fix # ##-------------------------------## mkdir /etc/systemd/system/nginx.service.d printf "[Service]\nExecStartPost=/bin/sleep 0.1\n" > /etc/systemd/system/nginx.service.d/override.conf systemctl daemon-reload systemctl restart nginx ##-----------------------## # MySQL Installation # ##-----------------------## wget -q -t7 https://git.ictmaatwerk.com/VPS-scripts/Ubuntu-Mail/raw/branch/alpha/mysql-8.0.sh -O /tmp/mysql-8.0.sh source /tmp/mysql-8.0.sh ##------------------------------## # MySQL_Secure_Installation # ##------------------------------## mysqladmin -u root password "$password" mysql -u root -p"$password" -e "DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1')" mysql -u root -p"$password" -e "DELETE FROM mysql.user WHERE User=''" mysql -u root -p"$password" -e "DELETE FROM mysql.db WHERE Db='test' OR Db='test\_%'" mysql -u root -p"$password" -e "SELECT user,authentication_string,plugin,host FROM mysql.user;" mysql -u root -p"$password" -e "ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '"$password"';" mysql -u root -p"$password" -e "FLUSH PRIVILEGES;" ##-----------------------------## # MySQL Database Creation # ##-----------------------------## mysql -u root -p"$password" -e "CREATE DATABASE postfixadmin;" mysql -u root -p"$password" -e "GRANT ALL ON postfixadmin.* TO 'postfixadmin'@'localhost' IDENTIFIED BY '"$password"';" mysql -u root -p"$password" -e "FLUSH PRIVILEGES;" ##------------------## # PostfixADMIN # ##------------------## echo "PostfixADMIN" apt install php${phpver} php${phpver}-zip php${phpver}-fpm php${phpver}-cli php${phpver}-json php${phpver}-mysql php${phpver}-opcache php${phpver}-mbstring php${phpver}-readline -y apt install libc-client2007e mlock php${phpver}-common php${phpver}-imap -y mkdir -p /var/www/"$domain"/html/postfixadmin/templates_c wget -q -t7 https://downloads.sourceforge.net/project/postfixadmin/postfixadmin/postfixadmin-3.1/postfixadmin-3.1.tar.gz -O /tmp/postfixadmin.tar.gz tar -xf /tmp/postfixadmin.tar.gz -C /var/www/"$domain"/html/postfixadmin --strip-components=1 chmod 755 -R /var/www/"$domain"/html/postfixadmin/templates_c chown -R www-data: /var/www/"$domain"/html/ wget https://git.ictmaatwerk.com/VPS-scripts/Ubuntu-Mail/raw/branch/"$branch"/config/postfixadmin/config.local.php -O /var/www/$domain/html/postfixadmin/config.local.php sed -i -e 's/PASSword/'$password'/' -e 's/dOmaINnamE/'$domonly'/' /var/www/"$domain"/html/postfixadmin/config.local.php sudo -u www-data php /var/www/"$domain"/html/postfixadmin/upgrade.php bash /var/www/"$domain"/html/postfixadmin/scripts/postfixadmin-cli admin add superadmin@"$domonly" --superadmin 1 --active 1 --password "$password" --password2 "$password" groupadd -g 5000 vmail useradd -u 5000 -g vmail -s /usr/sbin/nologin -d /var/mail/vmail -m vmail ##-------------## # Certbot # ##-------------## add-apt-repository ppa:certbot/certbot -y apt install -y python-certbot-nginx mkdir -p /etc/letsencrypt/live/$domain/ certbot --nginx -n -d "$domain" -m "$email" --hsts --redirect --no-eff-email --agree-tos echo "certbot --nginx -n -d $domain -m $email --hsts --redirect --no-eff-email --agree-tos" > ~/certbotactivate.sh bash ~/certbotactivate.sh sed -i 's/ssl ipv6only/ssl http2 ipv6only/g' /etc/nginx/sites-available/"$domain" sed -i 's/listen 443 ssl/listen 443 ssl http2/g' /etc/nginx/sites-available/"$domain" sed -i 's#include /etc/letsencrypt/options-ssl-nginx.conf;#ssl_ciphers EECDH+CHACHA20:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;#g' /etc/nginx/sites-available/"$domain" openssl dhparam -out /etc/ssl/certs/dhparam.pem "$dhparam" chmod 755 -R /etc/ssl/certs/dhparam.pem systemctl restart nginx ##-----------------------## # Postfix Installer # ##-----------------------## debconf-set-selections <<< "postfix postfix/mailname string $(hostname -f)" debconf-set-selections <<< "postfix postfix/main_mailer_type string 'Internet Site'" apt install postfix postfix-mysql -y ##---------------------------## # Postfix Configuration # ##---------------------------## mkdir -p /etc/postfix/sql wget -q -t7 https://git.ictmaatwerk.com/VPS-scripts/Ubuntu-Mail/raw/branch/"$branch"/config/postfix/mysql_virtual_alias_domain_catchall_maps.cf -O /etc/postfix/sql/mysql_virtual_alias_domain_catchall_maps.cf wget -q -t7 https://git.ictmaatwerk.com/VPS-scripts/Ubuntu-Mail/raw/branch/"$branch"/config/postfix/mysql_virtual_alias_domain_mailbox_maps.cf -O /etc/postfix/sql/mysql_virtual_alias_domain_mailbox_maps.cf wget -q -t7 https://git.ictmaatwerk.com/VPS-scripts/Ubuntu-Mail/raw/branch/"$branch"/config/postfix/mysql_virtual_alias_domain_maps.cf -O /etc/postfix/sql/mysql_virtual_alias_domain_maps.cf wget -q -t7 https://git.ictmaatwerk.com/VPS-scripts/Ubuntu-Mail/raw/branch/"$branch"/config/postfix/mysql_virtual_alias_maps.cf -O /etc/postfix/sql/mysql_virtual_alias_maps.cf wget -q -t7 https://git.ictmaatwerk.com/VPS-scripts/Ubuntu-Mail/raw/branch/"$branch"/config/postfix/mysql_virtual_domains_maps.cf -O /etc/postfix/sql/mysql_virtual_domains_maps.cf wget -q -t7 https://git.ictmaatwerk.com/VPS-scripts/Ubuntu-Mail/raw/branch/"$branch"/config/postfix/mysql_virtual_mailbox_maps.cf -O /etc/postfix/sql/mysql_virtual_mailbox_maps.cf sed -i 's/PASSword/'$password'/g' /etc/postfix/sql/mysql_virtual_domains_maps.cf sed -i 's/PASSword/'$password'/g' /etc/postfix/sql/mysql_virtual_alias_maps.cf sed -i 's/PASSword/'$password'/g' /etc/postfix/sql/mysql_virtual_alias_domain_maps.cf sed -i 's/PASSword/'$password'/g' /etc/postfix/sql/mysql_virtual_alias_domain_catchall_maps.cf sed -i 's/PASSword/'$password'/g' /etc/postfix/sql/mysql_virtual_mailbox_maps.cf sed -i 's/PASSword/'$password'/g' /etc/postfix/sql/mysql_virtual_alias_domain_mailbox_maps.cf echo "#MySQL Database" >> /etc/postfix/main.cf postconf -e "virtual_mailbox_domains = mysql:/etc/postfix/sql/mysql_virtual_domains_maps.cf" postconf -e "virtual_alias_maps = mysql:/etc/postfix/sql/mysql_virtual_alias_maps.cf, mysql:/etc/postfix/sql/mysql_virtual_alias_domain_maps.cf, mysql:/etc/postfix/sql/mysql_virtual_alias_domain_catchall_maps.cf" postconf -e "virtual_mailbox_maps = mysql:/etc/postfix/sql/mysql_virtual_mailbox_maps.cf, mysql:/etc/postfix/sql/mysql_virtual_alias_domain_mailbox_maps.cf" echo "#Use Dovecot's LMTP service as the default mail delivery" >> /etc/postfix/main.cf postconf -e "virtual_transport = lmtp:unix:private/dovecot-lmtp" echo "#TLS and SSL certificate" >> /etc/postfix/main.cf postconf -e "smtp_tls_security_level = may" postconf -e "smtpd_tls_security_level = may" postconf -e "smtp_tls_note_starttls_offer = yes" postconf -e "smtpd_tls_loglevel = 1" postconf -e "smtpd_tls_received_header = yes" postconf -e "smtpd_tls_cert_file = /etc/letsencrypt/live/"$domain"/fullchain.pem" postconf -e "smtpd_tls_key_file = /etc/letsencrypt/live/"$domain"/privkey.pem" echo "#SMTP Settings, hand off authentication to Dovecot" >> /etc/postfix/main.cf postconf -e "smtpd_sasl_type = dovecot" postconf -e "smtpd_sasl_path = private/auth" postconf -e "smtpd_sasl_local_domain =" postconf -e "smtpd_sasl_security_options = noanonymous" postconf -e "broken_sasl_auth_clients = yes" postconf -e "smtpd_sasl_auth_enable = yes" postconf -e "smtpd_recipient_restrictions = permit_sasl_authenticated,permit_mynetworks,reject_unauth_destination" sed -i 's/mynetworks = /#mynetworks = /g' /etc/postfix/main.cf wget -q -t7 https://git.ictmaatwerk.com/VPS-scripts/Ubuntu-Mail/raw/branch/$branch/config/postfix/master.cf -O /etc/postfix/master.cf ##-----------------------## # Dovecot Installer # ##-----------------------## wget -O- https://repo.dovecot.org/DOVECOT-REPO-GPG | sudo apt-key add - echo "deb https://repo.dovecot.org/ce-2.3-latest/ubuntu/$(lsb_release -cs) $(lsb_release -cs) main" | sudo tee -a /etc/apt/sources.list.d/dovecot.list apt update apt install dovecot-imapd dovecot-lmtpd dovecot-pop3d dovecot-mysql -y ##---------------------------## # Dovecot Configuration # ##---------------------------## wget -q -t7 https://git.ictmaatwerk.com/VPS-scripts/Ubuntu-Mail/raw/branch/"$branch"/config/dovecot/15-mailboxes.conf -O /etc/dovecot/conf.d/15-mailboxes.conf wget -q -t7 https://git.ictmaatwerk.com/VPS-scripts/Ubuntu-Mail/raw/branch/"$branch"/config/dovecot/10-mail.conf -O /etc/dovecot/conf.d/10-mail.conf wget -q -t7 https://git.ictmaatwerk.com/VPS-scripts/Ubuntu-Mail/raw/branch/"$branch"/config/dovecot/10-auth.conf -O /etc/dovecot/conf.d/10-auth.conf wget -q -t7 https://git.ictmaatwerk.com/VPS-scripts/Ubuntu-Mail/raw/branch/"$branch"/config/dovecot/10-master.conf -O /etc/dovecot/conf.d/10-master.conf wget -q -t7 https://git.ictmaatwerk.com/VPS-scripts/Ubuntu-Mail/raw/branch/"$branch"/config/dovecot/10-ssl.conf -O /etc/dovecot/conf.d/10-ssl.conf wget -q -t7 https://git.ictmaatwerk.com/VPS-scripts/Ubuntu-Mail/raw/branch/"$branch"/config/dovecot/20-lmtp.conf -O /etc/dovecot/conf.d/20-lmtp.conf wget -q -t7 https://git.ictmaatwerk.com/VPS-scripts/Ubuntu-Mail/raw/branch/"$branch"/config/dovecot/20-imap.conf -O /etc/dovecot/conf.d/20-imap.conf wget -q -t7 https://git.ictmaatwerk.com/VPS-scripts/Ubuntu-Mail/raw/branch/"$branch"/config/dovecot/dovecot-dict-sql.conf.ext -O /etc/dovecot/dovecot-dict-sql.conf.ext wget -q -t7 https://git.ictmaatwerk.com/VPS-scripts/Ubuntu-Mail/raw/branch/"$branch"/config/dovecot/dovecot-sql.conf.ext -O /etc/dovecot/dovecot-sql.conf.ext wget -q -t7 https://git.ictmaatwerk.com/VPS-scripts/Ubuntu-Mail/raw/branch/"$branch"/config/dovecot/quota-warning.sh -O /usr/local/bin/quota-warning.sh sed -i 's/PASSword/'$password'/g' /etc/dovecot/dovecot-sql.conf.ext sed -i 's/PASSword/'$password'/g' /etc/dovecot/dovecot-dict-sql.conf.ext sed -i -e 's/DOMAINname/'$domain'/' -e 's/#ssl_cert = /ssl_cert = /' -e 's/#ssl_key = /ssl_key = /' -e 's/#ssl_dh = /ssl_dh = /' /etc/dovecot/conf.d/10-ssl.conf chmod +x /usr/local/bin/quota-warning.sh ##--------------------------------------## # Dovecot move Spam to Spam Folder # ##--------------------------------------## apt install dovecot-sieve dovecot-managesieved -y mkdir -p /etc/dovecot/sieve/ wget -q -t7 https://git.ictmaatwerk.com/VPS-scripts/Ubuntu-Mail/raw/branch/"$branch"/config/dovecot/90-sieve.conf -O /etc/dovecot/conf.d/90-sieve.conf wget -q -t7 https://git.ictmaatwerk.com/VPS-scripts/Ubuntu-Mail/raw/branch/"$branch"/config/sieve/default.sieve -O /etc/dovecot/sieve/default.sieve sievec /etc/dovecot/sieve/default.sieve chown vmail:vmail /etc/dovecot/sieve/ -R ##--------------## # Rainloop # ##--------------## apt install unzip wget -q -t7 https://git.ictmaatwerk.com/VPS-scripts/Ubuntu-Mail/raw/branch/$branch/rainloop.sh -O /tmp/rainloop.sh source /tmp/rainloop.sh ##-----------------------## # Enabling Services # ##-----------------------## systemctl enable postfix.service postfix@-.service dovecot.service ##-----------------------## # Starting Services # ##-----------------------## systemctl restart postfix.service postfix@-.service dovecot.service