Main Page Content
Dirty Shell Scripts Part 1 Virtual Hosting
If you run any type of Web hosting service like me, you know what a pain in the ass it can be to have to add new virtual hosts to your Web server. If you have Linux and Apache, here's a quick'n'dirty little script that will save you a ton of time:
if [ -t 0 ] # Make sure stdio is a terminalthen echo "Enter the new hostname:"; read arg1 /usr/sbin/useradd -s /bin/false $arg1; echo "User added..."; sed -e "s/name.com/$arg1/g" virthost.conf >> httpd.conf; /usr/local/apache/bin/apachectl restart; echo "$arg1 was added and Apache restarted. You're so sexy!"; chmod 755 /home/$arg1;else echo "Houston, we had a problem!"fiStep by step:
- Basically, the script takes input from the terminal, and makes whatever you typed in the variable 'arg1'.
- Next, we create a user account for the new domain.
- The line that does most of the work, 'sed -e ...' is next. We're simply doing a searching for name.com and replacing it with the name of our new domain. It does the search and replace on a file named virthost.conf, which simply contains the Apache <VirtualHost> directive. Here's what my virthost.conf looks like:
<VirtualHost 10.10.10.10>ServerName www.name.comDocumentRoot /home/name.com/public_htmlCustomLog /home/name.com/web.log combinedCustomLog /home/name.com/main.referer.log referer</VirtualHost>
Naturally, you'll replace the IP address 10.10.10.10 with the IP address of your webserver.
This step finishes up by appending what it replaced in virthost.conf to your httpd.conf(Apache config) file - while leaving the virthost.conf file intact. - The script finishes up by restarting Apache, chmod'ing the users directory and letting you know how of your sexual prowess.
- To get everything to run, copy and paste the virthost.conf file above and save it in $your_apache_home/conf - same with the script itself, cut and paste it into $your_apache_home/conf and save it as newhost.sh(Don't forget to set it executable!). Everything should work pretty smurfy. If not, post a question below and I'll try to help ya out :)