Sample Header Ad - 728x90

Automating the Virtual host creation process

2 votes
3 answers
1967 views
Until recently I used tools like Cpanel or Webmin to manage Apache2 virtual hosts. I recently discovered how to create these manually under sites-available directory. I can do this process manually time and again but when done manually it includes lot's of coping & pasting, repetitive string changes (once for the file name and of the domain+tld inside it, and further executions like a2ensite (see below). I now seek to automize the process but I'm not sure what is the best way to change domain+tld both inside (in the Vhost) and outside (in the file name and further executions) This is my way to manually create each Vhost: 1. Copy a ready Vhost template file (I have such a file which I name d.t and I clone it whenever I need to create a new Vhost): DocumentRoot "/var/www/html/d.t" ServerName www.d.t Options +SymLinksIfOwnerMatch Require all granted ServerAlias www.d.t 3. Search and replace "d.t", with domain+tld: Do in nano. 4. Replace filename (d.t) with domain+tld: mv /etc/apache2/sites-available/d.t /etc/apache2/sites-available/domain.tld 5. Enable domain.tld.conf & restart the Apache service: a2ensite domain.tld.conf && systemctl restart apache2.service This might seem simple but when I manually do this algorithm from a manual file it might take even more time to rewrite each example like d.t into the relevant domain+tld. My question: ------------ I am looking for a way to run a script with these actions: The script will actually look quite similar but in this script, d.t will be replaced with the script's very file name. For example, if the script's filename is MyNewVhost.com, each d.t occurrence inside it will become MyNewVhost.com. I guess some variable work can serve that purpose, but I might be wrong; If so, a way to tell the program "Put the file name in each d.t" seems to be what I need. Notes: ------ 1. I believe an answer could also give a good direction for people with a similar problem in Nginx Server blocks (Nginx Sblocks).
Asked by user149572
Mar 27, 2017, 10:29 PM
Last activity: Mar 30, 2017, 12:01 PM