Adding and Removing Virtual Hosts on Ubuntu 14.04

Here's a cheat sheet for working with virtual hosts on an Ubuntu server. The instructions assume you're not logged in as the root user. Change to your own domain/subdomain.

Adding a Virtual Host

Create the directory for your new site and grant permissions. If your user isn't part of the www-data group, you might want to adjust the second line:

sudo mkdir -p /var/www/
sudo chown -R $USER:www-data /var/www/
sudo chmod -R 755 /var/www/

Add an index page (optional):

vim /var/www/

Create the virtual host config file:

sudo vim /etc/apache2/sites-available/

Here's a very basic example config file you can start with. Paste it in and adjust as necessary:

<VirtualHost *:80>

DocumentRoot /var/www/
<Directory /var/www/>
Options -Indexes
AllowOverride All
Order Allow,Deny
Allow from All

ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined


Now enable the new virtual host and restart Apache:

sudo a2ensite
sudo service apache2 restart

If your DNS is already configured, your site should be available at

Removing a Virtual Host

First, disable the virtual host and restart Apache:

sudo rm /etc/apache2/
sudo rm -Rf /var/www/

At this point, the site is disabled and will no longer be accessible.

Now just remove the config file and cleanup the /var/www/ directory to permanently remove the site and all of its files:

sudo rm /etc/apache2/sites-available/
sudo rm -Rf /var/www/
Author avatar

About the author

Creator of Surreal CMS and other web things. Follow me for tweets about JavaScript, CSS, and web programming.