Creating additional Virtual Host Ubuntu Server

1. You do not need second IP address to serve more than one virtual host on the server. Something more as I understood correctly you have only one static IP. So if you don't have any other special requirements I would advice you to revert the changes related to the network interfaces.

2. It is not mandatory to do anything with /etc/hosts. This file serves as local DNS and if you want to access your domain name (FQDN) locally (from the machine itself, when you do not have internet access or DNS setup) an entry as the next is enough:

127.0.0.1 localhost example.com www.example.com second-example.com www.second-example.com
  • where 127.0.0.1 is the address of the loopback interface that normally is bound to the localhost domain name.

You can edit the hosts file at the other LAN connected PCs in order to access your LAN server via domain name instead of an IP address. For this purposes you need to enter the LAN IP of the server within the hosts files of the other LAN connected PCs:

192.168.0.111 example.com www.example.com second-example.com www.second-example.com
  • replace 192.168.0.111 with the actual server's LAN IP address.

3. How the virtual hosts work?

On the client side, when you type any FQDN in the browser (for example http://example.com) it checks[1] whether there is a record for this FQDN in /etc/hosts. If there is not presented such record, it asks your DNS (provided in /etc/network/interfaces or in /etc/resolv.conf, or by DHCP) for the IP address of the requested FQDN. And the DNS returns the A record for the requested domain name. Then the browser sends HTTP request to the provided IP address. The request's header contains the IP, the fully qualified domain name, etc.

  • [1] not exactly the browser, but I do not want to extend the answer.

On the server side, when a request arrives to the server's IP address on certain port this request will be handled by the service that listen to this port. The default HTTP/HTTPS port is 80/443 and Apache listen to it/them - this is defined in /etc/apache2/ports.conf.

When Apace handle the request it reads the request header and redirects the request to the virtual host that correspond to the domain name in the request header.

4. Do not use the VirtualHost tag in this way: <VirtualHost mydomain.com:80>. This is a typo. Instead, use it as it is by default: <VirtualHost *:80>. Actually the asterisk * means all available network interfaces (IP addresses handled by the server).

5. According to the above your Apache's configuration should look like this:

<VirtualHost *:80>
   ServerName example.com
   ServerAlias www.example.com
   ServerAdmin [email protected]

   DocumentRoot "/var/www/example.com/public_html"

   ErrorLog ${APACHE_LOG_DIR}/example.com.error.log
   CustomLog ${APACHE_LOG_DIR}/example.com.access.log combined
</VirtualHost>

<VirtualHost *:80>
   ServerName second-example.com
   ServerAlias www.second-example.com
   ServerAdmin [email protected]

   DocumentRoot "/var/www/second-example.com/public_html"

   ErrorLog ${APACHE_LOG_DIR}/second-example.com.error.log
   CustomLog ${APACHE_LOG_DIR}/second-example.com.access.log combined
</VirtualHost>
  • Usually the definitions of the different virtual hosts are placed in separate .conf files to be a2ensite/a2dissite easily.

  • It is not mandatory to separate the log files (at it is shown in the example). This is just another idea.

  • In the question is written ServerAdmin admin.example.com, you must provide email with this directive.

6. If everything works fine you can go further and setup free HTTPS (SSL/TLS) certificate by the help of Let's encript (Certbot):

  • Let's Encrypt, Apache2 - Editing vhosts properly

  • When does Ubuntu 16.04 use /etc/apache2/ssl/apache.crt?

  • Failed to upgrade certbot on Ubuntu Bionic