1

I have been following this steps setup apache virtualhost (windows) to create a Virtual Host in Windows but I don't know what is wrong because it doesn't work.

I want a virtual host with this url: http://local.shop.

httpd-vhosts.conf

<VirtualHost *:80>    
DocumentRoot "C:/Apache24/htdocs/"
ServerName localhost
ServerAlias localhost
<Directory "C:/Apache24/htdocs/">
    AllowOverride All
</Directory>

<VirtualHost *:80>    
    DocumentRoot "C:/Apache24/htdocs/shop/"
    ServerName local.shop
    ServerAlias local.shop
    <Directory "C:/Apache24/htdocs/shop/">
        AllowOverride All
    </Directory>
    ErrorLog "logs/localhost.html-error.log"
    CustomLog "logs/localhost.html-access.log" common
</VirtualHost>

hosts

    127.0.0.1   localhost
    127.0.0.1   local.shop

If I try to access to http://localhost the page served is the page which is on C:/Apache24/htdocs/shop/ but if I try to access to http://local.shop I've got the next error ERR_NAME_NOT_RESOLVED

What am I doing wrong?

Edit 1:

I have removed ServerAlias directives and now If I try to access to http://localhost the page served is the right page, but if I try to acces to http://local.shop still doesn't work. I've got the same error ERR_NAME_NOT_RESOLVED

Edit 2: I have used "ping" command from windows to try to reach to each host. And I get a response from localhost but not from local.shop

enter image description here enter image description here

Edit 3: I have made a change in my definition of localhost in httpd-vhosts.conf. I have changed DocumentRoot to "C:/Apache24/htdocs/shop"

<VirtualHost *:80>    
    DocumentRoot "C:/Apache24/htdocs/shop"
    ServerName localhost
    ServerAlias localhost
    <Directory "C:/Apache24/htdocs/">
       AllowOverride All
    </Directory>
</VirtualHost>

Instead of getting the default page from shop directory, I still getting the default page from original localhost. It seems like Apache ignore httpd-vhosts.conf file.

halfer
  • 19,824
  • 17
  • 99
  • 186
José Carlos
  • 2,850
  • 12
  • 59
  • 95

2 Answers2

0

Apache maybe listening (in case you restarted it after editing vhosts file), but nothing points to it. You need to edit your hosts file (located under C:\Windows\System32\drivers\etc\hosts) and point domain "local.shop" to IP address 127.0.0.1 so Apache can pickup from there. You may edit that file with Notepad, opened with admin rights.

It should looks something like this:

# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
#      102.54.94.97     rhino.acme.com          # source server
#       38.25.63.10     x.acme.com              # x client host

# localhost name resolution is handled within DNS itself.
#   127.0.0.1       localhost
#   ::1             localhost
127.0.0.1   local.shop
Nemanja K.
  • 131
  • 6
0

I encountered this same issue installing Apache 2.4 on Nov. Tuesday 2nd, 2021 at 2:37 pm.

Things you need to do if you are installing Apache without WAMP or LAMP:

  1. If you do not want to do much configurations, unzip your downloaded files on c:\Apache 2.4 as many tutorials asked you to do.
  2. If you want to use httpd command variable you need to add c:\Apache 2.4\bin into your Environment Variables path.
  3. THIS IS WHAT MOST PEOPLE FAILED TO ADDRESS :: If you are going to create Virtual Hosts you need to open c:\Apache 2.4\bin\conf\httpd.conf in your favorite text editor or IDE and look for the line or statement #Include conf/extra/httpd-vhosts.com and uncomment it, which means removing the # (pound) sign at the beginning. At the time of this writing is line 510. It should look as follow:
509 #Virtual hosts
510 Include conf/extra/httpd-vhosts.conf
  1. You need to open you text editor as admin and edit c:\windows\system32\drivers\etc\hosts file and add all your virtual hosts as so, you get the idea:
127.0.0.1    vhOne.com #your virtual host server name
127.0.0.1    vhTwo.com #your second virutal host server name

After doing that close any terminal and open that again, and you should be able to go to your terminal, without having to cd to the Apache directory all the time, just by typing:

> httpd -k install
> httpd -v
> httpd -k restart

You should also be able to ping your virtual host from the terminal and expect the response desired:

ping vhOne.com

And you should be able to visit your virtual host URL. Keep in mind that at first most browsers won't recognized 127.0.0.1 URL to be web addresses but search string so you have to type http://vhOne.com with the http:// in the URL. After that first visit you can go back by typing the server name without the http://

Nimantha
  • 6,405
  • 6
  • 28
  • 69
raphie
  • 3,285
  • 2
  • 29
  • 26