Add Multiple Domains(virtual Hosts) on Apache Ubuntu

Date Posted:04-04-2017

Apache popular well known webserver which can be used to serve multiple domains on a single server. This post assumes that apache is already installed and we will deal with how to add multiple domains in it.

Assumption:
  1. Ubuntu Host
  2. Apache Webserver
  3. Two Domains(Both domains must be pointed to server IP address). In our post, we use domain1.tld  and domain2.tld

Incase if apache is not installed, follow below post to install apache.

Install Apache on Ubuntu

Implementation:

Create the domain documentroot directory for both domains.

mkdir /var/www/domain1.tld

mkdir /var/www/domain2.tld

Change the permission of the directories to apache2 user.

chown www-data. /var/www/domain1.tld

chown www-data. /var/www/domain2.tld

Create virtualhost configuration for each domains.

cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/domain1.tld.conf

cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/domain2.tld.conf

There are multiple parameters which you can tweak for each domain. We only deal with minimal parameter.

Open the domain1.tld and modify /update the following lines under <Virtulahost *:80>

vi /etc/apache2/sites-available/domain1.tld.conf

ServerAdmin admin@domain1.tld
ServerName domain1.tld
ServerAlias www.domain1.tld
DocumentRoot /var/www/domain1.tld

Open domain2.tld configuration and modify/update the following lines under <Virtulahost *:80>

vi /etc/apache2/sites-available/domain2.tld.conf

ServerAdmin admin@domain2.tld
ServerName domain2.tld
ServerAlias www.domain2.tld
DocumentRoot /var/www/domain2.tld

Enable the both the sites.

a2ensite domain1.tld

a2ensite domain2.tld

Check for any syntax error on configuration files.

apachectl -t

If there is no error then restart apache service to make the changes comes into effect.

service apache2 restart

Upload the website contents and check the page. If the domains is already pointed to server IP address then now both domains would work identical.

Incase if the domains are not pointed to the server , we can test it by adding the domains on your local /etc/hosts incase of linux/Mac Machine.

Open the host file and add the following lines.

vi /etc/hosts

xx.xx.xx.xx domain1.tld

xx.xx.xx.xx domain2.tld

Access both the domains on the browser and we can see different contents.

 

 

Leave a Reply