If I have 2 web domains that have the same ip address, how should I test in browser for each web domain if by just typing the ip address?

I am currently using Ubuntu 20.04 and a newbie, I have made 2 web domains: site1.com and site2.com, both of them having the same ip address, but when I type the ip address on the browser, only site1.com appears. If I want to get site2.com to appear using the same ip address, how should I do that?


Solution 1:

One of the ways around this is to edit the /etc/hosts file on your local machine to point some internal network addresses to the same IP address.

For example, you can edit your /etc/hosts file to include these lines:

127.0.0.1       site1.local
127.0.0.1       site2.local

Note: Be sure to replace 127.0.0.1 with the actual IP address. For the names, you can put almost anything, but do yourself a favour and do not use the common TLDs like .com or .net. It can create confusion later.

With the hosts file updated, you can now edit your Apache config files for the site. For example, site1.com might look like this:

<VirtualHost *:80>
        ServerAdmin [email protected]
        DocumentRoot /var/www/site1.com

        ServerName site1.com
        DirectoryIndex index.php index.html

        ErrorLog ${APACHE_LOG_DIR}/site1-error.log
        CustomLog ${APACHE_LOG_DIR}/site1-access.log combined
</VirtualHost>

Add a ServerAlias record that has the domain name you created in /etc/hosts. Generally, I write this to the line immediately after ServerName so that it looks something like:

ServerName site1.com
ServerAlias site1.com site1.local
DirectoryIndex index.php index.html

Save the file, then restart (or reload) Apache:

sudo service apache2 restart

Then you can go to your browser and access the sites using site1.local and site2.local (or whatever you assigned).