Want multiple vhosts without creating new apache config files each time? Use vhost_alias

This is a pretty quick technical post explaining how to get vhost_alias working with apache2 on Ubuntu 12.10.Enable the vhost_alias plugin for Apache2

sudo a2enmod vhost_alias

Then create a “site” (just a config file) which uses that module in /etc/apache2/sites-available/dynamic:

echo 'UseCanonicalName Off
LogFormat "%V %h %l %u %t \"%r\" %s %b" vcommon
CustomLog ${APACHE_LOG_DIR}/access.log vcommon
VirtualDocumentRoot /var/www_hosts/%0' | sudo tee /etc/apache2/sites-available/dynamic

Enable that “site” and restart Apache2

sudo a2ensite dynamic
sudo service apache2 restart

Create the directory structure needed for the development to start

sudo mkdir -p -m 755 /var/www_hosts/YourDevSiteName
sudo chown -R YourUserName:YourUserName /var/www_hosts

Edit /etc/hosts to create a hostname to use for my development work. This means that I can create as many “virtual” hosts as a I want, and Apache2 will just use whatever directory structure I’ve got set up.

127.0.0.1      localhost YourDevSiteName
127.0.1.1      mycomputer

JonTheNiceGuy

He/Him. Husband and father. Linux advocating geek. Co-Host on the AdminAdmin Podcast, occasional conference speaker.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.