← back to Watches
scripts/nginx-setup.sh
199 lines
#!/bin/bash
################################################################################
# OMEGA WATCHES - NGINX SETUP SCRIPT
# Configures Nginx reverse proxy with caching and load balancing
################################################################################
set -euo pipefail
GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m'
log() {
echo -e "${GREEN}[$(date +'%Y-%m-%d %H:%M:%S')]${NC} $1"
}
log_info() {
echo -e "${BLUE}[$(date +'%Y-%m-%d %H:%M:%S')]${NC} $1"
}
log_error() {
echo -e "${RED}[$(date +'%Y-%m-%d %H:%M:%S')] ERROR:${NC} $1"
}
print_header() {
echo -e "${BLUE}"
echo "╔════════════════════════════════════════════════════════════════╗"
echo "║ OMEGA WATCHES - NGINX SETUP ║"
echo "╚════════════════════════════════════════════════════════════════╝"
echo -e "${NC}"
}
# Check if Nginx is installed
check_nginx() {
log_info "Checking Nginx installation..."
if ! command -v nginx &> /dev/null; then
log_error "Nginx is not installed!"
echo "Install with: apt install nginx"
exit 1
fi
log "Nginx is installed ✓"
}
# Create cache directory
create_cache_dir() {
log_info "Creating cache directory..."
mkdir -p /var/cache/nginx/omega-watches
chown -R www-data:www-data /var/cache/nginx/omega-watches
log "Cache directory created ✓"
}
# Backup existing configuration
backup_config() {
log_info "Backing up existing Nginx configuration..."
if [ -f /etc/nginx/sites-enabled/omega-watches ]; then
cp /etc/nginx/sites-enabled/omega-watches \
/etc/nginx/sites-enabled/omega-watches.backup.$(date +%Y%m%d_%H%M%S)
log "Backup created ✓"
else
log "No existing configuration to backup"
fi
}
# Install configuration
install_config() {
log_info "Installing Nginx configuration..."
# Copy configuration file
cp /root/Projects/watches/infrastructure/nginx-omega-watches.conf \
/etc/nginx/sites-available/omega-watches
# Remove default site if exists
rm -f /etc/nginx/sites-enabled/default
# Enable site
ln -sf /etc/nginx/sites-available/omega-watches \
/etc/nginx/sites-enabled/omega-watches
log "Configuration installed ✓"
}
# Test configuration
test_config() {
log_info "Testing Nginx configuration..."
if nginx -t; then
log "Configuration test passed ✓"
else
log_error "Configuration test failed!"
exit 1
fi
}
# Configure firewall
configure_firewall() {
log_info "Configuring firewall..."
ufw allow 'Nginx Full' 2>/dev/null || true
ufw allow 80/tcp comment "HTTP"
ufw allow 443/tcp comment "HTTPS"
log "Firewall configured ✓"
}
# Reload Nginx
reload_nginx() {
log_info "Reloading Nginx..."
systemctl reload nginx
if systemctl is-active --quiet nginx; then
log "Nginx reloaded successfully ✓"
else
log_error "Nginx failed to start!"
systemctl status nginx
exit 1
fi
}
# Enable Nginx on boot
enable_nginx() {
log_info "Enabling Nginx on boot..."
systemctl enable nginx
log "Nginx enabled ✓"
}
# Test proxying
test_proxy() {
log_info "Testing reverse proxy..."
sleep 2
# Test local access
if curl -f -s http://localhost/api/health > /dev/null 2>&1; then
log "Local proxy test passed ✓"
else
log_error "Local proxy test failed!"
fi
# Test external access
if curl -f -s http://45.61.58.125/api/health > /dev/null 2>&1; then
log "External proxy test passed ✓"
else
log_error "External proxy test failed!"
fi
}
# Show status
show_status() {
echo ""
log "${GREEN}╔════════════════════════════════════════════════════════════════╗${NC}"
log "${GREEN}║ NGINX SETUP COMPLETE! ║${NC}"
log "${GREEN}╚════════════════════════════════════════════════════════════════╝${NC}"
echo ""
log_info "Nginx Status:"
systemctl status nginx --no-pager | head -10
echo ""
log_info "Access URLs:"
echo " HTTP: http://45.61.58.125/"
echo " API: http://45.61.58.125/api/watches"
echo " Health: http://45.61.58.125/api/health"
echo ""
log_info "Useful Commands:"
echo " Test config: nginx -t"
echo " Reload: systemctl reload nginx"
echo " View logs: tail -f /var/log/nginx/omega-watches-access.log"
echo " Error logs: tail -f /var/log/nginx/omega-watches-error.log"
echo ""
}
# Main function
main() {
print_header
check_nginx
create_cache_dir
backup_config
install_config
test_config
configure_firewall
reload_nginx
enable_nginx
test_proxy
show_status
}
# Run main
main "$@"