I have configured many virtualhosts in my current apache2 server, in my local machine (Ubuntu 13.10).

Those are different local sites, with domains which are set in my /etc/hosts:

127.0.0.1       localhost
127.0.0.1       agroplasticos.dev
127.0.0.1       resources.dev
127.0.1.1       luismasuelli-inspiron14

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

Where the agroplasticos.dev and resources.dev sites were created by me, and the previous settings were set by default.

For localhost, agroplasticos.dev, and resources.dev, I have site entries in /etc/apache2/sites-enabled directory (only those 3 entries exist in this directory), which are links to the corresponding files in /etc/apache2/sites-available:

agroplasticos.dev looks like:

<VirtualHost agroplasticos.dev:80>
    ServerName agroplasticos.dev:80
    ServerAdmin webmaster@localhost

    DocumentRoot /var/www/agroplasticos
    <Directory /var/www/agroplasticos>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride all
        Order allow,deny
        allow from all
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/agroplasticos-error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    CustomLog ${APACHE_LOG_DIR}/agroplasticos-access.log combined
</VirtualHost>

and resources.dev looks like:

<VirtualHost resources.dev:80>
    ServerName resources.dev:80
    ServerAdmin webmaster@localhost

    DocumentRoot /var/www/resources
    <Directory /var/www/resources>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride all
        Order allow,deny
        allow from all
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/resources-error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    CustomLog ${APACHE_LOG_DIR}/resources-access.log combined
</VirtualHost>

And -last but not least- localhost looks like this:

<VirtualHost localhost:80>
    ServerName localhost:80
    ServerAdmin webmaster@localhost

    DocumentRoot /var/www
    <Directory />
        Options FollowSymLinks
        AllowOverride None
    </Directory>
    <Directory /var/www/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride None
        Order allow,deny
        allow from all
    </Directory>

    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/

    <Directory "/usr/lib/cgi-bin">
        AllowOverride None
        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

