Skip to main content
ApacheBash ScriptingSUSE

Script the creation of vhosts in Apache

By April 12, 2013September 12th, 2022No Comments

Scripting is your lifesaver as a sysadmin, it gives you time for other things, such as the beach, bike, whatever. We are going to take a quick look creating Apache virtual hosts from a bash shell script. The main “meat and veg” of the script uses sed to parse the vhost template. Quick and easy to use and better than typing. Once the Linux BASH script is created then we can run the script and just specify, as an argument, the name of the virtual host to create in Apache. What could be more simple?

  • CONFDIR=/etc/apache2/vhosts.d
  • WEBDIR=/srv/www/vhosts
  • mkdir -p $WEBDIR/$1/cgi-bin
  • sed s/”dummy-host.example.com”/$1/g $CONFDIR/myhost.template > $CONFDIR/$1.conf
  • echo -e “127.0.0.1 t $1” >> /etc/hosts
  • echo “This is a website in construction for $1” > $WEBDIR/$1/index.html
  • rcapache2 restart