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:
- I can access
http://localhost/
(the sample, never-modified, "it works" screen appears). - I cannot access
http://resources.dev/
(the server did not mount it; http clients like browsers or ActionScript loaders cannot reach such url). - I cannot access
http://agroplasticos.dev/
(the server did not mount it; same about http clients). 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.
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: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:
Begin by checking the /etc/hosts file. If that file provides an IP address for the host name in question, it is used.
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.
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.
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.
You have to edit the /etc/apache2/apache2.conf file and add your domains in the global configuration section.
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.
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.