James John – Software Engineer

Using and Configuring Virtual Hosts (Addon Domain) in Apache for Ubuntu 14.04

If you’ve got a VPS or your own private server, of course you won’t want to use it for a site only, you would want to add more sites because it’s a private server and you do your stuffs the way you want it. Apache offers this which is so easy, hosting multiple websites in a server, it is called Virtual Host and cPanel calls it Addon Domain but Virtual Host is the real name 😉 . When I first landed on a private server this was a very big task for me but now knowing it, there is no much deal about it, firstly I will describe the Apache directories and how Apache uses them, firstly I will let you know am using Ubuntu 14.04 for this post so directory structures might not be same in other distros. So the directories am gonna describe are the conf-available, conf-enabled, mods-available, mods-enabled, sites-available and sites-enabled:

Creating Virtual Host

I guess you now understand the works of those directories, if not please use the comment and I’ll reply ASAP. Creating the Virtual Host we are gonna create the file in sites-available and name it mydomain.com.conf (This doesn’t mean it must be your domain name, but just for you know what you are working for. Must end in .conf)

$ sudo nano /etc/apache2/sites-available/mydomain.com.conf

then paste this:

#Lets start configuring!

#Tell Apache to listen to port 80 of mydomain.com, change 80 to * if using SSL
<VirtualHost *:80>
	#---------------------REQUIRED CONFIG-----------------------
	#Tell Apache the domain you are working with
	ServerName mydomain.com

	#Don't ignore WWW
	ServerAlias www.mydomain.com

	#Tell Apache to server this directory when the domain is visited
	DocumentRoot /var/www/mydomain

	#Make file accessible to Apache
	<Directory /var/www/mydomain>
		Require all granted
		#Allow changes to be made to config via htaccess
		AllowOverride All
	</Directory>

	#Block access to .ht files from clients
	<FilesMatch "^\.ht">
		Require all denied
	</FilesMatch>

	#-----------------------OPTIONAL---------------------------------
	#Files to serve as index page when no file is specified in a directory
	DirectoryIndex index.php index.html index.htm

	#Don't expose your Server Signature for security purposes
	ServerSignature Off
</VirtualHost>

I explained the code line by line using comments inside the config, this is how Virtual Hosts are created, you can use this config as much sites as you want, just change ServerName, ServerAlias, DocumentRoot and the <Directory* tag.

We are not done yet, is this virtual host activated for serving? No, just activate with executing

sudo a2ensite mydomain.com 

This is one advantage of using Apache in Ubuntu but in other distros, use:

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

Reload Apache

$ sudo service apache2 reload

Hit the domain in the browser and surf! 🙂

James John

Software Engineer