Find the Best Cosmetic Hospitals

Explore trusted cosmetic hospitals and make a confident choice for your transformation.

โ€œInvest in yourself โ€” your confidence is always worth it.โ€

Explore Cosmetic Hospitals

Start your journey today โ€” compare options in one place.

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:

  • Laravel (running on Apache via XAMPP in Ubuntu VM)
  • Docker (to spawn a container for each user)
  • Apache (as a reverse proxy)
  • Domain: wizbrand.com

We’ll configure Apache to dynamically proxy subdomains like user123.wizbrand.com to each user’s container (e.g., port 9123).


๐Ÿ  1. Environment Setup

1.1. Prerequisites

  • Ubuntu VM
  • XAMPP (with Apache)
  • Docker installed
  • Laravel app running in XAMPP (htdocs/myapp)

๐Ÿ”ง 2. Laravel: Container Creation Endpoint

Create a route and controller in Laravel to spawn a Docker container and generate a proxy URL.

Example Laravel Controller

public function createUserContainer()
{
    $user = auth()->user();
    $containerName = 'user-' . $user->id;
    $port = 9000 + $user->id;

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

    // Create Apache virtualhost config
    $vhost = "
<VirtualHost *:80>
    ServerName $containerName.wizbrand.com
    ProxyPreserveHost On
    ProxyPass / http://localhost:$port/
    ProxyPassReverse / http://localhost:$port/
</VirtualHost>
    ";

    file_put_contents("/opt/lampp/etc/extra/vhosts/$containerName.conf", $vhost);
    
    // Append hosts file (for local dev only)
    file_put_contents("/etc/hosts", "\n127.0.0.1 $containerName.wizbrand.com", FILE_APPEND);

    // Reload Apache
    shell_exec("sudo /opt/lampp/lampp reloadapache");

    return response()->json(['url' => "http://$containerName.wizbrand.com"]);
}
Code language: PHP (php)

Replace my-image with your actual Docker image name


๐ŸŒ 3. Apache Configuration

3.1 Enable Apache Modules

sudo a2enmod proxy
sudo a2enmod proxy_http
sudo systemctl restart apache2  # or /opt/lampp/lampp restart
Code language: PHP (php)

3.2 Main Apache Config File

Ensure it includes:

