diff --git a/setup-email-server.sh b/setup-email-server.sh index 6108d0d..e4d55a7 100755 --- a/setup-email-server.sh +++ b/setup-email-server.sh @@ -4,7 +4,7 @@ # Components: Postfix, Dovecot, Amavis, SpamAssassin, OpenDKIM, PostgreSQL, PostfixAdmin # SSL: Let's Encrypt (DNS-01 Challenge) # Author: Email Server Setup Script -# Date: Sun Aug 3 15:05:28 EDT 2025 +# Date: $(date) set -euo pipefail @@ -24,7 +24,6 @@ DB_USER="postfix" DB_PASSWORD="" POSTFIXADMIN_PASSWORD="" WEBROOT="/var/www/postfixadmin" -POSTFIXADMIN_VHOST_FILE="/etc/apache2/sites-available/postfixadmin.conf" # Logging LOG_FILE="/var/log/email-server-setup.log" @@ -102,13 +101,6 @@ update_system() { install_packages() { info "Installing required packages..." - # Check for and stop Nginx if it's running to avoid port conflicts with Apache - if systemctl is-active --quiet nginx; then - warning "Nginx is running and will be stopped to allow Apache to run." - systemctl stop nginx - systemctl disable nginx - fi - # Install basic packages apt install -y \ curl \ @@ -117,6 +109,7 @@ install_packages() { lsb-release \ software-properties-common \ certbot \ + nginx \ ufw # Install PostgreSQL @@ -729,7 +722,7 @@ get_ssl_certificates() { success "SSL certificates obtained" } -# Install PostfixAdmin and configure Apache +# Install PostfixAdmin install_postfixadmin() { info "Installing PostfixAdmin..." @@ -776,7 +769,7 @@ function maildir_name_hook(\$domain, \$user) { EOF # Configure Apache virtual host - cat > "$POSTFIXADMIN_VHOST_FILE" << EOF + cat > /etc/apache2/sites-available/postfixadmin.conf << EOF ServerName $HOSTNAME Redirect permanent / https://$HOSTNAME/ @@ -807,12 +800,7 @@ EOF a2ensite postfixadmin a2dissite 000-default - # Check Apache config before reloading - if ! apachectl configtest &> /dev/null; then - error "Apache configuration test failed. Please check $POSTFIXADMIN_VHOST_FILE" - fi - - success "PostfixAdmin installed and Apache configured" + success "PostfixAdmin installed" } # Configure firewall @@ -864,14 +852,8 @@ start_services() { systemctl enable --now clamav-daemon systemctl enable --now clamav-freshclam systemctl enable --now opendkim - - # Start Apache2 - info "Starting Apache2..." systemctl enable --now apache2 - if ! systemctl is-active --quiet apache2; then - error "Apache2 failed to start. Check 'journalctl -xeu apache2' for details." - fi - + success "All services started and enabled" } @@ -895,10 +877,10 @@ display_final_info() { cat /etc/opendkim/keys/$DOMAIN/mail.txt echo echo -e "${YELLOW}=== Next Steps ===${NC}" - echo "1. Add the DNS records shown above." - echo "2. Visit https://$HOSTNAME/postfixadmin/setup.php to complete PostfixAdmin setup." - echo "3. Create your first domain and mailbox in PostfixAdmin." - echo "4. Test email sending and receiving." + echo "1. Add the DNS records shown above" + echo "2. Visit https://$HOSTNAME/postfixadmin/setup.php to complete PostfixAdmin setup" + echo "3. Create your first domain and mailbox in PostfixAdmin" + echo "4. Test email sending and receiving" echo echo -e "${BLUE}=== Multiple Domain Support ===${NC}" echo "This server supports unlimited virtual domains!" @@ -918,7 +900,29 @@ display_final_info() { echo " Security: STARTTLS or SSL/TLS" echo echo -e "${GREEN}Setup completed successfully!${NC}" -} # <-- THIS IS THE MISSING BRACE +} + +# Main execution +main() { + echo -e "${BLUE}Email Server Setup Script${NC}" + echo "=========================" + echo + + check_root + get_configuration + update_system + install_packages + configure_postgresql + get_ssl_certificates + configure_postfix + configure_dovecot + configure_opendkim + configure_amavis + install_postfixadmin + configure_firewall + start_services + display_final_info +} # Run main function main "$@" \ No newline at end of file