Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set up the hosts file for multiple domains/hosts with the same IP?

As you surely know you can map host names to IP addresses with the "hosts" file. This is very useful especially when you are developing as you can change localhost for project1.

That part is clear, but unfortunately I can't figure out how to set this up to open multiple projects with the same IP. I've tried a few things without success such as:

127.0.0.1/projects/project1  project1 127.0.0.1/projects/project2  project2 

I checked some related questions but I couldn't find an answer. So, can I accomplish what I'm trying to do somehow? Any suggestions?

like image 436
Vinicius Santana Avatar asked Jul 06 '13 18:07

Vinicius Santana


People also ask

Can one IP host multiple domains?

Yes, this is an extremely common practice. It is called a Shared Web Hosting: In name-based virtual hosting, also called shared IP hosting, the virtual hosts serve multiple hostnames on a single machine with a single IP address.

How do I host multiple websites on one IP?

Name-based virtual hosting is the most commonly used method to host multiple websites on the same IP address and Port. You will need valid domain names to host multiple websites using name-based virtual hosting.

Can multiple DNS names for same IP?

You can certainly have multiple domains/hostnames in DNS that resolve to the same IP address. However, whether you can differentiate this traffic in your home network will depend on the capabilities of your router.


2 Answers

The hosts file is only a mapping from names to an IP. You can specify multiple names next to an IP like this:

127.0.0.1 name1 name2 name3.domain 
like image 99
DRC Avatar answered Dec 09 '22 04:12

DRC


I got this resolved thanks to Google and the collaborators, @jvilhena and @DRC. Here's how I did it:

If you are using Windows and XAMPP as in my case the first step is to set up the 'hosts' file. If you are using Windows it's likely that you will find it in C:\Windows\System32\drivers\etc\hosts. You can use any text editor to edit it.

You can set up as many host names as you like all pointing to your localhost, with the IP, 127.0.0.1.

For example:

 127.0.0.1               local.project1  127.0.0.1               local.project2  127.0.0.1               youcanuseany.name.here 

The second step was to deal with the Apache file httpd-vhosts.conf. Again, I'm using Windows and XAMPP. It's likely this file will be in C:\xampp\apache\conf\extra\httpd-vhosts.conf.

You don't have to but I like to keep my project folders in my htdocs folder @ C:\xampp\htdocs.

For each project that you create a "host name" for, you should add the following to your httpd-vhosts.conf file:

<VirtualHost *>     DocumentRoot "C:\xampp\htdocs\projectx"     ServerName youcanuseany.name.here     <Directory "C:\xampp\htdocs\projectx">         Order allow,deny         Allow from all     </Directory> </VirtualHost>   

One more example just for the sake of it :)

<VirtualHost *>     DocumentRoot "C:\xampp\htdocs\project1"     ServerName local.project1     <Directory "C:\xampp\htdocs\project1">         Order allow,deny         Allow from all     </Directory> </VirtualHost> 

Now you can type local.project1 and youcanuseany.name.here in your browser and it should open your project as if you were typing localhost/project1 and localhost/projectX. I hope this helps.

like image 35
Vinicius Santana Avatar answered Dec 09 '22 04:12

Vinicius Santana