webSIGHTdesigns - Web Design, Web Development, Web Hosting

Bash Script to Reload Apache Virtualhosts on Ubuntu

Posted on Saturday, October 26th, 2013 at 3:44 pm
by webSIGHTdesigns

Reload Apache virtualhost configurations with one command instead of four.

On Ubuntu systems in order to reload a virtualhost's configuration in Apache, you have to run four commands:

$ sudo a2dissite hostname.com
$ sudo service apache2 reload
$ sudo a2ensite hostname.com
$ sudo service apache2 reload

Since web developers will have to perform this task quite often, there's clearly a need for a faster way of handling this process. This bash script will allow issue one command to do the same as the above four commands.


RESTARTCMD="/usr/bin/sudo service apache2 reload"

if [ "$1" != '' ]; then
  # disable site
  sudo a2dissite $1
  # restart http daemon
  # enable site
  sudo a2ensite $1
  # restart http daemon
  echo "reloaded apache site"
elif [ "$1" = 'help' ] || [ "$1" = '' ]; then
  echo "usage:"
  echo "sudo resite "
  echo "example: to restart an apache site just run the command 'sudo resite hostname.com'"

# eof

Place the script into a file and make that file executable:

$ chmod +x resite

You could name the file whatever you want the subsequent command to be, in my case I chose to name the command "resite". So now I can just run one command:

$ sudo resite hostname.com

Please Sign In

Please sign in to post a comment.

Web Development

View details »

Web Hosting

View details »

Our Portfolio

View portfolio »

WebSight Designs webSIGHTdesigns preferred email webSIGHTdesigns United States United States