-2

I have a site working on a VM with Debian and Apache. The site is sitename.example.com . Now, I need to add a subdomain like subdomain.sitename.example.com on the same IP.

What have I done until now:

000-default.conf

<VirtualHost 192.167.102.49:80>

ServerAdmin webmaster@localhost
DocumentRoot /home/iamanuser/www/subdomain/public_html
ServerName subdomain.sitename.example.com
ServerAlias subdomain.example.com

</VirtualHost>

apache2.conf

<Directory /home/iamanuser/www/subdomain/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
<VirtualHost 192.167.102.49:80>
ServerName www.subdomain.sitename.example.com
DocumentRoot /home/iamanuser/www/subdomain/public_html
</VirtualHost>

etc/hosts

192.167.102.49 sitename.example.com
192.167.102.49 subdomain.sitename.example.com

sitename.example.com works perfect, but everytime I go for subdomain.sitename.example.com the DNS can't be resolved. I'm literally going crazy, please help me. Thanks in advance.

Jenny D
  • 27,358
  • 21
  • 74
  • 110
  • /etc/hosts is not DNS. Editing /etc/hosts only helps on the system trying to connect to that hostname. – wurtel May 06 '15 at 10:00
  • have you tried a `dig` or `nslookup` on the subdomain? – user3779430 May 06 '15 at 09:53
  • This is nslookup Server: 192.167.101.10 Address: 192.167.101.10#53 ** server can't find subdomain.sitename.domain.com: NXDOMAIN dig doesn't give an answer... – Mechromancer May 06 '15 at 10:08
  • @wurtel; `/etc/hosts` is DNS just fine; it is just not a server-based solution and only works locally (so the new subdomain name won't get known to machines that lack this change to `/etc/hosts`) – umläute May 06 '15 at 11:53
  • 1
    @Mechromancer: `nslookup` and `dig` andthe like are tools to query DNS-servers; since you don't use a DNS-server but `/etc/hosts`, you cannot use these tools; but running `ping subdomain.sitename.domain.com` on that machine should give you a clue whether things work as expected. – umläute May 06 '15 at 11:56
  • @umläute: yeah, basically I have switched my problem to: is it possible to resolve both sitename.domain.com and subdomain.sitename.domain.com by using the same DNS? Let's say, currently, I am not able to register a DNS for subdomain. Can I resolve it by using the DNS of sitename and reach both sitename and subdomain? – Mechromancer May 06 '15 at 12:02
  • on what level do you want your change to happen? if you are only interested in the `subdomain.example.com` available to yourself (e.g. for testing), then `/etc/hosts` is a good solution (but see my answer). But if you want others to be able to access subdomain you need a separate entry on the official *DNS server* for your domain. – umläute May 06 '15 at 12:06
  • Have a look at http://serverfault.com/a/520201/120438 - step 2 is the one that's failing for you, it has nothing to do with your apache config. – Jenny D May 06 '15 at 12:31

2 Answers2

1

Are you trying to access this from the machine that has these entries in /etc/hosts?

You might also want to check /etc/nsswitch.conf to check that 'hosts' comes before 'dns' etc.

You can use getent hosts subdomain.sitename.domain.com to see what would be returned to an application when it does a lookup (do this from your [Linux] client)

Cameron Kerr
  • 3,919
  • 18
  • 24
  • No, I am accessing from another machine. The VM is geographically in the other side of the city. In nsswitch.conf I have hosts: dns files, while getent returns 192.167.102.49 – Mechromancer May 06 '15 at 10:35
  • put 'files' before 'dns' and try again – Cameron Kerr May 06 '15 at 10:36
  • What were you using to test with? Some browsers may require a bit of a restart post changes to /etc/hosts. – Cameron Kerr May 06 '15 at 10:38
  • I'm using Chrome. Also, the changes to nsswitch.conf changes nothing. Still doesn't resolve dns. – Mechromancer May 06 '15 at 10:40
  • And is Chrome running inside your VM or outside? Are you editing /etc/hosts inside the client AND running Chrome from the client? Remember, /etc/hosts is ONLY relevant to client running on that same hsot. If you use `curl -v -i http://subdomain.sitename.domain.com/` on your VM, what do you get? What OS is the machine you're running Chrome on? – Cameron Kerr May 06 '15 at 10:45
  • Oh, this changes everything. Chrome is outside (it's my laptop, not the VM), I can access the VM ONLY via Putty. Also, curl -v -i returns me something interesting: *Hostname was NOT found in DNS cache *Trying 192.167.102.49 *Connected to subdomain.sitename.domain.com – Mechromancer May 06 '15 at 10:55
  • Then, assuming Windows, you will need to edit (as as admin) C:\Windows\system32\drivers\etc\hosts, and add a port forward (assuming you're using NAT'd connection for your VM) so that port 80 can be directed to your guest, and adjust any firewall rule inside the guest to allow connections to port 80. – Cameron Kerr May 06 '15 at 10:58
  • Mh, why should I do this? I need subdomain to be accessible to all users just like sitename. How can I accomplish this? – Mechromancer May 06 '15 at 11:01
  • Then you need to get it registered in DNS so everyone can find it. The instructions I gave should be sufficient to let you access it from Chrome, assuming you were working with a development environment and not running a production website on a VM hosted on a laptop (this is serverfault, after all). – Cameron Kerr May 06 '15 at 11:04
  • I see, is there a workaround to resolve both sites with the same DNS? Thanks a lot for help. – Mechromancer May 06 '15 at 11:51
0

chrome is known for ignoring /etc/hosts file.

There's an answer on superuser that tells you how to fix that:

- Go to chrome://settings

- Click on "Show advanced settings..."

- Unmark the Predict network actions to improve page load performance box.

- Check if the DNS prefetching is really disabled by going to 
chrome://dns. You should see there something like DNS 
pre-resolution and TCP pre-connection is disabled. If the 
prefetching is not disabled, you'll see some tables there.
umläute
  • 469
  • 1
  • 7
  • 26