Welcome

Multiples Sites on Multiple IP Addresses

One of the easiest ways to support multiple sites on a single web server is to use multiple IP addresses. While this is a good approach to take if your server is just on your LAN, the cost of a second IP address on the Internet probably isn’t worth the price and you would be better off using of the other multi-site techniques that I will show in a later page.

In terms of learning how to manage virtual hosts on a LAN, this is a relatively easy way to get started since the configuration of these sites has minimal places where things can go wrong.

Multiple IP Addresses

There are two ways to get multiple IP addresses on your Linux server, either have multiple network cards on your server or put multiple IP addresses on one network card.

The video shows the process of adding a second IP address to my eth0 network card. The configuration is quite simple if you have already converted your machine to use a static IP address.

Let’s modify the /etc/netplan/00-installer-config.yaml file:

Plain Text

This server is now configured with addresses 192.168.68.12 and 192.168.68.13. Once you modify the file you will need to run the command ‘netplan apply’.

You should run ‘ip addr show’ to make sure that both IP addresses appear and it is a good idea to try a ping test from another computer to make sure that your server is responding to both addresses.

HTML Folders

We still a folder for each website that we are going to create. Luckily we have the “red” and “blue” websites from the previous video. If you do not have these sites, you should of course go and make folders named /var/www/red and /var/www/blue and put some content into the folder.

Apache Configuration Files

The actual changes to the files in sites-available is very trivial but we will use this time to remind ourselves how apache deals with multiple websites.

Let’s look at the 000-default.conf file provided by Apache:

Plain Text
/etc/apache2/sites-available/000-default.conf

The main “container” in this configuration file is the “VirtualHost”. Apache uses the term VirtualHost to support the concept of running multiple sites. When a request comes into the server apache will look at all of the virtual hosts that are currently enabled and use the one that matches best. If there are multiple matches then it uses the first one it found (remember that configuration files are loaded alphabetically).

The VirtualHost entries that we have created so far always have the first line “<VirtualHost *:80>”. The item in front of the colon (:) is the IP address that apache will try to match up. The entry of * simply means that it matches “all” IP addresses on the host. If you enabled just the blue website and everything else, it would be served up when you visited both of the IP addresses on your server.

We will now fix the configuration files for both the red and the blue website to match the picture shown at the top of this document by modifying the configuration files in the “sites-available” folder.

Plain Text
/etc/apache2/sites-available/red.conf
Plain Text
/etc/apache2/sites-available/blue.conf

Now that the files are changed we just need to reload the configuration file using ‘systemctl reload apache2’ and then we should be able to visit the sites using the different IP addresses.

A second look at 000-default

The usefulness of the default site now becomes a bit more obvious. Suppose we leave the 000-default.conf site as it is (*:80). If we now disable the blue site (because perhaps we are doing some maintenance on it) and a request comes in for the 192.168.68.13 IP address, apache would look at the 2 virtual enabled sites and decide to serve up the 000-default one for the request while the 192.168.68.12 requests would still be handled by the red website.

In reality we would probably handle maintenance in a different way (show some ‘site down’ message, but you can see from the example if you misconfigured the server at least the default something will be showing.

Logs

You will notice that all the virtual hosts are currently using the same log files. I promise that the logging configuration will be done in a later page (it is in the roadmap). The only reason why I am including the log file lines in the configuration files is because they are part of the default configuration file that is provided by apache.

Leave a Reply

Your email address will not be published. Required fields are marked *