0

I am working on a client's website which is currently hosted on an external remote server. I downloaded a copy of the site to my local computer, which is running a 'LAMP stack' set up on Arch Linux, to use as a development version.

The site is built with WordPress, so the site's domain is stored in the database and this is used whenever links are generated on the site. So, to save from editing the database I configured the site in Apache with a virtual host and set the server's domain name to be the same as that of the 'live' site, e.g. in my 'vhosts.conf' file...

ServerName    live-domain-name.co.uk
ServerAlias   *.live-domain-name.co.uk


I also added the domain to my local 'hosts' file as:

127.0.0.1    live-domain-name.co.uk


What I would like to be able to do is access both sites from my browser, at least so that if I have Apache running I get my local copy of the site, but if Apache isn't running then I get the 'real' version of the site!


I have added 127.0.0.1 as the first 'nameserver' entry in my /etc/resolv.conf file, the second being my internet router's IP and I also have one of Goolge's public DNS servers as the third. However even when Apache is not running $ ping ... still shows the IP for the domain as 127.0.0.1!


I'm guessing that I need some kind of DNS resolver that will check my localhost first but if the site is not found (i.e. Apache is not running) then it will fall-back to using one of the other DNS servers! -- only I have no clue about installing and configuring a DNS server/forwarder...

Can anyone help me please?

  • I honestly thought that this would be/should be 'simple'... I mean "look here first, then if it's not found look here instead"... BUT I think I'm really failing to understand how DNS works! :( – Chris Dec 5 '13 at 12:23
2

I think the method suggested by @piercedRichard (i.e. delete entry from /etc/hosts when apache is down) is workable...

but I can also think of a twisted way (using iptables) to route the ip address of live-domain-name.co.uk back to localhost. I think this will be a lot more robust - because DNS resolution will be cached and apart from fixing /etc/hosts you'd also have to flush dns 'caches' at the OS layer and in some browsers (firefox for example). Changes to iptables should be instant ...

perhaps something like (redirect to local):

iptables -t nat -A OUTPUT -p tcp --dst live-ip --dport 80 -j DNAT --to-destination 127.0.0.1:80

delete rule (stop redirection):

iptables -t nat -D OUTPUT -p tcp --dst live-ip --dport 80 -j DNAT --to-destination 127.0.0.1:80

As far as detecting whether apache is up or not -- either put the iptables rules in your /etc/init.d/apache2 file --
or write a script that tries connecting to localhost:80, if ok, then create rule else delete rule...

  • Nice thinking. I never would have thought about using iptables. – Mat Carlson Dec 4 '13 at 22:51
  • I was hoping to find a solution that didn't require me to edit something each time I wanted to change between the local and live sites, something that would just 'work' :D -- However if I am going to have to edit something manually each time then I might as well just comment/un-comment the appropriate line in the '/etc/hosts' file as this is admittedly rather trivial. -- Your solution does indeed offer some benefits however and +1'd for it's technical merit alone! :) – Chris Dec 5 '13 at 12:19
0

Most OSes will resolve from their hosts file before they try finding a DNS server. If you remove (or comment out) that line from hosts, it will then query the DNS server and find that it's a remote IP.

You may also have to change the hostname, if it's set to the same, as hostname may be used to resolve IPs as well (at least by some systems in Linux).

hosts.conf man page - See more information about the order directive.

  • There is an option in the DNSmasq config file to prevent it from reading the 'hosts' file ("no-hosts"), surely this implies that the DNS is processed first? -- However my 'host.conf' does have the order directive configured as 'hosts,bind'! – Chris Dec 5 '13 at 12:12
  • 1
    My alternative answer is to use a separate server alias like live-domain-name.co.uk.int instead - So your test site is the non-public domain, and your live site remains unchanged. You can also create test.live-domain-name.co.uk, which has a DNS A record with the IP of your testing server - You can then access it anywhere you can access the server. Most companies run several subdomains for testing - Sometimes live on the public web, but behind a password prompt. – Mat Carlson Dec 5 '13 at 16:04
  • 1
    DNSMasq operates as a DNS server, whereas Bind operates as a DNS client on your system (unless you install/enable the server component). Your computer is probably set to read hosts, then Bind. Bind then checks with your primary DNS server, DNSMasq on itself, which then reads your host file for entries before forwarding the request out (if not found in hosts). – Mat Carlson Dec 5 '13 at 16:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.