I am creating a cloud based application in laravel/php.

I have a functionality where user can have their website at user.myapp.com and it works (with subdomain and model binding).

I now want to go one step further, and allow users to add their own custom domain names. So their website will be reachable at user-domain.com rather than (or as well as) user.myapp.com.

I want to understand how to do this. I'm using Laravel framework which is based on php and apache.

What kind of server do I need? Can I use shared hosting? What configs do I need to do in order for this to work? What DNS record the user must create to put things together?

Dani AI

Generated

Short answer: yes — you can let customers map their own domains to tenant sites, but you need control over DNS, vhosts and TLS to do it reliably. Shared hosting rarely gives the automation and certificate control you need for dozens or hundreds of customer domains; opt for a VPS/cloud instance or a PaaS that supports custom domains and programmatic cert management. was right that Apache VirtualHosts are involved, but for a SaaS it's usually better to use a single “catch‑all” vhost that forwards every Host to your Laravel app and let the app decide which tenant to serve. (httpd.apache.org)

DNS basics you will tell customers:

  • For a root/apex domain (example.com) ask for an A record to your server IP (or an ALIAS/ANAME if their DNS provider supports it).
  • For subdomains (www.example.com) a CNAME to their tenant target is fine.
    CNAMEs at the zone apex are not allowed by DNS itself; some providers provide “CNAME flattening” or ALIAS/ANAME workarounds. Set a low TTL while provisioning. (blog.cloudflare.com)

Apache + HTTPS notes:

  • For HTTP you can rely on a default vhost and route by Host header to Laravel.
  • For HTTPS you must present a certificate that matches the requested hostname; Apache/mod_ssl supports SNI but you need a cert per mapped domain (or a TLS-terminating proxy that issues certs automatically). Automate certificate issuance with an ACME client (Certbot) or use a proxy (Caddy/Traefik) that handles ACME for you. (httpd.apache.org)

App-side pattern and practical flow:

  • When a user adds a domain, ask them to create the DNS record, then verify ownership (HTTP token or DNS TXT). Once DNS resolves you issue/renew the cert and enable the mapping in your DB. In Laravel detect the host in middleware, look up the tenant record and bind the tenant to the request context. Example (very small sketch):
public function handle($request, Closure $next) {
    $host = $request->host();
    $tenant = \App\Models\Tenant::where('domain', $host)->first();
    if (! $tenant) abort(404);
    app()->instance('tenant', $tenant);
    return $next($request);
}

Use Laravel's request/host helpers and protect against certificate issuance abuse (verify ownership and watch LetsEncrypt limits). Automate everything you can (DNS check, cert issuance, config enable) to avoid frequent manual Apache reloads. (laravel.com)

Recommended Answers

All 2 Replies

You should be able to do it by setting up virtual hosts in Apache, I think.

Do i need dedicated host or shared host will be fine?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.