Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create an IP vHost in Apache

I want to create an Apache vHost that works with both the Domain Name and IP Address (specific). I tried this and it failed:

<VirtualHost IP:80>
    DocumentRoot /var/www/website/
</VirtualHost>
like image 788
Raees Iqbal Avatar asked Jul 03 '26 08:07

Raees Iqbal


2 Answers

At first Apache as 2 main modes, ip based virtualhosting or name based virtualhosting.

Now this second way works with the instruction NameVirtualhost

And the most important part on the first link given is (bold added):

It is important to recognize that the first step in name-based virtual host resolution is IP-based resolution. Name-based virtual host resolution only chooses the most appropriate name-based virtual host after narrowing down the candidates to the best IP-based match. Using a wildcard (*) for the IP address in all of the VirtualHost directives makes this IP-based mapping irrelevant.

When a request arrives, the server will find the best (most specific) matching argument based on the IP address and port used by the request. If there is more than one virtual host containing this best-match address and port combination, Apache will further compare the ServerName and ServerAlias directives to the server name present in the request.

So you should find the current NameVirtualHost instructions used on your main apache configuration (usually NameVirtualhost *:80) and instead of this single mode use several ones, something like:

NameVirtualhost 127.0.0.1:80
NameVirtualhost 10.1.1.1:80
NameVirtualhost 192.168.10.42:80

And then re-use this from in your VirtualHost top definition, associated with the right ServerName:

<VirtualHost 10.1.1.1:80>
  ServerName foo.bar.com
  DocumentRoot /var/www/website/
</VirtualHost>

Check result with :

sudo su
# Debian
. /etc/apache2/envvars; apache2 -t -D DUMP_VHOSTS
# RedHat
httpd -t -D DUMP_VHOSTS

You should get something like that, which is quite useful:

127.0.0.1:80                   is a NameVirtualHost
         default server foobar (/etc/apache2/sites-enabled/001-test:38)
         port 80 namevhost foobar (/etc/apache2/sites-enabled/001-test:38)
         port 80 namevhost project (/etc/apache2/sites-enabled/201-project:1)
         port 80 namevhost toto (/etc/apache2/sites-enabled/503-toto:1)
10.1.1.1:80                   is a NameVirtualHost
         default server tutu (/etc/apache2/sites-enabled/601-tutu:24)
         port 80 namevhost tutu (/etc/apache2/sites-enabled/601-tutu:24)
         port 80 namevhost zorg (/etc/apache2/sites-enabled/701-zorg:42)
Syntax OK
like image 65
regilero Avatar answered Jul 04 '26 22:07

regilero


Try This

<VirtualHost *:80>
  ServerName example.com
  ServerAlias IP-Address
  DocumentRoot /var/wwww/website
</VirtualHost>
like image 42
Steel Brain Avatar answered Jul 04 '26 22:07

Steel Brain



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!