Php – Having many subdomains with SSL–best practices

apache-2.2PHPsslssl-certificatevirtualhost

I have a site that has many subdomains (one for each client). The content for each subdomain will be different, pulling different data, different layouts etc, so I don't think I can just alias one directory to many different sub domains. My question is, what's the best practice for doing this? Each subdomain will need SSL. My setup is

  • Amazon EC2 (ubuntu)
  • Wildcard SSL for my domain (*.mydomain.com)
  • Apache 2
  • PHP (LAMP)

Currently, I'm just creating a new entry into "/etc/hosts" and an appropriate spot in the web root (ie /var/www/abc.mydomain.com, /var/www/def.mydomain.com, etc). I've just discovered that I'll need a unique IP for each subdomain, and that's tricky with EC2 as they limit you to the number you can have (unless you jump through some hoops). Right now I have about 10 subdomains, which is manageable, but theoretically I could have thousands.

Am I doing it the only way possible or is there a better way I should investigate?

Best Answer

Is there a particular reason you are adding them to /etc/hosts? If the hostname resolves, that should be plenty for a more general purpose.

I like the directory structure you are using and that is very similar to any approach I would use. Additionally, I recommend specifying separate ErrorLog and CustomLog files for each subdomain.

Zoredache makes a fantastic point on IPs, as that your wildcard certificate should eliminate the need for multiple IPs when adding subdomains. Additionally, there is a newer Server Name Indication (SNI) feature that eliminates the need for an IP per each separate SSL certificate.

If you have thousands, that is typically where you might consider writing a script for managing the adding or removing VirtualHosts, sharing content, redirects, and other potential solutions. Options are going to be based on your exact needs at that time.

If you are talking about multiple user systems, the approaches are fundamentally different as well. For example, each domain would be in /home/user/public_html or /home/user/www.

Related Topic