## Introduction ### Virtual Hosts Virtual Hosts are a way to host more than one domain from a single IP address/server. This could be helpful, for example, to people who wish to host more than one website from a single droplet. The visitors of the websites will be shown the correct information based on the domain they are accessing, whereas, without virtual hosts correctly setup, all domains would display the same information. There is no limit to the number of virtual hosts (e.g. domains) that can be added to a server, given enough computing and storage capacity. ### Prerequisites In order to run the commands in this tutorial, the user must have root privileges. If you log into your droplet using your root user account, you mustn’t worry about this. If you don’t, you can see how to set that up in the article [Initial Server Setup](https://www.digitalocean.com/community/articles/initial-server-setup-with-ubuntu-12-04). Additionally, you need to have Apache installed and running on your cloud server. If you don’t already, you can install it using the following command:
sudo apt-get install apache2
If you are going to be hosting websites that rely on PHP or MySQL (e.g. Wordpress), the easiest way to setup a LAMP (Linux, Apache, MySQL, PHP) stack is to run this command:
sudo tasksel install lamp-server
## What the Red Means The lines that a user needs to enter or customize will be in red troughout this tutorial! The rest is copy-and-pastable. ## Step One – Create a New Folder/Directory The first step is to create a directory where we will store the files (and folders) for your new domain. Normally, the name of this new directory should correspond to the name of the domain you are trying to setup, but that isn’t a rule. You can name the new directory anything you want, as long as you remember what it's called, since we will be needing the directory path later for the virtual host configuration file.
sudo mkdir -p /var/www/example.com
The -p flag ensures that all the parents of this directory exist, and if they don’t it generates them. `example.com` is a placeholder address – replace it with your correct domain name. ## Step Two – Granting Permissions First, we need to grant ownership of the directory to the user Apache is running as.
sudo chown -R www-data:www-data /var/www/example.com
Next, we need to set the correct permissions for the directory so that the files are accessible to everyone.
sudo chmod -R 755 /var/www
That does it for this step. ## Step Three – Create a Page We will now create a sample index.html file so that we can test whether our virtual host is working correctly. For this step, you will want to make sure you have the nano text editor installed.
sudo apt-get install nano
Next, create the index.html file.
sudo nano /var/www/example.com/index.html
You can copy and paste the code below to the newly created index.html file.
    &lth1>Success: You Have Set Up a Virtual Host&lt/h1>
Save and exit using Ctrl+O then Enter then Ctrl+X. ## Step Four – Create a New Virtual Host Configuration File Now we will set up the virtual host configuration file. Luckily for us, Ubuntu ships with a template for this configuration file. We simply have to make a copy of that file for our use by using the command below.
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/example.com.conf
Do note that adding the `.conf` to the end is required as of Ubuntu 13.10, which differs from previous versions. ## Step Five – Modifying the Configuration File Next, we need to modify the virtual host configuration file to match our setup for the domain. Open the new configuration file.
sudo nano /etc/apache2/sites-available/example.com.conf
When you open this file, you should be greeted with a message similar to this.
< VirtualHost *:80>
	# The ServerName directive sets the request scheme, hostname and port that
	# the server uses to identify itself. This is used when creating
	# redirection URLs. In the context of virtual hosts, the ServerName
	# specifies what hostname must appear in the request's Host: header to
	# match this virtual host. For the default virtual host (this file) this
	# value is not decisive as it is used as a last resort host regardless.
	# However, you must set it for any further virtual host explicitly.
	#ServerName www.example.com

	ServerAdmin webmaster@localhost
	DocumentRoot /var/www

	# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
	# error, crit, alert, emerg.
	# It is also possible to configure the loglevel for particular
	# modules, e.g.
	#LogLevel info ssl:warn

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

	# For most configuration files from conf-available/, which are
	# enabled or disabled at a global level, it is possible to
	# include a line for only one particular virtual host. For example the
	# following line enables the CGI configuration for this host only
	# after it has been globally disabled with "a2disconf".
	#Include conf-available/serve-cgi-bin.conf
< /VirtualHost>
Modifying this file to match the configuration for our domain name is easy. Firstly, remove the `#` symbol from in front of ServerName and add your domain in front of it. Doing so should make the line look exactly like this.
ServerName example.com
If you want your site to be accessible from more than one name, with a `www` in the name for instance, you will want to add a ServerAlias line after the ServerName line.
ServerAlias www.example.com
After you have followed the above steps, you will also need to modify the DocumentRoot line to match the directory you created for your domain name.
DocumentRoot /var/www/example.com
After you have followed all these steps correctly, your file should look similar to this.
ServerAdmin webmaster@example.com

ServerName example.com

ServerAlias www.example.com

DocumentRoot /var/www/example.com
Those are all the changes you will need to make to this file. Now save and exit. To activate the host, use this command.
sudo a2ensite example.com
And now restart Apache to let your changes take effect.
sudo service apache2 restart
Submitted by: Asad Dhamani