Add static IPs (for web devel) when using DHCP on Linux

1

(This post is in two parts: top part is what I've tried, bottom half is what I'm actually trying to do.)

New notebook, Mint 16 (which uses NetworkManager), and I'm trying to just use wifi+DHCP to connect to the internet. That works fine. But I also do web development, run Apache, etc. and want to have numerous virtual hosts. I thought it was just a matter of adding some static IPs, but something very weird is going on.

Let's say the wifi router gives me 10.1.2.3, and I can see that under wlan0 using either ifconfig or ip address show. I also see eth0 without any IP addresses, and lo with 127.0.0.1. All is fine at this point. But then I do:

ip address add 10.1.2.61/8 scope global dev eth0 label eth0:61

(I've already setup an Apache virtual host on this address.) Going to http://10.1.2.61/ appears to be working. Ping works. But what has definitely broken is the internet. E.g. when I google something there is now a long time-out before it replies. Or it doesn't reply at all. (Sorry, I've not quite narrowed down when it is one or the other.)

I've also tried with:

ip address add 10.1.2.61/8 scope global dev lo label lo:61

Again this works (ping works), and again it breaks the internet.

I've confirmed that /etc/resolv.conf is not being touched. The output of route -n is unchanged when I use dev lo, and when I use dev eth0 I see an eth0 entry in there, but the wlan0 entry is still in there fine.

When I remove the added IP address (ip address del 10.1.2.61 dev eth0), the internet starts working again.

I'm not tied to this approach. I spent a good long while reading NetworkManager docs but it didn't seem to cover this scenario. Which seemed strange.

The Actual Goal

  • Be able to have multiple Apache virtual hosts on this notebook, each of which I can access with a URL (whether IP address, like http://10.1.2.61/ or a name http://mytestsite/)
  • Have the URL be constant, so I can bookmark them. E.g. http://mytestsite/some/site/part.html)
  • Have it still work both on my local LAN (where I control the router and DHCP), and out and about (when I don't).
  • When on my LAN I'd love for the test sites to be visible to other devices connecting to the same wifi router. (This is an optional requirement: this is the only bit that I am not sure is possible... I suppose if I ran all the VirtualHosts off of the wlan0 DHCP address then it works, but I don't see how to configure Apache and /etc/hosts when I don't know what DHCP address I will get in advance, and that it could change at any time as I move from network to network.)

Darren Cook

Posted 2014-04-23T07:51:52.257

Reputation: 260

I'm thinking the question title needs rewording somehow... but it's kind of a mouthful any way I try. – BeowulfNode42 – 2014-04-23T10:28:47.213

Answers

1

Apart from the last requirement you have you should be able to do all this with IP addresses in the local host range. If you set up your system to have multiple IP's in the 127.0.0.0/8 range (using alias interfaces) you can bind your Apache to those IP's:

In /etc/network/interfaces:

auto lo lo:1 lo:2
iface lo inet loopback

iface lo:1 inet loopback
ipaddress 127.0.0.11
netmask 255.0.0.0

iface lo:2 inet loopback
ipaddress 127.0.0.12
netmask 255.0.0.0

Restart your networking stack and edit your /etc/hosts file and Apache config accordingly with the names of your sites.

For the last requirement you could set up an Apache reverse proxy. The proxy can listen on all interfaces, so the DHCP interface would be automatically included. Then you can forward the URL's to the interfaces listening on 127.0.0.x.

mtak

Posted 2014-04-23T07:51:52.257

Reputation: 11 805

Thanks. The reverse proxy idea is interesting. BTW, should lo:11 and lo:12 work as labels? Or are you supposed to start them from lo:1 ? – Darren Cook – 2014-04-23T11:59:16.337

I doesn't matter. It should just be a number, doesn't have to be sequential. – mtak – 2014-04-23T12:25:34.483

@DarrenCook Did you solve your problem using this answer? – mtak – 2014-06-12T09:29:27.413

1

even simpler for you locally on your laptop is you can just put a bunch of different names in /etc/hosts and have apache listen on all ip addresses.

Apache has 3 main ways of determining which virtual host to use to serve a website.

  • IP address
  • port number
  • hostname the client uses to access the site

To make your last requirement semi practical is to have your sites listen on their own port numbers so other people on your work lan when say you receive ip address 10.2.1.54 they can access things like

http://10.2.1.54    default site
http://10.2.1.54:81 site 1
http://10.2.1.54:82 site 2
http://10.2.1.54:83 site 3
http://10.2.1.54:84 site 4
etc

They won't be able to bookmark those sites but you can bookmark

http://127.0.0.1 default site
http://127.0.0.1:81 site 1
http://127.0.0.1:82 site 2
http://127.0.0.1:83 site 3
http://127.0.0.1:84 site 4

and if you have extra names in your host file for 127.0.0.1 then you can use names instead for when you access the sites on your laptop, and if you put those entries into your DNS at home other people on your home lan can use the names too. You probably should also use a DHCP reservation for your laptop at home for this to work well.

See the site http://httpd.apache.org/docs/2.2/vhosts/examples.html for some examples on how to write this into your apache config. Note the use of the * at various locations

BeowulfNode42

Posted 2014-04-23T07:51:52.257

Reputation: 1 629

Thanks for the idea. A single address and multiple ports might just work. – Darren Cook – 2014-04-23T12:01:08.323