#!/usr/bin/env bash # Install and configure Nginx on RPC containers (2500-2502) with SSL on port 443 # Usage: ./install-nginx-rpc.sh [vmid1] [vmid2] [vmid3] # If no VMIDs provided, defaults to 2500, 2501, 2502 set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" PROXMOX_HOST="${PROXMOX_HOST:-192.168.11.10}" # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' # No Color info() { echo -e "${GREEN}[INFO]${NC} $1"; } warn() { echo -e "${YELLOW}[WARN]${NC} $1"; } error() { echo -e "${RED}[ERROR]${NC} $1"; } # Get VMIDs (default to 2500-2502) if [[ $# -eq 0 ]]; then VMIDS=(2500 2501 2502) else VMIDS=("$@") fi # RPC container mapping declare -A RPC_IPS=( [2500]="192.168.11.250" [2501]="192.168.11.251" [2502]="192.168.11.252" ) declare -A RPC_HOSTNAMES=( [2500]="besu-rpc-1" [2501]="besu-rpc-2" [2502]="besu-rpc-3" ) # Domain mappings for each container declare -A RPC_HTTP_DOMAINS=( [2501]="rpc-http-pub.d-bis.org" [2502]="rpc-http-prv.d-bis.org" ) declare -A RPC_WS_DOMAINS=( [2501]="rpc-ws-pub.d-bis.org" [2502]="rpc-ws-prv.d-bis.org" ) info "Installing Nginx on RPC containers..." info "Proxmox Host: $PROXMOX_HOST" info "Containers: ${VMIDS[*]}" echo "" # Function to install Nginx on a container install_nginx_on_container() { local vmid=$1 local ip="${RPC_IPS[$vmid]}" local hostname="${RPC_HOSTNAMES[$vmid]}" local http_domain="${RPC_HTTP_DOMAINS[$vmid]:-}" local ws_domain="${RPC_WS_DOMAINS[$vmid]:-}" echo "==========================================" info "Processing VMID $vmid ($hostname - $ip)" echo "==========================================" # Check if container is running STATUS=$(ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no root@${PROXMOX_HOST} \ "pct status $vmid 2>/dev/null | awk '{print \$2}'" 2>/dev/null || echo "unknown") if [[ "$STATUS" != "running" ]]; then warn "Container $vmid is not running (status: $STATUS), skipping..." return 1 fi # Check if Nginx is already installed if ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no root@${PROXMOX_HOST} \ "pct exec $vmid -- which nginx >/dev/null 2>&1"; then warn "Nginx is already installed on VMID $vmid" read -p "Reinstall/update configuration? (y/N): " -n 1 -r echo if [[ ! $REPLY =~ ^[Yy]$ ]]; then info "Skipping VMID $vmid" return 0 fi fi # Install Nginx info "Installing Nginx on VMID $vmid..." ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no root@${PROXMOX_HOST} \ "pct exec $vmid -- bash -c ' export DEBIAN_FRONTEND=noninteractive apt-get update -qq apt-get install -y -qq nginx openssl '" || { error "Failed to install Nginx on VMID $vmid" return 1 } info "✓ Nginx installed" # Generate self-signed SSL certificate (or use Let's Encrypt later) info "Generating SSL certificate..." # Use first domain if available, otherwise use hostname local cert_cn="${http_domain:-$hostname}" ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no root@${PROXMOX_HOST} \ "pct exec $vmid -- bash -c ' mkdir -p /etc/nginx/ssl openssl req -x509 -nodes -days 3650 -newkey rsa:2048 \\ -keyout /etc/nginx/ssl/rpc.key \\ -out /etc/nginx/ssl/rpc.crt \\ -subj \"/CN=$cert_cn/O=RPC Node/C=US\" 2>/dev/null chmod 600 /etc/nginx/ssl/rpc.key chmod 644 /etc/nginx/ssl/rpc.crt '" || { error "Failed to generate SSL certificate" return 1 } info "✓ SSL certificate generated for $cert_cn" # Create Nginx configuration info "Creating Nginx configuration..." # Build server_name list local server_names="$hostname $ip" if [[ -n "$http_domain" ]]; then server_names="$server_names $http_domain" fi if [[ -n "$ws_domain" ]]; then server_names="$server_names $ws_domain" fi ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no root@${PROXMOX_HOST} \ "pct exec $vmid -- bash" < /etc/nginx/sites-available/rpc </dev/null 2>&1"; then info "✓ Nginx service is active" else error "Nginx service is not active" return 1 fi # Check if port 443 is listening if ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no root@${PROXMOX_HOST} \ "pct exec $vmid -- ss -tuln | grep -q ':443'"; then info "✓ Port 443 is listening" else warn "Port 443 may not be listening" fi echo "" return 0 } # Install on each container SUCCESS=0 FAILED=0 for vmid in "${VMIDS[@]}"; do if install_nginx_on_container "$vmid"; then ((SUCCESS++)) else ((FAILED++)) fi echo "" done # Summary echo "==========================================" info "Installation Summary:" echo " Success: $SUCCESS" echo " Failed: $FAILED" echo " Total: ${#VMIDS[@]}" echo "==========================================" if [[ $FAILED -gt 0 ]]; then exit 1 fi info "Nginx installation complete!" echo "" info "Next steps:" echo " 1. Test HTTPS: curl -k https://:443" echo " 2. Test RPC: curl -k -X POST https://:443 -H 'Content-Type: application/json' -d '{\"jsonrpc\":\"2.0\",\"method\":\"eth_blockNumber\",\"params\":[],\"id\":1}'" echo " 3. Replace self-signed certificate with Let's Encrypt if needed" echo " 4. Configure DNS records to point to container IPs"