Skip to page content or Skip to Accesskey List.

Work

Main Page Content

Dirty Shell Scripts Part 1 Virtual Hosting

Rated 3.91 (Ratings: 1)

Want more?

  • More articles in Code
 

Daniel Cody

Member info

User since: 14 Dec 1998

Articles written: 146

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 terminal

then

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!"

fi

Step 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.com

    DocumentRoot /home/name.com/public_html

    CustomLog /home/name.com/web.log combined

    CustomLog /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 :)

Now, this script is extremelly lacking in the error and exception checking department, so use at your own risk. Like I said, its quick'n'dirty :) However, if you are adding a couple virtual host's a day to your httpd.conf file, you'll soon be wonder where you would be without a script to do the grunt work for you..

Next time we'll throw together a script that will make updating your DNS servers a snap. If you have any other ideas for shell scripts that would make your life easier, and like to see them here, email me or put your suggestion below!

Dan lives a quiet life in the bustling city of Milwaukee, WI. Although he founded what would become evolt.org in 1998, he's since moved on to other projects and is now the owner of Progressive Networks, a Zimbra hosting company based in Milwaukee.

His personal site can be found at http://dancody.org/

The access keys for this page are: ALT (Control on a Mac) plus:

evolt.org Evolt.org is an all-volunteer resource for web developers made up of a discussion list, a browser archive, and member-submitted articles. This article is the property of its author, please do not redistribute or use elsewhere without checking with the author.