#!/bin/bash # Script to check and fix nginx configuration and service set -e SERVER_IP="147.45.152.129" SERVER_USER="root" echo "๐Ÿ”ง Checking and fixing nginx..." echo "" ssh "$SERVER_USER@$SERVER_IP" << 'ENDSSH' set -e echo "๐Ÿ“Š Checking nginx installation..." if /usr/sbin/nginx -v 2>&1; then echo "โœ… nginx is installed" else echo "โŒ nginx is NOT installed" exit 1 fi echo "" echo "๐Ÿงน Checking for port conflicts..." # Check if ports 80 and 443 are free if ss -tlnp | grep -q ":80 "; then echo "โš ๏ธ Port 80 is already in use:" ss -tlnp | grep ":80 " echo "Attempting to free port 80..." # Kill processes listening on port 80 (except nginx) ss -tlnp | grep ":80 " | grep -v nginx | awk '{print $6}' | cut -d'=' -f2 | cut -d',' -f1 | xargs -r kill -9 2>/dev/null || true sleep 1 fi if ss -tlnp | grep -q ":443 "; then echo "โš ๏ธ Port 443 is already in use:" ss -tlnp | grep ":443 " echo "Attempting to free port 443..." # Kill processes listening on port 443 (except nginx) ss -tlnp | grep ":443 " | grep -v nginx | awk '{print $6}' | cut -d'=' -f2 | cut -d',' -f1 | xargs -r kill -9 2>/dev/null || true sleep 1 fi echo "" echo "๐Ÿงน Checking for orphaned nginx processes..." NGINX_PIDS=$(pgrep -f nginx | wc -l) if [ "$NGINX_PIDS" -gt 0 ]; then echo "โš ๏ธ Found $NGINX_PIDS nginx processes running" if ! systemctl is-active --quiet nginx; then echo "Systemd thinks nginx is stopped, but processes exist. Cleaning up..." pkill -9 nginx sleep 2 fi fi echo "" echo "๐Ÿ“Š Testing nginx configuration..." if /usr/sbin/nginx -t; then echo "โœ… Configuration syntax is valid" else echo "โŒ Configuration syntax error!" echo "Checking for common issues..." # Check for missing events block if ! grep -q "events {" /etc/nginx/nginx.conf; then echo "โš ๏ธ Missing events block in nginx.conf" fi # Check for server blocks outside http if grep -q "^server {" /etc/nginx/nginx.conf; then echo "โš ๏ธ Found server block outside http context in nginx.conf" fi # Check for duplicate directives DUPLICATE_USER=$(grep "^user " /etc/nginx/nginx.conf | wc -l) if [ "$DUPLICATE_USER" -gt 1 ]; then echo "โš ๏ธ Found $DUPLICATE_USER 'user' directives (should be 1)" fi echo "Please fix the configuration errors and try again" exit 1 fi echo "" echo "๐Ÿ“Š Checking nginx service status..." if systemctl is-active --quiet nginx; then echo "โœ… nginx service is running" systemctl status nginx --no-pager | head -5 echo "" echo "๐Ÿ”„ Reloading nginx configuration..." if systemctl reload nginx; then echo "โœ… nginx reloaded successfully" else echo "โŒ nginx reload failed, restarting..." systemctl restart nginx fi else echo "โš ๏ธ nginx service is not running, starting..." if systemctl start nginx; then echo "โœ… nginx started successfully" else echo "โŒ nginx failed to start!" systemctl status nginx --no-pager exit 1 fi fi echo "" echo "๐Ÿ“Š Checking if nginx is active..." if systemctl is-active --quiet nginx; then echo "โœ… nginx is active" else echo "โŒ nginx is not active" exit 1 fi echo "" echo "๐Ÿ“Š Checking site configurations..." # Check all expected configurations SITES=("vscode.mnemo-cards.online" "code.mnemo-cards.online" "mnemo-cards.online") for site in "${SITES[@]}"; do if [ -f "/etc/nginx/sites-enabled/$site" ]; then echo "โœ… $site config is enabled" elif [ -f "/etc/nginx/sites-available/$site" ]; then echo "โš ๏ธ $site config exists but not enabled" echo "Enabling $site..." ln -sf "/etc/nginx/sites-available/$site" "/etc/nginx/sites-enabled/" /usr/sbin/nginx -t && systemctl reload nginx echo "โœ… $site enabled and reloaded" else echo "โŒ $site config not found" fi done echo "" echo "๐Ÿ“Š Checking SSL certificates..." if [ -d "/etc/letsencrypt/live/mnemo-cards.online" ]; then echo "โœ… Let's Encrypt certificates found for mnemo-cards.online" # Ensure correct permissions chmod 755 /etc/letsencrypt/archive 2>/dev/null || true chmod 755 /etc/letsencrypt/live 2>/dev/null || true find /etc/letsencrypt -type d -exec chmod 755 {} \; 2>/dev/null || true find /etc/letsencrypt -type f -exec chmod 644 {} \; 2>/dev/null || true chmod 600 /etc/letsencrypt/archive/mnemo-cards.online/privkey*.pem 2>/dev/null || true chmod 600 /etc/letsencrypt/live/mnemo-cards.online/privkey.pem 2>/dev/null || true else echo "โš ๏ธ Let's Encrypt certificates not found" fi echo "" echo "๐Ÿ“Š Checking DNS resolution..." DOMAINS=("code.mnemo-cards.online" "vscode.mnemo-cards.online" "mnemo-cards.online") for domain in "${DOMAINS[@]}"; do if host "$domain" &> /dev/null; then echo "โœ… $domain resolves correctly" else echo "โš ๏ธ DNS resolution failed for $domain" fi done echo "" echo "๐Ÿ“Š Testing HTTP responses..." for domain in "${DOMAINS[@]}"; do if curl -I -k "https://$domain/" --max-time 10 --silent | head -1 | grep -q "200\|301\|302"; then echo "โœ… $domain responds correctly" else echo "โš ๏ธ $domain not responding or returning error" fi done ENDSSH echo "" echo "โœ… Done!" echo "" echo "๐ŸŒ Test these URLs in your browser:" echo " https://mnemo-cards.online/ (main web app)" echo " https://code.mnemo-cards.online/ (Forgejo)" echo " https://vscode.mnemo-cards.online/ (VSCode Server)" echo "" echo "๐Ÿ”‘ Password: AGktOidxrah1KVC0"