Apache, Make Shared Hosting (single) IP open a particular domain

This is based on Apache on Ubuntu.
The server has a single IP Address and is used to host multiple domains using VirtualHost
If it is needed that on using the IP address of the server (in a browser) a particular site/domain get opened, then do the following:

  • Create a vhost file with the IP address as name e.g 192.168.0.1.vhost
  • Add the necessary VirtualHost configuration. It can be copied from the domain’s vhost file
  • The ServerName in the vhost file will be the IP. e.g. ServerName 192.168.0.1
  • Create a link of the vhost inside sites-available folder to a proper file in sites-enabled folder. e.g.
    ln -s /etc/apache2/sites-available/192.168.0.1.vhost ../sites-enabled/192.168.0.100.vhost
  • Restart apache

Please note adjustments might be necessary in the .htaccess file. Like if non-www to www redirection is enabled or http to https redirection is enabled, they will give error and warning (respectively) when IP is used to access the site. Adding

RewriteCond %{HTTP_HOST} !192\.168\.0\.1

before the existing condition ( e.g. RewriteCond %{HTTP_HOST} !^www\. ) should help.

Leave a Reply