# Blockscout Configuration - Run Commands

**Copy and paste these commands into your SSH session (root@192.168.11.140)**

---

## Complete Configuration Script

```bash
# Complete Blockscout Setup - Copy everything below this line
set -e

CHAIN_ID=138
RPC_URL="http://192.168.11.250:8545"
WS_URL="ws://192.168.11.250:8546"
BLOCKSCOUT_HOST="192.168.11.140"

echo "════════════════════════════════════════════════════════"
echo "Configuring Blockscout..."
echo "════════════════════════════════════════════════════════"

# Step 1: Check Docker
systemctl start docker 2>/dev/null || true
systemctl enable docker 2>/dev/null || true

# Step 2: Setup directory
if [ -d /opt/blockscout ]; then
    cd /opt/blockscout
elif [ -d /root/blockscout ]; then
    cd /root/blockscout
else
    mkdir -p /opt/blockscout && cd /opt/blockscout
fi

# Step 3: Create docker-compose.yml
cat > docker-compose.yml <<'EOF'
version: '3.8'

services:
  postgres:
    image: postgres:15-alpine
    container_name: blockscout-postgres
    environment:
      POSTGRES_USER: blockscout
      POSTGRES_PASSWORD: blockscout
      POSTGRES_DB: blockscout
    volumes:
      - postgres-data:/var/lib/postgresql/data
    restart: unless-stopped
    networks:
      - blockscout-network
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U blockscout"]
      interval: 10s
      timeout: 5s
      retries: 5

  blockscout:
    image: blockscout/blockscout:latest
    container_name: blockscout
    depends_on:
      postgres:
        condition: service_healthy
    environment:
      - DATABASE_URL=postgresql://blockscout:blockscout@postgres:5432/blockscout
      - ETHEREUM_JSONRPC_HTTP_URL=http://192.168.11.250:8545
      - ETHEREUM_JSONRPC_WS_URL=ws://192.168.11.250:8546
      - ETHEREUM_JSONRPC_TRACE_URL=http://192.168.11.250:8545
      - ETHEREUM_JSONRPC_VARIANT=besu
      - CHAIN_ID=138
      - COIN=ETH
      - BLOCKSCOUT_HOST=192.168.11.140
      - BLOCKSCOUT_PROTOCOL=http
      - SECRET_KEY_BASE=PLACEHOLDER
      - POOL_SIZE=10
      - ECTO_USE_SSL=false
    ports:
      - "4000:4000"
    volumes:
      - blockscout-data:/app/apps/explorer/priv/static
    restart: unless-stopped
    networks:
      - blockscout-network

volumes:
  postgres-data:
  blockscout-data:

networks:
  blockscout-network:
    driver: bridge
EOF

SECRET_KEY=$(openssl rand -hex 64)
sed -i "s|SECRET_KEY_BASE=PLACEHOLDER|SECRET_KEY_BASE=${SECRET_KEY}|" docker-compose.yml

# Step 4: Start services
docker-compose down 2>/dev/null || docker compose down 2>/dev/null || true
docker-compose up -d postgres || docker compose up -d postgres

echo "Waiting for PostgreSQL..."
for i in {1..30}; do
    docker exec blockscout-postgres pg_isready -U blockscout >/dev/null 2>&1 && break
    sleep 2
done

docker-compose up -d blockscout || docker compose up -d blockscout

# Step 5: Configure Nginx
apt-get update -qq && apt-get install -y -qq nginx >/dev/null 2>&1

cat > /etc/nginx/sites-available/blockscout <<'EOF'
server {
    listen 80;
    server_name 192.168.11.140 explorer.d-bis.org;
    location / {
        proxy_pass http://localhost:4000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_read_timeout 300s;
    }
}
EOF

ln -sf /etc/nginx/sites-available/blockscout /etc/nginx/sites-enabled/blockscout
rm -f /etc/nginx/sites-enabled/default
nginx -t && systemctl reload nginx
systemctl enable nginx && systemctl start nginx

# Step 6: Check status
sleep 5
docker ps
echo ""
echo "✓ Configuration complete!"
echo "Access: http://192.168.11.140 or https://explorer.d-bis.org"
```

---

**Copy the script above and paste it into your SSH session to container.**

