Ubuntu – Creating additional Virtual Host Ubuntu Server

Apache2networkingservervirtualhost

I have currently one website running perfectly on my LAMP server (Ubuntu Server 16.04.3).
I now would like to add another one to the server. So i first went to the /etc/apache2/sites-available file and copied the 000-default.conf file to the same directory while changing the name to mydomainname.com.conf.

Then i edited this file to the following:

<VirtualHost mydomain.com:80>
   ServerName mydomain.com
   ServerAlias www.mydomain.com
   ServerAdmin admin.mydomain.com
   DocumentRoot /var/www/mydomain.com/public_html
   ErrorLog ${APACHE_LOG_DIR}/error.log
   CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Next i typed the command a2ensite mydomain.com.conf
Than i restarted apache.
Then I mkdir /var/www/mydomain.com/public.html (Also changed the permissions)
After this i edited the /etc/hosts file and added:

127.0.0.1 mydomain.com.
myinternalIP mydomain.com 
myStaticIp mydomain.com 

I then added the internal IP for the new site and also the public IP for the new site here.

Then I went to the /etc/network/interfaces file:

The loopback network interface
auto lo
iface lo inet loopback

- The primary network interface(this is for the virtual host already on the server)

auto eno1
iface eno1 inet static
     address 192.168.11.199
     netmask 255.255.255.0
     network 192.168.11.0
     broadcast 192.168.11.255
     gateway 192.168.11.59
     dns-nameservers 8.8.8.8
     dns-search currentsite.com

-Second IP interface(This is for the new virtual host)<br>
iface eno1:1 inet static
   address 192.168.11.200
   netmask 255.255.255.0

Output when I do an Ifconfig:

eno1: Link encap: Ethernet HWaddr xx:xx:xx:xx:xx:xx 
      inet addr:192.168.11.199 Bcast:192.168.11.255 Mask:255.255.255.0 
      inet6 addr: xxxx::xxxx:xxxx:xxxx:xxxx/64 Scope:link 
      UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 
      TX packets:6085 errors:0 dropped:0 overruns:0 carrier:364 
      collisions:381 txqueuelen:1000 
      RX bytes:563896 (563.8 KB) TX bytes:5541282 (5.5 MB) 
      Interrupt:20 Memory:f7c00000-f7c20000

eno1:1 Link encap: Ethernet HWaddr xx:xx:xx:xx:xx:xx 
       inet addr:192.168.11.200 Bcast:192.168.11.255 Mask:255.255.255.0 
       UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 
       Interrupt:20 Memory:f7c00000-f7c20000

lo    Link encap:Local Loopback 
      inet addr:127.0.0.1 Mask:255.0.0.0 
      inet6 addr: ::1/128 Scope:Host 
      UP LOOPBACK RUNNING MTU:65536 Metric:1 
      RX packets:9856 errors:0 dropped:0 overruns:0 frame:0 
      TX packets:9856 errors:0 dropped:0 overruns:0 carrier:0 
      collisions:0 txqueuelen:1 
      RX bytes:731051 (731.0 KB) TX bytes:731051 (731.0 KB) 

Output for ifconfig After i rebooted the server- I can't see the eno1:1 that i inserted in the /etc/network/interfaces file

eno1: Link encap: Ethernet HWaddr xx:xx:xx:xx:xx:xx 
      inet addr:192.168.11.199 Bcast:192.168.11.255 Mask:255.255.255.0 
      inet6 addr: xxxx::xxxx:xxxx:xxxx:xxxx/64 Scope:link 
      UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 
      RX packets:77 errors:0 dropped:0 overruns:0
      TX packets:76 errors:0 dropped:0 overruns:0 carrier:0  
      collisions:0 txqueuelen:1000 
      RX bytes:8760 (8.7 KB) TX bytes:10725 (10.7 KB) 
      Interrupt:20 Memory:f7c00000-f7c20000

 lo   Link encap:Local Loopback 
      inet addr:127.0.0.1 Mask:255.0.0.0 
      inet6 addr: ::1/128 Scope:Host 
      UP LOOPBACK RUNNING MTU:65536 Metric:1 
      RX packets:190 errors:0 dropped:0 overruns:0 frame:0 
      TX packets:190 errors:0 dropped:0 overruns:0 carrier:0 
      collisions:0 txqueuelen:1 
      RX bytes:14760 (14.7 KB) TX bytes:14760 (14.7 KB) 

Then i do ifup -v eno1:1 and then the eno1:1 comes back up when i do an ifconfig. I can also now search locally for 192.168.11.200 however it redirects to the other website on the server.

Output for service network start

failed to start network.service: Unit network.service not found.

Output for service network status

network.service
 Loaded:not-found (Reason: No such file or directory)
 Active: inactive (dead)

Outpu for ip route

default via 192.168.11.59 dev eno1 onlink 
192.168.11.0/24 dev eno1 proto kernel scope link src 192.168.11.199

Afterwards I added a new entry in the firewall(using Mikrotik Firewall) for port 80 to allow traffic to HTTP for the domain. I also restarted the firewall to be safe.

Then i went to my domain provider and created an A record pointing my static ip to the new domain and also created the WWW record pointing my static ip to the new domain. Everything is good on this side.
I have googled around but can't seem to get this right. I went through many posts and still can't see my new domain from the browser.

I have done it this way when putting up the first website on the server and everything is working correctly.

If this is answered somewhere please guide me there.

Best Answer

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 admin@example.com

   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 admin@second-example.com

   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):

Related Question