{"id":581,"date":"2013-12-06T07:58:29","date_gmt":"2013-12-06T07:58:29","guid":{"rendered":"https:\/\/intelligentbee.com\/blog\/?p=581"},"modified":"2024-08-28T12:37:51","modified_gmt":"2024-08-28T12:37:51","slug":"how-to-quickly-switch-apache2-virtualhost","status":"publish","type":"post","link":"https:\/\/intelligentbee.com\/blog\/how-to-quickly-switch-apache2-virtualhost\/","title":{"rendered":"How to Quickly Switch the Apache2 VirtualHost"},"content":{"rendered":"<p>If you&#8217;re just a little bit like me, then you&#8217;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&#8217;ve built a small bash script that will hopefully ease the process.<\/p>\n<h2>How to Quickly Switch the Apache2 VirtualHost<\/h2>\n<p><!--more--><\/p>\n<p>&nbsp;<\/p>\n<p>The script \u00a0that 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.<\/p>\n<p>&nbsp;<\/p>\n<p>Then it removes all symbolic links from \/etc\/apache2\/sites-enabled\/ and enables the VirtualHost you so badly want.<br \/>\nBut, enough talk. Here are the &#8216;echos&#8217; and &#8216;sudos&#8217; that will make your life easier:<\/p>\n<pre class=\"theme:tomorrow-night toolbar:2 lang:default decode:true\">#!\/bin\/bash\r\necho \"--------------------------------\"\r\necho \"switching to a virtual host\"\r\necho \"--------------------------------\"\r\necho \"changing root dir\"\r\ncd \"\/etc\/apache2\/sites-available\/\"\r\nsudo ls\r\necho -n \"Pick the virtual host you want to switch to: \"\r\nread -e new_vh\r\nif [ ! -f \/etc\/apache2\/sites-available\/$new_vh ]; then\r\nfor i in 1 2\r\ndo\r\n        echo  '========'\r\n        sudo ls\r\n        echo -n \"Pick the virtual host you want to switch to: \"\r\n        read -e new_vh\r\n        if [ -f \/etc\/apache2\/sites-available\/$new_vh ]; then\r\n           break\r\n        fi\r\n\r\ndone\r\nfi\r\nif [ ! -f \/etc\/apache2\/sites-available\/$new_vh ]; then\r\n   echo \"you had your chance! I'm not doing anything!!!\"\r\n   exit\r\nfi\r\n\r\necho \"disable current host \"\r\nsudo rm -f \/etc\/apache2\/sites-enabled\/* &amp;&gt;\/dev\/null\r\necho \"enable host $new_vh\"\r\nsudo a2ensite $new_vh &amp;&gt;\/dev\/null\r\necho \"restart apache2\"\r\nsudo service apache2 restart<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you&#8217;re just a little bit like me, then you&#8217;ll love working with only a single active VirtualHost on apache2. [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[77],"tags":[91,194,225,257],"yst_prominent_words":[729,1061],"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/posts\/581"}],"collection":[{"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/comments?post=581"}],"version-history":[{"count":3,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/posts\/581\/revisions"}],"predecessor-version":[{"id":133213,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/posts\/581\/revisions\/133213"}],"wp:attachment":[{"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/media?parent=581"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/categories?post=581"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/tags?post=581"},{"taxonomy":"yst_prominent_words","embeddable":true,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/yst_prominent_words?post=581"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}