Problem with Virtual Host ( Virtual Host throws domain name not found error , if we call it from other computer )

March 29, 2016 276 views
Apache DNS VPN Ubuntu

My Question :

I would like to host two web application into single server but i am not having domain name but i have IP Address. and it is an Remote Server .

I use LAMP and i have created two Virtual Hosts for separate two web application and i have enabled that also.Suppose if my Server IP is 192.168.XXX.XXX , then

My first Virtual host and Second one is same as first but project name will be changed

<VirtualHost *:80>
ServerAdmin webmaster@myfirstproject
ServerName 192.168.XXX.XXX.myfirstproject
ServerAlias 192.168.XXX.XXX
DocumentRoot /var/www/html/myfirstproject/public
<Directory "/var/www/html/myfirstproject/public">
AllowOverride all
Allow from all
Require all granted
ErrorLog ${APACHELOGDIR}/error.log
CustomLog ${APACHELOGDIR}/access.log combined

If i call with 192.168.XXX.XXX.myfirstproject/ in browser it shows Domain not found error , but i did the same process in my local machine , it work fine , why its not working if we called from other computer( Except locally ) and this is my
/etc/hosts/ file content localhost

192.168.XXX.XXX 192.168.XXX.XXX.myfirstproject
192.168.XXX.XXX 192.168.XXX.XXX.mysecondproject

can you please help me where i am going wrong.

what my expected output is If i type 192.168.XXX.XXX.myfirstproject in browser it will load the content for myfirstproject and 192.168.XXX.XXX.mysecondproject in browser it will load the content for mysecondproject , But it working locally , but if i accessed from other computer it wont work , that was my problem .

1 Answer

There are three types of virtual hosts apache supports. Name-based, port-based and ip-based.

Name based: This requires a domain name and uses the requested domain name to decide which site to show. You can get around this by making up a name and adding it to the hosts file on your local computer (not the server). On windows the hosts file is in C:\Windows\system32\drivers\etc while on Mac or Linux it is /etc/hosts. The name in /etc/hosts must be pointed to your droplet's IP address.

Port based: You can run different sites on different ports but ports other than 80 and 443 would require you to specify the port in the URL. Do this by creating different virtualhost blocks for different posts. Your example shows a configuration for port 80 <VirtualHost *:80>

IP based: If you have more than one IP address available you can specify different sites for different IPs.

Looking at your configuration you are indicating 192.168 addresses for your remote services. 192.168 addresses are not public, these are LAN only IPs. Your droplet's public address will not look like this.

Have another answer? Share your knowledge.