#!/bin/bash # Fix Forgejo configuration issues # Usage: ./fix-forgejo-config.sh 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 print_status() { echo -e "${GREEN}[INFO]${NC} $1" } print_warning() { echo -e "${YELLOW}[WARNING]${NC} $1" } print_error() { echo -e "${RED}[ERROR]${NC} $1" } print_success() { echo -e "${GREEN}[SUCCESS]${NC} $1" } print_info() { echo -e "${BLUE}[INFO]${NC} $1" } echo "🔧 Fixing Forgejo configuration issues..." # Find Forgejo config file print_status "Searching for Forgejo configuration file..." FORGEJO_CONFIG="" # Common locations for Forgejo/Gitea config CONFIG_PATHS=( "/etc/forgejo/app.ini" "/etc/gitea/app.ini" "/home/forgejo/custom/conf/app.ini" "/home/gitea/custom/conf/app.ini" "/var/lib/forgejo/custom/conf/app.ini" "/var/lib/gitea/custom/conf/app.ini" "/opt/forgejo/custom/conf/app.ini" "/opt/gitea/custom/conf/app.ini" "/usr/local/etc/forgejo/app.ini" "/usr/local/etc/gitea/app.ini" ) for config_path in "${CONFIG_PATHS[@]}"; do if [ -f "$config_path" ]; then FORGEJO_CONFIG="$config_path" print_success "Found Forgejo config at: $FORGEJO_CONFIG" break fi done # If not found, try to find any app.ini file if [ -z "$FORGEJO_CONFIG" ]; then print_warning "Config file not found in standard locations, searching everywhere..." FORGEJO_CONFIG=$(find /etc /home /var /opt /usr -name "app.ini" 2>/dev/null | head -1) if [ -n "$FORGEJO_CONFIG" ]; then print_success "Found config file at: $FORGEJO_CONFIG" fi fi if [ -z "$FORGEJO_CONFIG" ]; then print_error "Could not find Forgejo/Gitea configuration file!" print_info "Please check if Forgejo is installed and find the app.ini file manually" exit 1 fi # Show current ROOT_URL print_status "Checking current ROOT_URL..." CURRENT_ROOT_URL=$(grep -E "^ROOT_URL" "$FORGEJO_CONFIG" | cut -d'=' -f2 | tr -d ' ') print_info "Current ROOT_URL: $CURRENT_ROOT_URL" # Backup config BACKUP_FILE="${FORGEJO_CONFIG}.backup.$(date +%Y%m%d_%H%M%S)" cp "$FORGEJO_CONFIG" "$BACKUP_FILE" print_success "Config backed up to: $BACKUP_FILE" # Update ROOT_URL print_status "Updating ROOT_URL to https://code.mnemo-cards.online..." sed -i 's|^ROOT_URL.*=.*|ROOT_URL = https://code.mnemo-cards.online|' "$FORGEJO_CONFIG" # Update HTTP_ADDR to listen only on localhost (for security) print_status "Updating HTTP_ADDR to listen only on localhost..." if grep -q "^HTTP_ADDR" "$FORGEJO_CONFIG"; then sed -i 's|^HTTP_ADDR.*=.*|HTTP_ADDR = 127.0.0.1|' "$FORGEJO_CONFIG" else # Add HTTP_ADDR if not present sed -i '/^\[server\]/a HTTP_ADDR = 127.0.0.1' "$FORGEJO_CONFIG" fi # Verify changes NEW_ROOT_URL=$(grep -E "^ROOT_URL" "$FORGEJO_CONFIG" | cut -d'=' -f2 | tr -d ' ') NEW_HTTP_ADDR=$(grep -E "^HTTP_ADDR" "$FORGEJO_CONFIG" | cut -d'=' -f2 | tr -d ' ') if [ "$NEW_ROOT_URL" = "https://code.mnemo-cards.online" ]; then print_success "ROOT_URL updated successfully: $NEW_ROOT_URL" else print_error "Failed to update ROOT_URL!" exit 1 fi if [ "$NEW_HTTP_ADDR" = "127.0.0.1" ]; then print_success "HTTP_ADDR updated successfully: $NEW_HTTP_ADDR (localhost only)" else print_error "Failed to update HTTP_ADDR!" exit 1 fi # Show the full [server] section for verification print_status "Current [server] section in config:" echo "----------------------------------------" sed -n '/^\[server\]/,/^\[/p' "$FORGEJO_CONFIG" | grep -E "(ROOT_URL|HTTP_ADDR|DOMAIN|HTTP_PORT|ROOT_PATH)" echo "----------------------------------------" # Restart Forgejo/Gitea service print_status "Restarting Forgejo/Gitea service..." if systemctl is-active --quiet forgejo 2>/dev/null; then sudo systemctl restart forgejo print_success "Forgejo service restarted" elif systemctl is-active --quiet gitea 2>/dev/null; then sudo systemctl restart gitea print_success "Gitea service restarted" else print_warning "No active Forgejo/Gitea service found via systemctl" # Try to find and restart manually FORGEJO_PID=$(pgrep -f "forgejo web" || pgrep -f "gitea web") if [ -n "$FORGEJO_PID" ]; then print_info "Found running Forgejo process (PID: $FORGEJO_PID), restarting..." sudo kill $FORGEJO_PID sleep 2 # Try to start via systemctl anyway sudo systemctl start forgejo 2>/dev/null || sudo systemctl start gitea 2>/dev/null || print_warning "Could not restart service automatically" else print_warning "No running Forgejo process found" fi fi # Apply updated nginx configuration print_status "Applying updated nginx configuration..." print_info "Script directory: $SCRIPT_DIR" print_info "Looking for nginx config: $SCRIPT_DIR/forgejo-nginx.conf" if [ -f "$SCRIPT_DIR/forgejo-nginx.conf" ]; then print_success "Nginx config file found" if [ -f "/etc/nginx/sites-available/forgejo" ]; then print_status "Updating existing nginx config..." sudo cp "$SCRIPT_DIR/forgejo-nginx.conf" "/etc/nginx/sites-available/forgejo" print_success "Nginx config updated" else print_status "Installing new nginx config..." sudo cp "$SCRIPT_DIR/forgejo-nginx.conf" "/etc/nginx/sites-available/forgejo" sudo ln -sf "/etc/nginx/sites-available/forgejo" "/etc/nginx/sites-enabled/forgejo" print_success "Nginx config installed" fi else print_error "Nginx config file not found at: $SCRIPT_DIR/forgejo-nginx.conf" print_error "Please ensure forgejo-nginx.conf exists in the same directory as this script" exit 1 fi # Check nginx configuration and reload print_status "Checking nginx configuration..." if sudo nginx -t 2>/dev/null; then print_success "Nginx configuration is valid" print_status "Reloading nginx..." sudo systemctl reload nginx print_success "Nginx reloaded" else print_error "Nginx configuration has errors!" sudo nginx -t fi print_success "Forgejo configuration fix completed!" print_info "Please wait 30 seconds for services to fully restart, then test:" print_info "- Domain access: https://code.mnemo-cards.online" print_info "- IP access should be blocked: https://147.45.152.129" print_info "- No more ROOT_URL warnings"