What I expect is that if I access such domains, they are resolved to 127.0.0.1 and they are served by apache using the different settings for each site. In particular, resources.dev is just a static files website (it has only images I load externally via http from programs / scripts I'm making), so I don't care about even having PHP for such site. This means: I hit http://resources.dev/mirrorlings/images/sample.png in my browser, and I retrieve an image.

However -and there's the catch- the site is successfully mounted if I have network connection. If i'm not connected, then:

  1. I can access http://localhost/ (the sample, never-modified, "it works" screen appears).
  2. I cannot access http://resources.dev/ (the server did not mount it; http clients like browsers or ActionScript loaders cannot reach such url).
  3. I cannot access http://agroplasticos.dev/ (the server did not mount it; same about http clients).
  4. I'm getting this error log when running sudo service apache2 restart:

    [Sun Jul 20 15:39:55 2014] [error] (111)Connection refused: Could not resolve host name localhost -- ignoring!
    [Sun Jul 20 15:39:55 2014] [error] (111)Connection refused: Could not resolve host name agroplasticos.dev -- ignoring!
    [Sun Jul 20 15:39:55 2014] [warn] The Alias directive in /etc/apache2/conf.d/phpmyadmin.conf at line 3 will probably never match because it overlaps an earlier Alias.
    [Sun Jul 20 15:39:55 2014] [error] (111)Connection refused: Could not resolve host name agroplasticos.dev -- ignoring!
    [Sun Jul 20 15:39:55 2014] [error] (111)Connection refused: Could not resolve host name localhost -- ignoring!
    [Sun Jul 20 15:39:55 2014] [error] (111)Connection refused: Could not resolve host name resources.dev -- ignoring!
    apache2: apr_sockaddr_info_get() failed for luismasuelli-inspiron14
    apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName
    [Sun Jul 20 15:39:56 2014] [error] (111)Connection refused: Could not resolve host name localhost -- ignoring!
    [Sun Jul 20 15:39:56 2014] [error] (111)Connection refused: Could not resolve host name agroplasticos.dev -- ignoring!
    [Sun Jul 20 15:39:56 2014] [warn] The Alias directive in /etc/apache2/conf.d/phpmyadmin.conf at line 3 will probably never match because it overlaps an earlier Alias.
    [Sun Jul 20 15:39:56 2014] [error] (111)Connection refused: Could not resolve host name agroplasticos.dev -- ignoring!
    [Sun Jul 20 15:39:56 2014] [error] (111)Connection refused: Could not resolve host name localhost -- ignoring!
    [Sun Jul 20 15:39:56 2014] [error] (111)Connection refused: Could not resolve host name resources.dev -- ignoring!
    apache2: apr_sockaddr_info_get() failed for luismasuelli-inspiron14
    apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName
    

    Also do not understand why does this log appeared twice. You can see the same line block is repeated with one-second delay.

And my question here: Why do I need to have internet connection when what I want is that Apache resolved such fake domains as local, since they are in /etc/hosts? What do I have to configure in order to allow local, networkless, resolution?

I was trying to develop without having internet connection and could not hit my local servers using local /etc/hosts domain resolution to local (loopback) ip address.

  • 1
    Have you tried VirtualHost *:80 instead of VirtualHost <fdqn>:80? – muru Jul 21 '14 at 5:51
  • I'm not in that pc right now. Will that solve it? I thought <fqdn>:80 was like a pattern which was only matched for that file (i.e. I thought that, by doing what you say, every file would match every local domain) – Luis Masuelli Jul 21 '14 at 21:31
  • that was my impression too, when I was trying to add a host. But no, Apache can use only the request fdqn and the ServerName/ServerAlias values to work. And for me, using fqdns in the virtualhost spec only caused problems. – muru Jul 22 '14 at 2:04
  • I think the DNS resolver needs to look up the .dev top level domain first, to see what DNS server is authorative for it. Not finding a server for your domains, it falls back to the hosts file. – Jos Jun 19 '16 at 14:48
  • I think @muru should be right. Try with <VirtualHost *:80> and ServerName agroplasticos.dev. I have explored this case, according to this post: askubuntu.com/q/824086/566421 – pa4080 Oct 19 '16 at 17:50

You have to edit the /etc/apache2/apache2.conf file and add your domains in the global configuration section.

  • yeah but those domains do not exist except for what /etc/hosts says, and when I have internet they are resolved. When I don't have internet, they are not resolved. Why the difference? – Luis Masuelli Jul 21 '14 at 14:09
  • I will try what you say when I'm back home. Currently I don't have that pc here. – Luis Masuelli Jul 21 '14 at 14:10

Your apache configuration looks okay. And these will work just fine as a fallback if you have an internet connection.

But what you're missing is a local dns resolver, which can transfer your request to a local server, rather than making it resolve at the global internet dns. There is a mini dns server for that called DNSMASQ.

You can configure it to resolve all the .dev domains to your loopback address without consulting with the internet's dns server.

sudo apt-get install dnsmasq

You can find the necessary configuration by looking at its docs.

You have an another option to make it happen via a PAC file. Though I suggest you to stick with dnsmasq as it is much popular and easier to get this done.

It looks like your resolv.conf settings are causing your hosts file to ignored.

Recent ubuntu's have used resolveconf to manage resolv.conf because its better at working out scenarios where there are multiple connections.

General resolvconf documentation http://manpages.ubuntu.com/manpages/trusty/man8/resolvconf.8.html

Use the technique in the accepted answer here to regenerate your resolv.conf settings How do I get resolvconf to regenerate resolv.conf after I change /etc/network/interfaces?

There is another thing to look at - /etc/nsswitch.conf. You can specify the priority of hostname lookups. I have in my nsswitch.conf file the following line:

hosts:          files mdns4_minimal [NOTFOUND=return] dns mdns4

This is how to interpret it:

The hosts: line specifies the order in which various name resolution services will be tried. The default is to:

  1. Begin by checking the /etc/Hosts file. If that file provides an IP address for the host name in question, it is used.

  2. Otherwise try mdns4_minimal, which will attempt to resolve the name via multicast DNS only if it ends with .local. If it does but no such mDNS host is located, mdns4_minimal will return NOTFOUND. The default name service switch response to NOTFOUND would be to try the next listed service, but the [NOTFOUND=return] entry overrides that and stops the search with the name unresolved.

  3. Then try the specified DNS servers. This will happen more-or-less immediately if the name does not end in .local, or not at all if it does. If you remove the [NOTFOUND=return] entry, nsswitch would try to locate unresolved .local hosts via unicast DNS. This would generally be a bad thing , as it would send many such requests to Internet DNS servers that would never resolve them. Apparently, that happens a lot.

  4. The final mdns4 entry indicates mDNS will be tried for names that don't end in .local if your specified DNS servers aren't able to resolve them. I thought this was meant to catch mDNS hosts when you don't specifiy the .local TLD, but I just tried it and it doesn't work. Guess I will look into it.

Source: http://ubuntuforums.org/showthread.php?t=971693

By changing order in this line you can change the order of how hostnames are resolved. That might explain the fact that localhost cannot be resolved.

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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