MYHOSPITALNOW.COM

Find Trusted Doctors & Hospitals

Discover healthcare providers, compare hospitals, and explore treatment options worldwide.

Free Treatment Quotes • Doctor Search • Hospital Comparison

Find Healthcare Options

www.myhospitalnow.com

Production-Ready Setup: Apache + Laravel + Docker + Dynamic Reverse Proxy for Multiple Users

This updated guide explains how to implement a scalable, production-ready setup where each user gets a Docker container and unique subdomain like user123.wizbrand.com, without creating new Apache VirtualHosts for each.


โœ… 1. Apache Configuration (Dynamic Reverse Proxy)

Apache Modules (Enable Once)

sudo a2enmod proxy
sudo a2enmod proxy_http
sudo a2enmod rewrite
sudo systemctl restart apache2

Apache Wildcard VirtualHost (Single File)

Edit /opt/lampp/etc/extra/httpd-vhosts.conf or /etc/apache2/sites-available/000-default.conf:

<VirtualHost *:80>
    ServerName wizbrand.com
    ServerAlias *.wizbrand.com

    ProxyPreserveHost On
    RewriteEngine On

    # Extract user ID from subdomain (e.g., user123)
    RewriteCond %{HTTP_HOST} ^user([0-9]+)\.wizbrand\.com$ [NC]
    RewriteRule ^/(.*)$ http://127.0.0.1:9%1/$1 [P,L]

    ProxyPassReverse / http://127.0.0.1/
</VirtualHost>
Code language: PHP (php)

Restart Apache:

sudo systemctl restart apache2

โœ… 2. Laravel Controller to Spawn Container and Return URL

Create ContainerController.php:

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class ContainerController extends Controller
{
    public function createUserContainer(Request $request)
    {
        $user = Auth::user();
        $containerName = 'user-' . $user->id;
        $port = 9000 + $user->id;

        // Start Docker container mapped to unique port
        shell_exec("docker run -d -p {$port}:80 --name {$containerName} my-image");

        // Return container's unique URL
        return response()->json([
            'url' => "http://user{$user->id}.wizbrand.com"
        ]);
    }
}
Code language: PHP (php)

Add Laravel Route (routes/web.php)

Route::get('/create-container', [ContainerController::class, 'createUserContainer'])->middleware('auth');
Code language: PHP (php)

โœ… 3. Domain Setup (Production)

In your domain provider’s DNS (e.g., GoDaddy, Cloudflare), add:

TypeNameValue
A*your_server_ip

This wildcard allows user123.wizbrand.com โ†’ your server.


โœ… 4. Local Testing Setup

Add to /etc/hosts (on dev machine)

127.0.0.1 user123.wizbrand.com
127.0.0.1 user456.wizbrand.com
Code language: CSS (css)

Or use:

  • nip.io: user123.127.0.0.1.nip.io
  • sslip.io: user123.127.0.0.1.sslip.io

โœ… 5. Docker Image Assumptions

  • Exposes port 80
  • Named my-image or configured dynamically

Example container:

docker run -d -p 9123:80 --name user-123 my-image
Code language: CSS (css)

โœ… 6. Test Script (Optional Bash)

#!/bin/bash
USER_ID=$1
PORT=$((9000 + USER_ID))
CONTAINER="user-${USER_ID}"

# Launch container
sudo docker run -d -p ${PORT}:80 --name ${CONTAINER} my-image

# Access URL
echo "Container for user ${USER_ID} available at: http://user${USER_ID}.wizbrand.com"
Code language: PHP (php)

Usage:

./create-container.sh 123

๐Ÿง  Summary

ComponentRole
Apache (Wildcard)Dynamic proxy via subdomain-to-port
LaravelContainer creator & subdomain router
DockerUser-specific environments
DNS (Wildcard)Resolves all subdomains to one IP

This approach is clean, scalable, reload-free, and requires just one Apache config regardless of user count. Let me know if you’d like to add Let’s Encrypt SSL automation, container auto-cleanup, or database tracking next.

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services โ€” all in one place.

Explore Hospitals
I'm Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms. I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.

Related Posts

Install and Configure Gerrit on CentOS and Ubuntu (MySQL + Apache Reverse Proxy)

A practical, step-by-step guide to installing Gerrit for code review on CentOS/RHEL and Ubuntu/Debian. Includes package command comparisons, setup workflow, Apache proxy configuration, and verification checks.

Read More

Step-by-Step Tutorial: Dynamic Container URL Routing using Apache (XAMPP) for Multi-User Laravel App

This tutorial guides you through setting up a multi-user container-based architecture using: We’ll configure Apache to dynamically proxy subdomains like user123.wizbrand.com to each user’s container (e.g., port…

Read More

mod_jk module Vs mod_rewrite and mod_proxy

rajeshkumar created the topic: mod_jk module Vs mod_rewrite and mod_proxy Hi, In order to Integrate Jboss with Apache, i got to 2 solutions below; 1. Integration using…

Read More

Integration of Jboss and Apache2

rajeshkumar created the topic: Integration of Jboss and Apache2 Hi, My Application(.ear) is running in Jboss with any issues on 7001 port. There are following requirement as…

Read More

Apache: Comprehensive list of Apache HTTP Server configuration directives

This page lists the Apache HTTP Server 2.4 configuration directives you will see most often in httpd.conf, virtual host files and SSL configs, grouped by function. Below…

Read More

The Complete Pomerium Tutorial: From Absolute Beginner to Advanced Zero Trust Access Engineering

Course overview Pomerium is an open-source, identity-aware proxy that helps organizations securely provide access to internal applications and services. It combines identity verification, access policies, and reverse-proxy…

Read More
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
0
Would love your thoughts, please comment.x
()
x