# In httpd.conf or extra/httpd-vhosts.conf
IncludeOptional etc/extra/vhosts/*.conf
Code language: PHP (php)

Create folder:

sudo mkdir /opt/lampp/etc/extra/vhosts

๐ŸŒŽ 4. Domain & Local Setup

4.1 Production DNS Setup

  • Add one wildcard DNS record:
*.wizbrand.com โ†’ your_server_ip
Code language: CSS (css)

4.2 Local Development Setup

  • Edit /etc/hosts on host system (your laptop):
127.0.0.1 user-123.wizbrand.com
127.0.0.1 user-456.wizbrand.com
Code language: CSS (css)

๐Ÿค– 5. Laravel Route (routes/web.php)

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

๐Ÿ”„ 6. Workflow Summary

  1. User logs into Laravel app
  2. Clicks button to create container
  3. Laravel creates Docker container on a unique port
  4. Laravel generates Apache virtualhost config
  5. Apache proxies userXYZ.wizbrand.com โ†’ user’s container
  6. User accesses the unique subdomain URL

๐Ÿš€ Optional Enhancements

  • Use Laravel queue to create containers async
  • Setup SSL (Letโ€™s Encrypt + wildcard)
  • Add container expiry/auto-deletion scheduler
  • Store container info in DB

๐Ÿšจ Caveats

  • Apache config reload is needed for each new subdomain (downtime may be noticeable)
  • Traefik/Nginx are more scalable but not used here as per your request
  • Ensure ports are not reused across containers

๐Ÿ“Š Tools Used

  • Laravel (PHP Backend)
  • Docker (User environment containers)
  • Apache (Reverse proxy)
  • XAMPP (AMP stack)

๐ŸŽ“ Conclusion

This setup allows you to offer per-user container environments with unique URLs, using Apache as a reverse proxy without requiring manual DNS records for each user. It’s great for development, testing, or isolated workspaces.

You’re absolutely right โ€” the solution I gave does involve creating a new Apache VirtualHost config per container, which works but is not scalable for hundreds of users.

Since youโ€™re looking for a better, scalable approach using Apache only (no Traefik or Nginx), here’s the optimized solution you’re likely aiming for:


โœ… Improved Approach: One Wildcard VirtualHost + Rewrite Rules

Instead of generating a new VirtualHost file for every container/user, weโ€™ll use one single wildcard-based VirtualHost that dynamically reverse-proxies based on the subdomain.


๐Ÿง  Concept

Assume:

  • Each user gets a subdomain like user123.wizbrand.com
  • Each container listens on a unique port like 91XXX (e.g., 9123 for user 123)

We’ll use mod_rewrite and mod_proxy to extract the subdomain and map it to the corresponding port dynamically.


๐Ÿ”ง Step-by-Step: Dynamic Apache Reverse Proxy Using Subdomains

โœ… 1. Enable Required Apache Modules

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

โœ… 2. Single Wildcard VirtualHost Configuration

Edit your Apache config (XAMPP: /opt/lampp/etc/extra/httpd-vhosts.conf or Ubuntu default: /etc/apache2/sites-available/000-default.conf):

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

    ProxyPreserveHost On
    RewriteEngine On

    # Extract username from subdomain (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)

โœ… This does the magic:

  • Matches user123.wizbrand.com
  • Extracts 123 from the subdomain
  • Proxies to localhost:9123

โœ… 3. Docker Container Port Strategy

Each container should expose port 80 to a host port like 9123.

Example command from Laravel:

$containerName = "user-{$user->id}";
$hostPort = 9000 + $user->id;

shell_exec("docker run -d -p {$hostPort}:80 --name {$containerName} my-image");
Code language: PHP (php)

โœ… 4. DNS Setup (Production)

In your domain DNS provider (for wizbrand.com), create:

TypeNameValue
A*your_server_ip

This allows *.wizbrand.com to point to your server automatically.


โœ… 5. Local Development (/etc/hosts)

For testing on your local machine or VM:

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

Or use dynamic wildcard resolution services like:

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

๐Ÿ” Summary: Why This is Better

FeaturePrevious MethodImproved Method โœ…
VHost per userYes โŒNo โœ…
Apache reload for each userYes โŒNo โœ…
Scalable for 100+ usersNot practical โŒFully scalable โœ…
Performance-friendlyNo โŒYes โœ…
Maintains Apache-only setupYes โœ…Yes โœ…

๐Ÿ“˜ Laravel Integration Recap

$port = 9000 + $user->id;
shell_exec("docker run -d -p {$port}:80 --name user-{$user->id} my-image");

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

Find Trusted Cardiac Hospitals

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

Explore Hospitals
Iโ€™m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I have worked at <a href="https://www.cotocus.com/">Cotocus</a>. I share tech blog at <a href="https://www.devopsschool.com/">DevOps School</a>, travel stories at <a href="https://www.holidaylandmark.com/">Holiday Landmark</a>, stock market tips at <a href="https://www.stocksmantra.in/">Stocks Mantra</a>, health and fitness guidance at <a href="https://www.mymedicplus.com/">My Medic Plus</a>, product reviews at <a href="https://www.truereviewnow.com/">TrueReviewNow</a> , and SEO strategies at <a href="https://www.wizbrand.com/">Wizbrand.</a> Do you want to learn <a href="https://www.quantumuting.com/">Quantum Computing</a>? <strong>Please find my social handles as below;</strong> <a href="https://www.rajeshkumar.xyz/">Rajesh Kumar Personal Website</a> <a href="https://www.youtube.com/TheDevOpsSchool">Rajesh Kumar at YOUTUBE</a> <a href="https://www.instagram.com/rajeshkumarin">Rajesh Kumar at INSTAGRAM</a> <a href="https://x.com/RajeshKumarIn">Rajesh Kumar at X</a> <a href="https://www.facebook.com/RajeshKumarLog">Rajesh Kumar at FACEBOOK</a> <a href="https://www.linkedin.com/in/rajeshkumarin/">Rajesh Kumar at LINKEDIN</a> <a href="https://www.wizbrand.com/rajeshkumar">Rajesh Kumar at WIZBRAND</a> <a href="https://www.rajeshkumar.xyz/dailylogs">Rajesh Kumar DailyLogs</a>

Related Posts

Terraform Backend Tutorial

Terraform is a popular open-source infrastructure as code tool used to create and manage infrastructure resources. The state of the infrastructure resources managed by Terraform is stored…

Read More

Best Tools for Software Composition Analysis (SCA)

Hereโ€™s a clear and professional explanation of the three related concepts you asked about โ€” all of which are critical parts of secure software development, especially in…

Read More

Top 10 AI Code Review Tools in 2026: Features, Pros, Cons & Comparison

Introduction In 2026, AI code review tools have become essential for developers aiming to enhance code quality, streamline workflows, and accelerate software delivery. These tools leverage advanced…

Read More

Top 10 Expense Management Tools in 2026: Features, Pros, Cons & Comparison

Introduction Expense management tools are critical for businesses of all sizes in 2026 as they help streamline financial processes, improve budgeting, ensure compliance, and enhance financial visibility….

Read More

Top 10 Web Application Firewall (WAF) Tools in 2026: Features, Pros, Cons & Comparison

Introduction In the rapidly evolving landscape of cybersecurity, Web Application Firewalls (WAFs) have become a critical component in defending web applications from malicious attacks such as SQL…

Read More

Top 10 Endpoint Management Tools in 2026: Features, Pros, Cons & Comparison

Introduction In 2026, businesses of all sizes are increasingly reliant on a variety of devicesโ€”laptops, desktops, mobile devices, and other endpointsโ€”that connect to their networks. With the…

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