Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm a newbie server administrator having recently purchased an account from and using it to host a few personal sites, so I can learn more (I'm primarily a web developer).

Years ago I remember playing with batch scripts on Windows that would allow me to automate certain basic things. Does anything like this exist for Unix?

I would like to automate setting up of new domains in Apache2, so basically creating directories, setting permissions, creating the configuration file in sites-available then running the a2ensite command and finally restarting Apache.

Is this achievable from a basic script I can run from the command line? Any basic pointers and guidance you can give would be much appreciated.


share|improve this question
up vote 5 down vote accepted

I'd suggest you read up on bash scripting.

Some good links:

Bash is a lot more powerful than a batch file :)

share|improve this answer
More powerful than batch programming? I don't know. Better documented? Absolutely. – J. Polfer Jul 6 '09 at 17:09
I have found that *nix shell scripting languages are much more powerful than default Windows batch scripting. This is due to partially to more powerful script language features but mostly due to *nix philosophy for making very powerful command line utilities that accept piped data. – Chris Nava Jul 6 '09 at 18:26
Perfect! Thanks, that's really handy - I'll check out those resources. – Hellweaver666 Jul 7 '09 at 10:32

It is definitely doable. Anything that can be accomplished by a series of commands on the command line can be put into a bash script and run from there.

The script starts with #!/bin/bash or #!/bin/sh (for sh instead of bash) After that # starts a comment line $VARIABLE_NAME can be used to refer to a variable set by VARIABLE_NAME=foo

Command line parameters can be grabbed starting with $1, ($0 is the name of the program that was called).

so if you want to evoke a script as new_site

you could have looks something like this


mkdir /var/www/htdocs/new_site chmod 755 /var/www/htdocs/new_site ... apachectl restart

then you simply have to run the script with sufficient permissions actually perform all the commands run.

Use sh -x to debug (it will step through the script with lots of output so you can tell what is actually happening).

share|improve this answer

You might also look into puppet. It's a bit of overkill for a single server, but is definitely designed to handle this kind of thing.

share|improve this answer
Is it this Puppet: ? "Puppet is an open source configuration management tool. It is written in Ruby." – Peter Mortensen Jul 14 '09 at 23:44
Yes, that one. – freiheit Jul 15 '09 at 0:56

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.