Skip to content

ServerKit is a lightweight, modern server control panel for managing web applications, databases, and services on your VPS or dedicated server. Built with Python/Flask backend and React frontend.

Add multi-site support with host nginx reverse proxy

Juan Deniseebb37fd5b97
diff --git a/docker-compose.yml b/docker-compose.yml
index 9a19522a..d84d526d 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -4,10 +4,16 @@
# The backend runs directly on the host (not in Docker)
# for full system access. Only the frontend runs in Docker.
#
+# Architecture:
+# - Host nginx (port 80/443) routes domains to apps
+# - ServerKit frontend (port 8080) serves the UI
+# - ServerKit backend (port 5000) provides the API
+#
# Usage:
# 1. Copy .env.example to .env and configure
# 2. Start backend: sudo systemctl start serverkit
# 3. Start frontend: docker compose up -d
+# 4. Configure host nginx to route your domain
# ============================================
services:
@@ -17,8 +23,8 @@ services:
dockerfile: Dockerfile
container_name: serverkit-frontend
ports:
- - "${PORT:-80}:80"
- - "${SSL_PORT:-443}:443"
+ # Use 8080 internally - host nginx handles 80/443 and routes by domain
+ - "127.0.0.1:8080:80"
extra_hosts:
# Allows container to reach the host machine where backend runs
- "host.docker.internal:host-gateway"
diff --git a/install.sh b/install.sh
index 4a88d917..14538cb9 100644
--- a/install.sh
+++ b/install.sh
@@ -188,6 +188,32 @@ ln -sf "$INSTALL_DIR/serverkit" /usr/local/bin/serverkit
print_success "CLI installed"
+# Install and configure host nginx as reverse proxy
+print_info "Setting up nginx reverse proxy..."
+apt-get install -y nginx
+
+# Stop nginx and remove default site
+systemctl stop nginx 2>/dev/null || true
+rm -f /etc/nginx/sites-enabled/default
+
+# Ensure sites directories exist
+mkdir -p /etc/nginx/sites-available
+mkdir -p /etc/nginx/sites-enabled
+
+# Ensure nginx.conf includes sites-enabled
+if ! grep -q "sites-enabled" /etc/nginx/nginx.conf; then
+ sed -i '/http {/a \ include /etc/nginx/sites-enabled/*;' /etc/nginx/nginx.conf
+fi
+
+# Install ServerKit site config
+cp "$INSTALL_DIR/nginx/sites-available/serverkit.conf" /etc/nginx/sites-available/
+ln -sf /etc/nginx/sites-available/serverkit.conf /etc/nginx/sites-enabled/
+
+# Copy site template
+cp "$INSTALL_DIR/nginx/sites-available/example.conf.template" /etc/nginx/sites-available/
+
+print_success "Nginx proxy configured"
+
# Build and start frontend container
print_info "Building frontend container..."
cd "$INSTALL_DIR"
@@ -201,6 +227,10 @@ systemctl start serverkit
# Start frontend (Docker)
docker compose up -d
+# Start nginx
+systemctl start nginx
+systemctl enable nginx
+
# Wait for services to start
print_info "Waiting for services to start..."
sleep 10
diff --git a/nginx/sites-available/example.conf.template b/nginx/sites-available/example.conf.template
new file mode 100644
index 00000000..b56eb9d7
--- /dev/null
+++ b/nginx/sites-available/example.conf.template
@@ -0,0 +1,46 @@
+# Example Site Configuration Template
+# Copy this file and modify for each new domain/subdomain
+#
+# Usage:
+# 1. Copy: sudo cp example.conf.template /etc/nginx/sites-available/mysite.conf
+# 2. Edit: sudo nano /etc/nginx/sites-available/mysite.conf
+# 3. Enable: sudo ln -s /etc/nginx/sites-available/mysite.conf /etc/nginx/sites-enabled/
+# 4. Test: sudo nginx -t
+# 5. Reload: sudo systemctl reload nginx
+
+server {
+ listen 80;
+ server_name example.builditdesign.com; # Change this to your domain
+
+ # Option 1: Proxy to a local app (Docker container, Node.js, etc.)
+ location / {
+ proxy_pass http://127.0.0.1:3000; # Change port to your app's port
+ proxy_http_version 1.1;
+ 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_set_header Upgrade $http_upgrade;
+ proxy_set_header Connection "upgrade";
+ }
+
+ # Option 2: Serve static files (uncomment and modify)
+ # root /var/www/example.builditdesign.com;
+ # index index.html index.htm;
+ # location / {
+ # try_files $uri $uri/ =404;
+ # }
+
+ # Option 3: PHP site (uncomment and modify)
+ # root /var/www/example.builditdesign.com;
+ # index index.php index.html;
+ # location / {
+ # try_files $uri $uri/ /index.php?$query_string;
+ # }
+ # location ~ \.php$ {
+ # fastcgi_pass unix:/run/php/php8.2-fpm.sock;
+ # fastcgi_index index.php;
+ # fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
+ # include fastcgi_params;
+ # }
+}
diff --git a/nginx/sites-available/serverkit.conf b/nginx/sites-available/serverkit.conf
new file mode 100644
index 00000000..82ab3bd8
--- /dev/null
+++ b/nginx/sites-available/serverkit.conf
@@ -0,0 +1,49 @@
+# ServerKit - Main Application
+# /etc/nginx/sites-available/serverkit.conf
+#
+# This is the default server block for ServerKit.
+# Update server_name to your domain, or leave as _ for catch-all.
+
+server {
+ listen 80 default_server;
+ server_name _;
+
+ # Redirect to HTTPS (handled by Cloudflare in Flexible mode)
+ # If using Full SSL mode, uncomment the redirect below
+ # return 301 https://$server_name$request_uri;
+
+ location / {
+ proxy_pass http://127.0.0.1:8080;
+ proxy_http_version 1.1;
+ 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;
+ }
+
+ location /api {
+ proxy_pass http://127.0.0.1:5000;
+ proxy_http_version 1.1;
+ 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_connect_timeout 60s;
+ proxy_send_timeout 60s;
+ proxy_read_timeout 60s;
+ }
+
+ location /socket.io {
+ proxy_pass http://127.0.0.1:5000;
+ 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_connect_timeout 7d;
+ proxy_send_timeout 7d;
+ proxy_read_timeout 7d;
+ }
+}
diff --git a/scripts/setup-nginx-proxy.sh b/scripts/setup-nginx-proxy.sh
new file mode 100644
index 00000000..d9990a93
--- /dev/null
+++ b/scripts/setup-nginx-proxy.sh
@@ -0,0 +1,120 @@
+#!/bin/bash
+#
+# Setup Host Nginx as Reverse Proxy
+# This allows multiple domains/apps on one server
+#
+# Usage: sudo ./scripts/setup-nginx-proxy.sh
+#
+
+set -e
+
+# Colors
+RED='\033[0;31m'
+GREEN='\033[0;32m'
+YELLOW='\033[1;33m'
+BLUE='\033[0;34m'
+NC='\033[0m'
+
+print_success() { echo -e "${GREEN}✓ $1${NC}"; }
+print_error() { echo -e "${RED}✗ $1${NC}"; }
+print_warning() { echo -e "${YELLOW}! $1${NC}"; }
+print_info() { echo -e "${BLUE}→ $1${NC}"; }
+
+# Check if running as root
+if [ "$EUID" -ne 0 ]; then
+ print_error "Please run as root (sudo)"
+ exit 1
+fi
+
+echo -e "${BLUE}"
+echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
+echo " Setting up Host Nginx as Reverse Proxy"
+echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
+echo -e "${NC}"
+
+# Install nginx if not present
+if ! command -v nginx &> /dev/null; then
+ print_info "Installing nginx..."
+ apt-get update
+ apt-get install -y nginx
+ print_success "Nginx installed"
+else
+ print_success "Nginx already installed"
+fi
+
+# Stop nginx temporarily
+systemctl stop nginx 2>/dev/null || true
+
+# Backup default config
+if [ -f /etc/nginx/sites-enabled/default ]; then
+ print_info "Removing default nginx site..."
+ rm -f /etc/nginx/sites-enabled/default
+fi
+
+# Create sites directories if they don't exist
+mkdir -p /etc/nginx/sites-available
+mkdir -p /etc/nginx/sites-enabled
+
+# Check if nginx.conf includes sites-enabled
+if ! grep -q "sites-enabled" /etc/nginx/nginx.conf; then
+ print_info "Updating nginx.conf to include sites-enabled..."
+ # Add include directive before the last closing brace
+ sed -i '/^http {/a \ include /etc/nginx/sites-enabled/*;' /etc/nginx/nginx.conf
+fi
+
+# Copy ServerKit site config
+SERVERKIT_DIR="/opt/serverkit"
+if [ -f "$SERVERKIT_DIR/nginx/sites-available/serverkit.conf" ]; then
+ print_info "Installing ServerKit nginx config..."
+ cp "$SERVERKIT_DIR/nginx/sites-available/serverkit.conf" /etc/nginx/sites-available/
+ ln -sf /etc/nginx/sites-available/serverkit.conf /etc/nginx/sites-enabled/
+ print_success "ServerKit config installed"
+fi
+
+# Copy template
+if [ -f "$SERVERKIT_DIR/nginx/sites-available/example.conf.template" ]; then
+ cp "$SERVERKIT_DIR/nginx/sites-available/example.conf.template" /etc/nginx/sites-available/
+ print_success "Template copied to /etc/nginx/sites-available/"
+fi
+
+# Update ServerKit docker-compose to use port 8080
+cd "$SERVERKIT_DIR"
+if docker compose ps | grep -q "serverkit-frontend"; then
+ print_info "Updating ServerKit frontend to use port 8080..."
+ docker compose down
+ docker compose up -d
+fi
+
+# Test nginx config
+print_info "Testing nginx configuration..."
+if nginx -t; then
+ print_success "Nginx configuration is valid"
+else
+ print_error "Nginx configuration has errors"
+ exit 1
+fi
+
+# Start nginx
+systemctl start nginx
+systemctl enable nginx
+
+echo ""
+echo -e "${GREEN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
+echo -e "${GREEN} Setup Complete!${NC}"
+echo -e "${GREEN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
+echo ""
+echo "Architecture:"
+echo " • Host nginx (ports 80/443) - routes domains to apps"
+echo " • ServerKit frontend (port 8080) - UI"
+echo " • ServerKit backend (port 5000) - API"
+echo ""
+echo "To add a new site:"
+echo " 1. Create DNS record in Cloudflare (A record → your server IP)"
+echo " 2. Run: sudo serverkit add-site example.builditdesign.com"
+echo " Or manually:"
+echo " sudo cp /etc/nginx/sites-available/example.conf.template \\"
+echo " /etc/nginx/sites-available/mysite.conf"
+echo " sudo nano /etc/nginx/sites-available/mysite.conf"
+echo " sudo ln -s /etc/nginx/sites-available/mysite.conf /etc/nginx/sites-enabled/"
+echo " sudo nginx -t && sudo systemctl reload nginx"
+echo ""
diff --git a/serverkit b/serverkit
index ac02cc62..106f23d7 100644
--- a/serverkit
+++ b/serverkit
@@ -87,6 +87,12 @@ cmd_help() {
echo " backup-db Backup the database"
echo " restore-db Restore database from backup"
echo ""
+ echo "Site Management:"
+ echo " setup-proxy Setup host nginx as reverse proxy (run once)"
+ echo " add-site Add a new site/domain"
+ echo " list-sites List all configured sites"
+ echo " remove-site Remove a site"
+ echo ""
echo "Utility Commands:"
echo " generate-keys Generate secure keys for .env"
echo " config Edit configuration"
@@ -94,6 +100,7 @@ cmd_help() {
echo "Examples:"
echo " sudo serverkit start"
echo " sudo serverkit create-admin"
+ echo " sudo serverkit add-site test.mysite.com 3000"
echo " serverkit logs backend"
echo ""
}
@@ -413,6 +420,141 @@ cmd_config() {
print_info "Restart ServerKit to apply changes: sudo serverkit restart"
}
+cmd_setup_proxy() {
+ check_root
+ check_installed
+ print_info "Setting up host nginx as reverse proxy..."
+
+ if [ -f "$INSTALL_DIR/scripts/setup-nginx-proxy.sh" ]; then
+ chmod +x "$INSTALL_DIR/scripts/setup-nginx-proxy.sh"
+ "$INSTALL_DIR/scripts/setup-nginx-proxy.sh"
+ else
+ print_error "Setup script not found. Run 'serverkit update' first."
+ exit 1
+ fi
+}
+
+cmd_add_site() {
+ check_root
+
+ DOMAIN="$1"
+ PORT="${2:-3000}"
+
+ if [ -z "$DOMAIN" ]; then
+ echo "Usage: serverkit add-site <domain> [port]"
+ echo ""
+ echo "Examples:"
+ echo " serverkit add-site test.builditdesign.com"
+ echo " serverkit add-site myapp.builditdesign.com 3000"
+ exit 1
+ fi
+
+ SITE_FILE="/etc/nginx/sites-available/${DOMAIN}.conf"
+
+ if [ -f "$SITE_FILE" ]; then
+ print_warning "Site config already exists: $SITE_FILE"
+ read -p "Overwrite? (y/N) " -n 1 -r
+ echo
+ if [[ ! $REPLY =~ ^[Yy]$ ]]; then
+ exit 0
+ fi
+ fi
+
+ print_info "Creating site config for $DOMAIN..."
+
+ cat > "$SITE_FILE" << EOF
+# Site: $DOMAIN
+# Created: $(date)
+
+server {
+ listen 80;
+ server_name $DOMAIN;
+
+ location / {
+ proxy_pass http://127.0.0.1:$PORT;
+ proxy_http_version 1.1;
+ 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_set_header Upgrade \$http_upgrade;
+ proxy_set_header Connection "upgrade";
+ }
+}
+EOF
+
+ # Enable the site
+ ln -sf "$SITE_FILE" /etc/nginx/sites-enabled/
+
+ # Test and reload nginx
+ if nginx -t 2>/dev/null; then
+ systemctl reload nginx
+ print_success "Site added: $DOMAIN → localhost:$PORT"
+ echo ""
+ echo "Next steps:"
+ echo " 1. Add DNS record in Cloudflare:"
+ echo " Type: A, Name: ${DOMAIN%%.*}, Content: $(curl -s ifconfig.me)"
+ echo " 2. Start your app on port $PORT"
+ echo ""
+ else
+ print_error "Nginx config error. Check: $SITE_FILE"
+ rm -f "/etc/nginx/sites-enabled/${DOMAIN}.conf"
+ exit 1
+ fi
+}
+
+cmd_list_sites() {
+ print_header
+ echo ""
+ echo "Configured Sites:"
+ echo "─────────────────────────────────────────"
+
+ if [ -d /etc/nginx/sites-enabled ]; then
+ for site in /etc/nginx/sites-enabled/*.conf; do
+ if [ -f "$site" ]; then
+ DOMAIN=$(grep -m1 "server_name" "$site" | awk '{print $2}' | tr -d ';')
+ PORT=$(grep -m1 "proxy_pass" "$site" | grep -oP ':\K[0-9]+' || echo "static")
+ ENABLED="✓"
+ printf " %s %-35s → port %s\n" "$ENABLED" "$DOMAIN" "$PORT"
+ fi
+ done
+ else
+ echo " No sites configured. Run 'serverkit setup-proxy' first."
+ fi
+ echo ""
+}
+
+cmd_remove_site() {
+ check_root
+
+ DOMAIN="$1"
+
+ if [ -z "$DOMAIN" ]; then
+ echo "Usage: serverkit remove-site <domain>"
+ cmd_list_sites
+ exit 1
+ fi
+
+ SITE_FILE="/etc/nginx/sites-available/${DOMAIN}.conf"
+ ENABLED_FILE="/etc/nginx/sites-enabled/${DOMAIN}.conf"
+
+ if [ ! -f "$SITE_FILE" ]; then
+ print_error "Site not found: $DOMAIN"
+ exit 1
+ fi
+
+ print_warning "This will remove the site config for $DOMAIN"
+ read -p "Continue? (y/N) " -n 1 -r
+ echo
+
+ if [[ $REPLY =~ ^[Yy]$ ]]; then
+ rm -f "$ENABLED_FILE"
+ rm -f "$SITE_FILE"
+ systemctl reload nginx
+ print_success "Site removed: $DOMAIN"
+ fi
+}
+
# Main
case "${1:-help}" in
help|--help|-h)
@@ -475,6 +617,18 @@ case "${1:-help}" in
config)
cmd_config
;;
+ setup-proxy)
+ cmd_setup_proxy
+ ;;
+ add-site)
+ cmd_add_site "$2" "$3"
+ ;;
+ list-sites)
+ cmd_list_sites
+ ;;
+ remove-site)
+ cmd_remove_site "$2"
+ ;;
*)
print_error "Unknown command: $1"
echo "Run 'serverkit help' for usage"