-1

I want to allow the users of my PHP application to create their own subdomains on my server.

For example, I currently operate example.com and for my premium users I manually create a subdomain like bob.example.com or jack.example.com, I point the user subdomains to the same server that runs my application which has a wildcard SSL cert.

What I want to do is have a system that will automatically create subdomains of my domain for my users when they register on my application but I'm not sure where to start on this. I'm assuming that this can't be done purely in PHP but I'm hoping someone can point me in the right direction for what I want to do here.

Ross
  • 112
  • 1
  • 17
  • 1
    Your problem is not related to PHP at all. What you need is a [wildcard DNS record](https://en.wikipedia.org/wiki/Wildcard_DNS_record). From there, you handle requests with your web server (Apache) and you can have it rewrite the domain to a variable or whatever. There are many examples lying around on SO and google, so try to find one now that you know you're after a wildcard dns record. – N.B. Apr 18 '18 at 13:12
  • Sounds like you need a script that updates the apache configuration files... Be carefull with that ;) – Borjante Apr 18 '18 at 13:12
  • Thanks wildcard DNS certainly looks like what I want to do - Thanks – Ross Apr 18 '18 at 13:14
  • @Borjante That's not necessary. It's possible with one apache virtualhost that catches everything and lets your PHP code handle the rest. – Jakub Judas Apr 18 '18 at 13:15
  • @JakubJudas Sounds interesting but not really efficient. I would go for the option of creating virtualhost files throught a script. – Borjante Apr 18 '18 at 13:23
  • @JakubJudas is correct. Do not create a vhost per subdomain, especially if you're using Apache. Been there done that. It is most certainly not efficient and it will quickly get out of hand. Instead, configure the server to simply answer all requests on the IP that the wildcard DNS points to, then use PHP to inspect `$_SERVER['HTTP_HOST']` to determine which content to serve. Your config will remain one small and efficient stanza that never needs to change, and you'll be able to instantly add new subdomains without restarting the service. – Alex Howansky Apr 18 '18 at 13:33
  • @Borjante you're supplying incredibly bad and false info. Wildcard domain **is** efficient. If you like your idea of creating a vhost file per subdomain - go ahead and implement it on your own project, it's bad because you need to deal with adding and removing a file, reloading the web server and how do you even scale to multiple machines? Your suggestion is a maintenance nightmare and gain is - quite literally - nonexistent. – N.B. Apr 18 '18 at 17:10
  • Well I have said nothing about wildcard domain being inneficient, but saying that if he really is loading diferent applications for each subdomain doing it througth PHP it's kind of an overhead for the server. I don't like the idea of modifying apache files throught a script either, I was just proposing one of the two options he's got. – Borjante Apr 18 '18 at 17:17
  • @N.B. Things are not so clear cut. See mass virtualhost at https://httpd.apache.org/docs/current/vhosts/mass.html Each solution has its advantages and drawbacks. – Patrick Mevzek Apr 19 '18 at 22:52
  • Your question is too broad and offtopic here. Basically you need: 1) a system for getting the subdomain name to create (validating it, and other business rules) 2) if you do not use a DNS wildcard (they do create problems sometimes so it can be best without), have a way to update the `example.com` zone, and 3) update Apache configuration, depending on how you configure each host (see debate above in commands). Plus probably creating an account for the user, getting him some kind of access to upload files, etc. It is more a sysadmin question than a developer problem. – Patrick Mevzek Apr 19 '18 at 22:55

1 Answers1

0

Thank you N.B - Wildcard domain seems to be exactly what I'm looking for.

Ross
  • 112
  • 1
  • 17
  • No its not. That's only one very small part of the problem - and if wildcard DNS records came as a surprise, then you have a lot of learning to do. – symcbean Apr 23 '18 at 11:44