If you’re just a little bit like me, then you’ll love working with only a single active VirtualHost on apache2. Since switching from one server to another needs a lot of Linux terminal commands, I’ve built a small bash script that will hopefully ease the process.
How to Quickly Switch the Apache2 VirtualHost
The script that changes the active VirtualHost of an apache2 web server simply lists all available hosts from /etc/apache2/sites-avaliable/ and then politely asks you to pick the one you want to activate.
Then it removes all symbolic links from /etc/apache2/sites-enabled/ and enables the VirtualHost you so badly want.
But, enough talk. Here are the ‘echos’ and ‘sudos’ that will make your life easier:
#!/bin/bash echo "--------------------------------" echo "switching to a virtual host" echo "--------------------------------" echo "changing root dir" cd "/etc/apache2/sites-available/" sudo ls echo -n "Pick the virtual host you want to switch to: " read -e new_vh if [ ! -f /etc/apache2/sites-available/$new_vh ]; then for i in 1 2 do echo '========' sudo ls echo -n "Pick the virtual host you want to switch to: " read -e new_vh if [ -f /etc/apache2/sites-available/$new_vh ]; then break fi done fi if [ ! -f /etc/apache2/sites-available/$new_vh ]; then echo "you had your chance! I'm not doing anything!!!" exit fi echo "disable current host " sudo rm -f /etc/apache2/sites-enabled/* &>/dev/null echo "enable host $new_vh" sudo a2ensite $new_vh &>/dev/null echo "restart apache2" sudo service apache2 restart