0

I'm a newbie server administrator having recently purchased an account from Slicehost.com 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.

Thanks!

freiheit
  • 14,544
  • 1
  • 47
  • 69
Hellweaver666
  • 165
  • 1
  • 1
  • 4

3 Answers3

5

I'd suggest you read up on bash scripting.

Some good links:

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

theotherreceive
  • 8,365
  • 1
  • 31
  • 44
  • More powerful than batch programming? I don't know. Better documented? Absolutely. – J. Polfer Jul 06 '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 06 '09 at 18:26
  • Perfect! Thanks, that's really handy - I'll check out those resources. – Hellweaver666 Jul 07 '09 at 10:32
3

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 script.sh new_site

you could have script.sh looks something like this

#!/bin/sh

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

Catherine MacInnes
  • 1,958
  • 11
  • 15
0

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.

freiheit
  • 14,544
  • 1
  • 47
  • 69