← back to Watches

scripts/setup-nginx.sh

90 lines

#!/bin/bash

###############################################################################
# Setup Nginx Reverse Proxy for Omega Watch Price History
###############################################################################

set -euo pipefail

GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
NC='\033[0m'

log() {
    echo -e "${GREEN}[$(date +'%H:%M:%S')]${NC} $1"
}

error() {
    echo -e "${RED}[ERROR]${NC} $1" >&2
}

warning() {
    echo -e "${YELLOW}[WARNING]${NC} $1"
}

PROJECT_DIR="/root/Projects/watches"
NGINX_CONFIG="${PROJECT_DIR}/nginx/omega-watches.conf"
NGINX_AVAILABLE="/etc/nginx/sites-available/omega-watches"
NGINX_ENABLED="/etc/nginx/sites-enabled/omega-watches"

# Check if nginx is installed
if ! command -v nginx &> /dev/null; then
    error "Nginx is not installed!"
    echo "Install with: apt-get update && apt-get install -y nginx"
    exit 1
fi

log "Setting up Nginx reverse proxy..."

# Backup existing configuration if it exists
if [ -f "${NGINX_AVAILABLE}" ]; then
    warning "Existing configuration found, creating backup..."
    cp "${NGINX_AVAILABLE}" "${NGINX_AVAILABLE}.backup.$(date +%Y%m%d_%H%M%S)"
fi

# Copy configuration
log "Copying Nginx configuration..."
cp "${NGINX_CONFIG}" "${NGINX_AVAILABLE}"

# Create symlink
log "Creating symlink to sites-enabled..."
ln -sf "${NGINX_AVAILABLE}" "${NGINX_ENABLED}"

# Remove default site if it exists
if [ -L "/etc/nginx/sites-enabled/default" ]; then
    log "Removing default Nginx site..."
    rm -f "/etc/nginx/sites-enabled/default"
fi

# Test configuration
log "Testing Nginx configuration..."
if nginx -t; then
    log "✓ Nginx configuration is valid"
else
    error "Nginx configuration test failed!"
    exit 1
fi

# Reload Nginx
log "Reloading Nginx..."
systemctl reload nginx

# Check status
if systemctl is-active --quiet nginx; then
    log "✓ Nginx is running"
else
    error "Nginx is not running!"
    exit 1
fi

log "=========================================="
log "Nginx setup completed successfully!"
log "=========================================="
log "Configuration: ${NGINX_AVAILABLE}"
log "Service will be available at: http://45.61.58.125"
log "API endpoint: http://45.61.58.125/api"
log ""
log "To enable HTTPS, configure SSL certificates and uncomment the HTTPS block"
log "=========================================="