#!/bin/bash # Setup script for Nginx reverse proxy # This script helps set up Nginx for the Moyos Wedding App # Usage: Run this script on the Nginx container or host set -e # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color log() { echo -e "${GREEN}[$(date +'%Y-%m-%d %H:%M:%S')]${NC} $1" } error() { echo -e "${RED}[ERROR]${NC} $1" >&2 } warn() { echo -e "${YELLOW}[WARN]${NC} $1" } info() { echo -e "${BLUE}[INFO]${NC} $1" } # Check if running as root if [ "$EUID" -ne 0 ]; then error "This script must be run as root" exit 1 fi log "Starting Nginx setup for Moyos Wedding App..." # Update system log "Updating system packages..." apt-get update apt-get upgrade -y # Install Nginx log "Installing Nginx..." if ! command -v nginx &> /dev/null; then apt-get install -y nginx log "Nginx installed" else warn "Nginx already installed" fi # Install Certbot for Let's Encrypt log "Installing Certbot..." if ! command -v certbot &> /dev/null; then apt-get install -y certbot python3-certbot-nginx log "Certbot installed" else warn "Certbot already installed" fi # Check if nginx.conf exists in app directory APP_DIR="${APP_DIR:-/opt/moyos-wedding-app/app}" NGINX_CONFIG_SOURCE="$APP_DIR/nginx.conf" if [ ! -f "$NGINX_CONFIG_SOURCE" ]; then warn "nginx.conf not found at $NGINX_CONFIG_SOURCE" warn "You'll need to manually configure Nginx" exit 1 fi # Copy nginx config NGINX_SITE="moyos-wedding-app" NGINX_CONFIG="/etc/nginx/sites-available/$NGINX_SITE" log "Copying Nginx configuration..." cp "$NGINX_CONFIG_SOURCE" "$NGINX_CONFIG" # Prompt for domain name echo "" read -p "Enter your domain name (e.g., your-domain.com): " DOMAIN_NAME if [ -z "$DOMAIN_NAME" ]; then error "Domain name is required" exit 1 fi # Replace placeholder domain in config log "Updating domain name in configuration..." sed -i "s/your-domain.com/$DOMAIN_NAME/g" "$NGINX_CONFIG" # Enable site log "Enabling Nginx site..." ln -sf "$NGINX_CONFIG" "/etc/nginx/sites-enabled/$NGINX_SITE" # Remove default site if it exists if [ -f "/etc/nginx/sites-enabled/default" ]; then log "Removing default Nginx site..." rm -f /etc/nginx/sites-enabled/default fi # Test Nginx configuration log "Testing Nginx configuration..." if nginx -t; then log "Nginx configuration is valid" else error "Nginx configuration test failed" exit 1 fi # Get SSL certificate echo "" read -p "Do you want to obtain an SSL certificate from Let's Encrypt? (y/n): " GET_SSL if [ "$GET_SSL" = "y" ] || [ "$GET_SSL" = "Y" ]; then log "Obtaining SSL certificate..." # First, reload Nginx with HTTP config systemctl reload nginx # Get certificate certbot --nginx -d "$DOMAIN_NAME" -d "www.$DOMAIN_NAME" --non-interactive --agree-tos --email "admin@$DOMAIN_NAME" || { warn "SSL certificate setup failed. You can run manually:" warn " certbot --nginx -d $DOMAIN_NAME -d www.$DOMAIN_NAME" } # Update nginx config with SSL paths (certbot should do this automatically) log "SSL certificate configured" else warn "Skipping SSL certificate setup" warn "You can obtain a certificate later with:" warn " certbot --nginx -d $DOMAIN_NAME -d www.$DOMAIN_NAME" fi # Reload Nginx log "Reloading Nginx..." systemctl reload nginx # Enable Nginx to start on boot log "Enabling Nginx to start on boot..." systemctl enable nginx # Summary echo "" log "Nginx setup completed successfully!" echo "" info "Configuration file: $NGINX_CONFIG" info "Domain: $DOMAIN_NAME" info "SSL: $([ "$GET_SSL" = "y" ] || [ "$GET_SSL" = "Y" ] && echo "Configured" || echo "Not configured")" echo "" info "Useful commands:" info " - Test config: nginx -t" info " - Reload: systemctl reload nginx" info " - Status: systemctl status nginx" info " - Logs: tail -f /var/log/nginx/error.log" echo "" warn "Important: Make sure your application is running on port 3000" warn "Update the upstream servers in $NGINX_CONFIG if needed" echo ""