skip to Main Content

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


  1. Chosen as BEST ANSWER

    Answering my own question:

    Enabled mod_macros in apache;

    sudo a2enmod macro
    

    Added a macro for creating a http :80 virtual host and another for creating a https :443 virtual host with the necessary variables;

    <Macro ProtectedVHost $domain>
        <VirtualHost *:80>
            ServerAdmin [email protected]
            ServerName $domain
            ServerAlias www.$domain
    
            DocumentRoot /var/www/customer_sites/$domain/public_html
    
            ErrorLog ${APACHE_LOG_DIR}/error.log
            CustomLog ${APACHE_LOG_DIR}/access.log combined
    
            RewriteEngine on
            RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
        </VirtualHost>
    
        <IfModule mod_ssl.c>
            <VirtualHost *:443>
                ServerName $domain
                ServerAlias www.$domain
                ServerAdmin [email protected]
    
                DocumentRoot /var/www/customer_sites/$domain/public_html
    
                ErrorLog ${APACHE_LOG_DIR}/error.log
                CustomLog ${APACHE_LOG_DIR}/access.log combined
    
                SSLEngine on
                SSLCertificateFile /etc/letsencrypt/live/$domain/fullchain.pem
                SSLCertificateKeyFile /etc/letsencrypt/live/$domain/privkey.pem
                Include /etc/letsencrypt/options-ssl-apache.conf
            </VirtualHost>
        </IfModule>
    </Macro>
    

    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:

    Use {Macro Name} {Domain Name}
    

    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


  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search