Dev Notes

Various Cheat Sheets and Resources by David Egan/Carawebs.

Enable all Apache Config Files

Apache, BASH, Sysadmin
David Egan

Use this BASH script to quickly enable all Apache virtual host configurations.

THis can be handy if for example you are setting up a new development environment.

Background: Enabling Virtual Hosts in Apache 2.4

To set up Apache 2.4 virtual host configurations under Debian/Ubuntu you add a configuration file with a <VirtualHost> block in the /etc/apache2/sites-available directory.

Such config files must have the .conf file extension.

To enable the site, you need to run the a2ensite script, passing the config filename as an argument. This creates a symlink to the config file in the /etc/apache2/sites-enabled directory. You then need to reload Apache.


The following script loops through all config files in /etc/apache2/sites-available and runs a2ensite. There’s no problem if any site is already enabled, since this does not result in an error.

# Enable all config files in /etc/apache2/sites-available directory.
# Usage: Make this script executable and run as a user with sudo privileges.
# ==============================================================================


for FILEPATH in $BASE_DIR/*; do
  if [[ -f "$FILEPATH" ]]; then
    CONF=$(basename "$FILEPATH")
    a2ensite ${CONF}
    if [ $? != 0 ]
      echo "a2ensite NOT successful for ${CONF}"
      echo "a2ensite successful for ${CONF}"

# Reload apache
read -p "Do you want to reload Apache (y/n)?" CONT
if [ "$CONT" = "y" ]; then
    service apache2 reload
    if [ $? -eq 0 ]
      echo "Apache reloaded - check sites in browser."
      echo "There was a problem reloading Apache."
  echo "Changes won't take effect until Apache is reloaded or restarted.";


Copy across the contents of /etc/apache2/sites-available from the old environment to the new (e.g. with rsync).

Download and run the above script:

# Make a directory if necessary
mkdir -p ~/sysadmin/apache
cd ~/sysadmin/apache

# Get the script
curl -o

# Make executable
sudo chmod +x

# Run script
sudo ./

Move across /etc/hosts from the old environment to the new.


comments powered by Disqus