Add virtual host and domains using a single IP

The first IP address is free with Amazon AWS. Extra IP’s are not. This how-to guides you through the process of setting up as many virtual host domain in Apache as you need on a single IP. All WordPress sites using SSL / httpS://.

Sites without SSL / httpS:// is given a lower SEO ranking by Google

We have blocked port 80 in the firewall. All sites are using port 443 with a valid SSL certificate provided by CloudFlare. The only port open in the firewall is 22 for SSH admin and 443 for the hosts. SEO ranking for non-SSL sites are lower on Google.com. Since valid  SSL certificates are free. Who would create a site without encryption?

Make sure Apache is configured correct for SSL and virtual host

Ubuntu is not SSL prepared by default. Especially not for using many SSL sites on the same IP.  We will walk you through the needed modifications here:

 

How to configure Apache for SSL with virtual hosts on a single IP

 

Adding SSL certificate from CloudFlare

Create root folders to store your content

First you need to create the website route folders on the file system where the files is to be stored.

cd /var/www/
sudo mkdir /var/www/MyDomain1.com
sudo mkdir /var/www/MyDomain2.com
sudo mkdir /var/www/MyDomain3.com

Create a test file to use when verifying that the hosting works.

sudo nano /var/www/MyDomain1.com/index.html

Write:

This is a test page for the domain https://www.mydomain1.com and/or https://mydomain1.com

Save the file and exit Nano.

Then make sure Apache can access these folders:

chown -R www-data /var/www/

Create the first site and use it as a template

To add new domains to your configuration is a quite easy task on Apache.

You just go to the folder where you find your existing config files.

cd /etc/apache2/sites-available

Then add a new config file for the new domain. Some times it’s easier to just copy an existing config file and edit it to reflect the name of the domain. To make it easier to troubleshoot problems it is a good habit to give each config file a number. This is because Apache will start one host at a time based on the number sequence.

01-MyDomain1-com.conf
02-MyDomain2-com.conf
03-MyDomain3-com.conf

To copy and the modifying an existing Apache configuration file

Stop all the active config files and running Apache hosts.

cd /etc/apache2/sites-available
sudo a2dissite *.conf

Then restart Apache so the changes takes effect

sudo service apache2 restart

First backup the default config files:

sudo zip default-apache-config-files.zip *.conf

Move the backup to your home folder:

sudo mv zip default-apache-config-files.zip /home/ubuntu/default-apache-config-files.zip

You might want to remove the non-SSL config file to keep the folder orderly:

 sudo rm 000-default.conf

Now rename the default ssl config file

sudo mv default-ssl.conf 01-MyDomain1-com.conf

Finally we will modify the the configuration file:

sudo nano 01-MyDomain1-com.conf

This is what the Apache config file looks like before you start editing:

<ifmodule mod_ssl.c>
 <virtualhost _default_:443>
 ServerAdmin mail@locahost

 DocumentRoot /var/www/html

 # 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-default-.log
 CustomLog ${APACHE_LOG_DIR}/access-default-.log combined

Modify the values in the file to look like this:

<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerAdmin mail@mydomain1.com
ServerName mydomain1.com
ServerAlias mydomain1.com
DocumentRoot /var/www/MyDomain1.com ErrorLog ${APACHE_LOG_DIR}/01-mydomain1-com-error.log CustomLog ${APACHE_LOG_DIR}/01-mydomain1-com-access.log combined 

Save the file.
Activate the config file:

sudo a2ensite 01-MyDomain1-com.conf

Restart Apache

sudo service apache2 restart

GoCheck that the configuration file is correct with these tools:

sudo apachectl configtest

sudo systemctl status apache2.service

sudo journalctl -xe

Start a web browser and go to https://mydomain1-com or https://www.mydomain1-com to verify that you can access the page you created earlier with the text:

This is a test page for the domain https://www.mydomain1.com and/or https://mydomain1.com

If this page is not displayed, you should not continue before the error is solved. Continueing will make the troubleshooting more complex.

Continuing with the 2. and 3. domain / virtual host

Now use 01-MyDomain1-com.conf as the template for the two next virtual hosts.

To copy an existing file:

sudo cp 01-MyDomain1-com.conf 02-MyDomain2-com.conf
sudo nano 02-MyDomain2-com.conf

Then edit the config to match the second WordPress site:

<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerAdmin mail@mydomain2.com
ServerName mydomain2.com
ServerAlias mydomain2.com
DocumentRoot /var/www/MyDomain2.com
ErrorLog ${APACHE_LOG_DIR}/02-mydomain2-com-error.log
CustomLog ${APACHE_LOG_DIR}/02-mydomain2-com-access.log combined

Save.
Then activate the configuration.
sudo a2ensite *.conf

Finally restart Apache so the new site gets published.

sudo service apache2 restart

Check and verify that the new site are online.

Now repeat the process above to create the 3. WordPress site:

sudo cp 01-MyDomain1-com.conf 03-MyDomain3-com.conf

Then edit the config file

sudo nano 03-MyDomain3-com.conf

Change the values so it looks like this:

<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerAdmin mail@mydomain3.com
ServerName mydomain3.com
ServerAlias mydomain3.com
DocumentRoot /var/www/MyDomain3.com ErrorLog ${APACHE_LOG_DIR}/03-mydomain3-com-error.log CustomLog ${APACHE_LOG_DIR}/03-mydomain3-com-access.log combined

Save.
Then activate the configuration.

sudo a2ensite *.conf

Finally restart Apache so the new site gets published.

sudo service apache2 restart

Check that the Apache config file is correct:

sudo apachectl configtest

sudo systemctl status apache2.service

sudo journalctl -xe

When troubleshooting then, disable, enable and activate Apache domain config files with these commands:

sudo a2ensite *.conf

sudo a2dissite *.conf

sudo service apache2 restart

 

Check and verify that the new site is online.

Check the server error logs

It’s a good practice to check the error logs. They are here:

cd /var/log/apache2/
sudo nano error.log
sudo nano 01-mydomain1-com-error.log
sudo nano 02-mydomain2-com-error.log
sudo nano 03-mydomain3-com-error.log

Set correct permissions so Apache can access

sudo chown -R www-data /etc/apache2/sites-enabled
sudo chown -R www-data /var/www/MyDomain1.com