Multiple domains in apache2 Ubuntu16

18 July 2018 sysadminTechnology
Share with:

The idea of this tutorial is to setup a configuration with multiple domains active in a single server.

This guide is “stolen” from various sources through internet. For the example, we’ll setup the site example.com to run on a server with IP 1.2.3.4 .


We assume that you have already installed apache2 all the required modules, php etc and the web server is running. First step is to create the desired directory structure:

sudo mkdir -p /var/www/html/example.com

Next we need to provide ownership to the apache2 account:

chwon -R $user:$user /var/www/html/example.com

You need to substitute $user with the web user, which can be seen in apache configuration. Set proper permissions:

chmod -R 755 /var/www/html/example.com

Next create some basic index file.

vi /var/www/html/example.com/index.php

Fill some bulk info – I usually create one containing phpinfo, as it provides useful information:

<?php
echo '<h1>Work in progress</h1>';
phpinfo();
?>

Next we need to create the proper configuration for the new site:

cd /etc/apache2/sites-available
sudo cp 000-default.conf example.com.conf

Then add the correct configuration:

<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html/example.com
<Directory /var/www/html/example.com>
Options Indexes FollowSymlinks MultiViews
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Next we need to enable the newly created config file

sudo a2ensite example.com

Add some neccessary configuration to the hosts file:

sudo vi /etc/hosts/
1.2.3.4 example.com

And finally – restart apache, so settings can take effect.

sudo service apache2 restart

The above steps were done just before enabling this site on my staging server, (Ubuntu 16) so I assume this should work. Good luck!