I have an ubunto vm running a lamp stack, and a for now, one domain name redirecting to the vm’s ip address.
Concept wise the main principle is I want users to register an account and get a "website" running over on a subdomain of my own domain. That part is easy with wildcard subdomains in apache vhosts etc and with certbot i managed to automatically have all subdomains protected under ssl.
Now, if a customer wants to buy his/her own domain name, with me or some other registrar they need to point an A record to my ip address and a CNAME from www to the domain name. And in my end I need to add a vhost file under sites-available folder configuring the virtual host file for that new domain name and restarting "gracefully" apache.
Here lies the problem. How can I manipulate apache vhosts files etc so I can accomplish this at the push of a button on my main website? I’m using PHP in my backend and doing it in php (shell exec etc) seems like a security risk..
Im running on GCP so any of GCP’s services are available.
Thanks in advance.
2
Answers
Answering my own question:
Enabled mod_macros in apache;
Added a macro for creating a http :80 virtual host and another for creating a https :443 virtual host with the necessary variables;
Created a conf file out of my public_html folder on my main websites editable in php where i add records using my own macro as the user adds in his domain:
Import that file into apache.conf
Finally i created a CRON that starts ever so often and reads that file and compares it to a saved version. If the files has changed, it will gracefully restar apache which will read and re-create the necessary virtual hosts.
Hope this helps anybody looking to do the same thing
If you are worried about a shell script in your server you can run an application using SSH from a different machine; It can be a small instance that an application can be something like a putty which is available only on windows. You should find something relevant for Linux as there are many alternatives available. Use SSH client application and Python language to automate it.
As the manual process involves running the same command multiple times you can create virtual host files and see if files are created in sites-available directory etc.
Once the new users register for your service you need to create virtual host files and run chown commands and see if the files are created in the sites from the directory and restart your apache. You can refer to the steps in Automating virtual host setup on your server.