10

I'm attempting to host multiple website locally with HTTPS enabled, but in order to do this I'd need to configure my local machine to use multiple IP address for localhost (I believe). Is there any way to have multiple IP addresses resolving locally at the same time?

I am using OS X 10.6 (standard edition - not server), and the MAMP server setup.

Thanks!

user9517
  • 114,104
  • 20
  • 206
  • 289
user1086746
  • 443
  • 1
  • 3
  • 7

3 Answers3

24

To alias localhost, you can use this terminal command to create a 'loopback':

ifconfig lo0 alias 127.0.0.2

With the localhost alias setup, you can create multiple HTTPS virtual hosts thusly:

<VirtualHost 127.0.0.1:443> ...... </VirtualHost>
<VirtualHost 127.0.0.2:443> ...... </VirtualHost>

You could also initialize these hosts on startup, if desired, through root's cron:

sudo crontab -e
@reboot ifconfig lo0 alias 127.0.0.2

Hopefully this helps anyone running into the same issues I did!

chmac
  • 977
  • 1
  • 7
  • 16
user1086746
  • 443
  • 1
  • 3
  • 7
  • 1
    Using `echo PASSWORD` is a **terrible** idea. A crude alternative would be to put something in root's cron like `sudo crontab -e` and `@reboot ifconfig lo0 alias 127.0.0.2`. There is certainly a more elegant solution, but anything that involves your password in plain text is unacceptable. – chmac Apr 10 '14 at 11:46
  • 1
    Wow, that's a lot of fun! I use this for **ssh port forwarding**. This allows forwarding the same port multiple times by listening on different local IP addresses. E.g. `ssh -L 127.0.0.2:443 foo:443 -L 127.0.0.3:443 beta.foo:443 user@host`. You can then define host names to these local IP addresses in your `/etc/hosts` file if needed. Turns out to be pretty useful for web developing and testing/remoting, especially if you can't use custom ports for your applications. – djule5 Apr 08 '16 at 19:17
4

You don't need multiple ip in order to host multiple websites on one web server. You need to use "Virtual Hosts" (with https if you need also it). Here there is a guide for virtual hosts on MAMP http://sawmac.com/mamp/virtual/

NoNoNo
  • 1,939
  • 14
  • 19
  • To host multiple sites with HTTPS enabled I believe they each need a separate IP address. See: http://stackoverflow.com/questions/11217538/mamp-2-multiple-virtualhosts-with-ssl – user1086746 Jun 27 '12 at 16:01
  • @user1086746 Apache supports TLS Server Name Indication, as do all major client browsers on.. pretty much every OS that's not Windows XP. The answer you should have gotten on your linked question is to set `NameVirtualHost *:443`. – Shane Madden Jun 28 '12 at 01:01
  • @Shane that still doesn't appear to work when dealing with multiple sites using HTTPS over the same IP address. I did post what ended up working for me. – user1086746 Jun 28 '12 at 15:12
  • @user1086746 It most certainly works; I'm guessing your `NameVirtualHost` didn't match your `` blocks, if you were binding specifically to 127.0.0.1. No matter in this case since you've found a working solution, but NoNoNo is right - you don't need extra IPs. – Shane Madden Jun 28 '12 at 15:30
  • Well, I would like to get this working 'correctly' as setting up those IP addresses are an extra step. I have 2 NameVirtualHosts like: `NameVirtualHost *:80 NameVirtualHost *:443` and have tried using vhosts like: `` and ``, but neither will provide an HTTPS connection after the first listed vhost. I could provide the full ssl.conf and vhost.conf files if you were interested! – user1086746 Jun 28 '12 at 15:43
  • @user1086746 The second part of `NameVirtualHost` should match exactly to your host setting in `` blocks. So if you have several virtual hosts set up with ``, then set `NameVirtualHost 127.0.0.1:443` in the main config before those virtual hosts are defined. That should do the trick! – Shane Madden Jun 28 '12 at 18:58
  • @ShaneMadden Alright - That did the trick! Thanks for your patience! – user1086746 Jun 28 '12 at 21:03
  • @ShaneMadden [`NameVirtualHost` is deprecated since apache 2.3.11](https://httpd.apache.org/docs/2.4/mod/core.html#namevirtualhost) – jfs Aug 26 '19 at 12:52
2

You never need to add additional IP addresses to the local host on Linux or Windows. They will respond by default, without additional configuration, to all IP addresses from 127.0.0.0/8:

$ ping 127.254.0.100
PING 127.254.0.100 (127.254.0.100) 56(84) bytes of data.
64 bytes from 127.254.0.100: icmp_seq=1 ttl=64 time=0.026 ms

So just make your application listen on any IP from the 127.0.0.0/8 range and you shall be good to go.

Example:

One console:

$ nc -vvl 127.0.34.2 9022
Connection from 127.0.0.1 port 9022 [tcp/*] accepted
Hello

Another console:

$ echo Hello | nc -vv 127.0.34.1 9022
nc: connect to 127.0.34.1 port 9022 (tcp) failed: Connection refused
$ echo Hello | nc -vv 127.0.34.2 9022
Connection to 127.0.34.2 9022 port [tcp/*] succeeded!

As a commenter noted, it is necessary to add IPs to the localhost interface explicitly on MacOS/Darwin.

the paul
  • 118
  • 4
Florin Asăvoaie
  • 6,932
  • 22
  • 35
  • I can't seem to be able to listen without the alias on the loopback interface. Trying `nc -vvl 127.0.34.2 9022` returns `nc: Can't assign requested address`. With the alias defined with `ifconfig lo0 alias 127.0.34.2`, it does work. Any ideas why that is? Running on OSX Yosemite. – djule5 Aug 06 '16 at 00:41
  • 1
    In Mac, only 127.0.0.1 is mapped to lo0 (loopback) device. You need to manually add alias to the loopback device to make it work. But in Linux, all 127.0.0.* is mapped to loopback device. – Sri Jan 10 '17 at 23:51
  • 1
    For Mac: sudo ifconfig lo0 alias 127.0.0.2 up (and so on, must be done individually for each address) – lilalinux Jul 12 '18 at 13:54