🚀 DevOps & SRE Certification Program 📅 Starting: 1st of Every Month 🤝 +91 8409492687 🔍 Contact@DevOpsSchool.com

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

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/\ [P,L]

    ProxyPassReverse / http://127.0.0.1/
</VirtualHost>

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"
        ]);
    }
}

Add Laravel Route (routes/web.php)

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

✅ 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

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

✅ 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"

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.

Subscribe
Notify of
guest


0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments

Certification Courses

DevOpsSchool has introduced a series of professional certification courses designed to enhance your skills and expertise in cutting-edge technologies and methodologies. Whether you are aiming to excel in development, security, or operations, these certifications provide a comprehensive learning experience. Explore the following programs:

DevOps Certification, SRE Certification, and DevSecOps Certification by DevOpsSchool

Explore our DevOps Certification, SRE Certification, and DevSecOps Certification programs at DevOpsSchool. Gain the expertise needed to excel in your career with hands-on training and globally recognized certifications.

0
Would love your thoughts, please comment.x
()
x