Posted By

jfoucher on 11/11/09


Tagged

apache hosts


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

karmacode


Script to create Apache virtual hosts automatically


 / Published in: Bash
 

  1. #!/bin/bash
  2. CMDLN_ARGS="$@" # Command line arguments for this script
  3. export CMDLN_ARGS
  4.  
  5. # Run this script as root if not already.
  6. chk_root () {
  7.  
  8. if [ ! $( id -u ) -eq 0 ]; then
  9. echo "Please enter your password."
  10. exec sudo su -c "${0} ${CMDLN_ARGS}" # Call this prog as root
  11. exit ${?} # sice we're 'execing' above, we wont reach this exit
  12. # unless something goes wrong.
  13. fi
  14.  
  15. }
  16. chk_root
  17. FILE=$(cat /etc/hosts)
  18. echo $FILE |grep -o "test$";
  19.  
  20. if [ `echo $FILE |grep -o "\$1$"` ];
  21. then
  22. echo "/etc/hosts file is ok"
  23. else
  24. echo "127.0.0.1 $1" >> /etc/hosts #├ža c'est ok
  25. fi
  26. cp /etc/apache2/sites-available/timetracker /etc/apache2/sites-available/$1
  27. cat /etc/apache2/sites-available/$1 | sed "s/timetracker/$1/g" |cat > /etc/apache2/sites-available/$1
  28. ln -s /etc/apache2/sites-available/$1 /etc/apache2/sites-enabled/$1
  29. if [ -d /home/jonathan/html/$1 ];
  30. then
  31. echo "host directory exists";
  32. else
  33. echo "creating directory"
  34. mkdir "/home/jonathan/html/$1"
  35. fi
  36. chown jonathan /home/jonathan/html/$1
  37. chgrp jonathan /home/jonathan/html/$1
  38. /etc/init.d/apache2 restart

Report this snippet  

You need to login to post a comment.