Setting up Named Virtual Hosts on Ubuntu Linux

In recent versions of Ubuntu Linux (I’m writing this on 14.04 LTS), Apache 2.4 is the standard version of the Apache web server. Version 2.4 has a few differences from version 2.2 that make setting up Named Virtual Hosts a bit quirky. Here are some instructions for how to set them up.

For this example, I’m going to use my projects folder in my home directory (e.g., /home/tim/projects) as the virtual host root (top-level) directory.  Ubuntu contains two directories for your virtual hosts files:  /etc/apache2/sites-available and /etc/apache2/sites-enabled.  As you might expect from the names, these directories contain the virtual host configuration files for all of the sites (“available”) and for those that are currently active (“enabled”).

First, in the terminal shell, create a new configuration in file in /etc/apache2/sites-available:

sudo touch /etc/apache2/sites-available/projects.conf

replacing projects with the desired configuration name. The .conf file extension is required (unless you want to change the parameters is /etc/apache2/apache2.conf).

Make yourself the owner of this file using the chown command:

sudo chown username:username /etc/apache2/sites-available/projects.conf

where username should be replaced with your Ubuntu account username.

Now, open this file in your favorite text editor (I like medit!) and paste in the following code:

<VirtualHost projects>
	ServerName projects
	ServerAdmin admin@admin
	DocumentRoot /home/tim/projects

	<Directory /home/tim/projects/>
	DirectoryIndex index.php
	Options Indexes FollowSymLinks
	AllowOverride All
	Require all granted

	ErrorLog ${APACHE_LOG_DIR}/projects_error.log
	CustomLog ${APACHE_LOG_DIR}/projects_access.log combined
	LogLevel warn

Of course, you’ll need to adjust the names and paths appropriately for your situation. A couple of important points to pay special attention to:

  • On line #1, ensure that you use the desired virtual host name. This is different from earlier versions of Apache, where you would typically specify *:80.
  • On line #2, set ServerName to the alias that you want to use in your browser for the new virtual host. In this example, projects means that the URL for the virtual host will be http://projects.
  • On line #7, make sure that you include the trailing slash (/) in the path name for the Directory specification.

To activate your new virtual host, create a symbolic link to the new configuration in the /etc/apache2/sites-enabled directory:

sudo ln -s /etc/apache2/sites-available/projects.conf /etc/apache2/sites-enabled/projects.conf

Alternately, you can use the a2ensite helper command:

sudo a2ensite projects.conf

Finally, you’ll need to restart the Apache web server to make the new virtual host available:

sudo service apache2 reload

To confirm that the new virtual host is working properly, create a simple PHP script named phpinfo.php to display the PHP environment configuration in your root (“home”) directory for the new virtual host (/home/tim/projects in this example):

	echo phpinfo();

In your web browser, enter the the URL for the new virtual host with the name of the PHP script: http://projects/phpinfo.php. If everything is working you should see the details provided by the PHP phpinfo() function.

Virtual host on ubuntu 13.10 and apache 2.4.6 – Stack Overflow
Setting Up Name Based Virtual Hosting – Ubuntu Tutorials
How To Set Up Apache Virtual Hosts on Ubuntu 12.04 LTS – Digital Ocean
How to Create Name Based and IP Based Virtual hosts in Apache – Ubuntu Geek


One thought on “Setting up Named Virtual Hosts on Ubuntu Linux

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s