VirtualHost in Apache 2.4 replaces DocumentRoot when using http://localhost - apache

I'm using Yosemite with Apache 2.4
/private/etc/apache2/httpd.conf
ServerName 127.0.0.1:80
DocumentRoot "/Library/WebServer/Documents/"
<Directory "/Library/WebServer/Documents">
Options Multiviews FollowSymLinks
MultiviewsMatch Any
AllowOverride All
Order allow,deny
Allow from all
</Directory>
Everything is fine, http://localhost directs me to the default /Library/WebServer/Documents/index.html
After adding Include /private/etc/apache2/extra/httpd-vhosts.conf to /private/etc/apache2/httpd.conf
And adding the config below to /private/etc/apache2/extra/httpd-vhosts.conf
<VirtualHost *:80>
ServerAdmin admin#xxx.com
ServerName xxx.local
DocumentRoot "/Library/WebServer/Documents/xxx"
<Directory "/Library/WebServer/Documents/xxx/">
Options +FollowSymLinks
AllowOverride all
Require all granted
</Directory>
</VirtualHost>
When using http://localhost and http://127.0.0.1 from the browser, Apache directs me to /Library/WebServer/Documents/xxx/index.html instead of /Library/WebServer/Documents/index.html, how can I fix it?, please guide.
Thanks.

I had the same problem today.
I found solution here: httpd.apache.org/docs/current/vhosts/name-based.html.
Main host goes away
Any request that doesn't match an existing is handled by the global server configuration, regardless of the hostname or ServerName.
When you add a name-based virtual host to an existing server, and the virtual host arguments match preexisting IP and port combinations, requests will now be handled by an explicit virtual host. In this case, it's usually wise to create a default virtual host with a ServerName matching that of the base server. New domains on the same interface and port, but requiring separate configurations, can then be added as subsequent (non-default) virtual hosts.

Related

This site can’t be reached - XAMPP ignores custom domain vhosts

Environment
XAMPP 7.2.4 installed on Windows 10
Apache running on ports 80 and 443
Configuration
C:\Windows\System32\drivers\etc\hosts:
127.0.0.1 www.test.local test.local
C:\xampp\apache\conf\httpd.conf:
DocumentRoot "D:/htdocs"
<Directory "D:/htdocs">
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Require all granted
</Directory>
C:\xampp\apache\conf\extra\httpd-vhosts.conf:
<VirtualHost *:80>
DocumentRoot D:/htdocs
ServerName localhost
<Directory "D:/htdocs">
</Directory>
</VirtualHost>
<VirtualHost *:80>
DocumentRoot D:/htdocs
ServerName test.local
<Directory "D:/htdocs">
Allow from all
</Directory>
</VirtualHost>
The problem
When I start Apache and I access to localhost is all right. The problem comes when I try to reach test.local: the browser returns a This site can’t be reached message. It's like XAMPP ignores the custom domain vhosts defined into httpd-vhost.conf file. The same message is displayed with the server off.
This site can’t be reached
The webpage at http://test.local/ might be temporarily down or it may have moved permanently to a new web address.
ERR_ADDRESS_INVALID
A few days ago the setup was working and the only changes that I made was the new software installation. I mean, no changes in Apache config files.
If all your stack were working correctly before, don't be like me and don't forget to check if some browser extension is blocking your local traffic, like some extension related with an antivirus system.
In my case it was the Kaspersky Protection extension. When something goes wrong, it shows the following graphic.
C:\Windows\System32\drivers\etc
127.0.0.1 test.local
C:\xampp\apache\conf\extra\httpd-vhosts.conf:
<VirtualHost *:80>
ServerAdmin webmaster#dummy-host2.example.com
DocumentRoot "C:/wamp64/www/test"
ServerName test.local
<Directory "C:/wamp64/www/hydroboost/">
Options +Indexes +Includes +FollowSymLinks +MultiViews
AllowOverride All
Require local
</Directory>
</VirtualHost>
Have you updated your C:\Windows\System32\drivers\etc\hosts file with a mapping like this this?
add this line to the end of the hosts file
127.0.0.1 test.local
the localhost domain works without this mapping, because the OS already recognizes "localhost" as a built in alias for the loopback IP address
(this suggestion is assuming that you are running and testing on the same local machine)

XAMPP Virtual Hosts not working

Before marking this as answered, please read the entire thing because yes there is a good amount of these questions but NONE of the answers on them have worked at all.
For the last 6 months I've looked at around 50 different form posts here and around the web to try and get my XAMPP virtual hosts working.
Hosts File
127.0.0.1 localhost
127.0.0.1 vws.localhost
127.0.0.1 instancegaming.net
127.0.0.1 vws.instancegaming.net
http-vhosts File (Updated 7/26)
# Virtual Hosts
#
# Required modules: mod_log_config
# If you want to maintain multiple domains/hostnames on your
# machine you can setup VirtualHost containers for them. Most configurations
# use only name-based virtual hosts so the server doesn't need to worry about
# IP addresses. This is indicated by the asterisks in the directives below.
#
# Please see the documentation at
# <URL:http://httpd.apache.org/docs/2.4/vhosts/>
# for further details before you try to setup virtual hosts.
#
# You may use the command line option '-S' to verify your virtual host
# configuration.
#
# Use name-based virtual hosting.
#
# NameVirtualHost *:80
#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for all requests that do not
# match a ##ServerName or ##ServerAlias in any <VirtualHost> block.
#
<VirtualHost *:80>
ServerName localhost
DocumentRoot "C:\xampp\htdocs"
<Directory "C:\xampp\htdocs">
DirectoryIndex index.php
</Directory>
</VirtualHost>
I'm at a loss of what to do, I've tried putting all fields to localhost then to instancegaming.net but nothing seems to work. I've read the Apache log and there's only SSL errors floating around.
When I try going to [ http:// ] vws.localhost, vws.192.168.0.47, vws.instancegaming.net all of which give the same error in chrome:
ERR_NAME_NOT_RESOLVED
Then I tried emptying chromes host cache, but that didn't help either.
Sidenote: I reinstalled XAMPP 4 times trying to make this work.
Follow these steps if you are in windows environment (7 & 10 tested) with XAMPP:
Add in hosts file [C:\Windows\System32\drivers\etc]
127.0.0.1 vws.localhost
127.0.0.1 instancegaming.net
127.0.0.1 vws.instancegaming.net
Add this in httpd.conf [C:\__Server\apache\conf] . though some says there are security risks with this but couldn't find a way without this
<Directory />
AllowOverride none
Require all granted
</Directory>
Add these in httpd-vhosts.conf [C:\__Server\apache\conf\extra]
<VirtualHost *:80>
ServerName localhost
DocumentRoot "C:\__Server\htdocs"
<Directory "C:\__Server\htdocs">
DirectoryIndex index.php
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName tools.com.at
DocumentRoot "E:\phpStorms\git\tools-class"
SetEnv APPLICATION_ENV "development"
<Directory "E:\phpStorms\git\tools-class">
DirectoryIndex index.php
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName laravel.test.com.at
DocumentRoot "E:\laravel.test.com.at\public"
SetEnv APPLICATION_ENV "development"
<Directory "E:\laravel.test.com.at\public">
DirectoryIndex index.php
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
template of this
<VirtualHost *:80>
ServerName nameInHostsFile
DocumentRoot "pathOfTheWindowsFileLocationWhichWillBeDocumentRoot"
SetEnv APPLICATION_ENV "development"
<Directory "pathOfTheWindowsFileLocationWhichWillBeDocumentRoot">
DirectoryIndex index.php
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
Note 1: My xampp installed on C:\__Server location
Note 2 : My folder name is laravel.test.com.at and its in E: drive E:\laravel.test.com.at
Note 3: Always copy folder location from address bar in windows explorer, so there is no mistakes.
Note 4: You must restart XAMPP after editing httpd-vhosts.conf every time.
Note 5: Use uncommon part in url [i.e .com.at] so that it is unique and there is no issue resolving dns.
Update: troubleshoot#Jacob Jewett
After a fresh copy of XAMPP installation in C drive -
just add/append these line in httpd-vhosts.conf file
<VirtualHost *:80>
ServerName localhost
DocumentRoot "C:\xampp\htdocs"
<Directory "C:\xampp\htdocs">
DirectoryIndex index.php
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName vws.localhost
DocumentRoot "C:\xampp\vws"
SetEnv APPLICATION_ENV "development"
<Directory "C:\xampp\vws">
DirectoryIndex index.php
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
Add append this in httpd.conf file
<Directory />
AllowOverride none
Require all granted
</Directory>
ADD PATH variable for php [C:\xampp\php], and also check that there isn't another php path in the PATH variable.
restart xampp and browse localhost
Additionally make sure that the line
Include etc/extra/httpd-vhosts.conf
is not quoted out in httpd.conf
The answer I found that works is to to this in the hosts file.
192.168.0.47 localhost somthing.instancegaming.net
One thing I'd like to add here: For anyone who is sure everything is setup correctly, make sure you don't use any underscores in the alias string within your hosts file; for some reason it can't accept underscores.
I have similar issue, vhosts not working. But in my case I found there was the permission issue with host file. Hope this helps for some one. hosts file ignored, how to troubleshoot?
If pings are successful but a Virtual Host is not working with XAMPP 7.3.1 and Windows 10 Pro the problem may be interference with the World Wide Web Publishing Service.
On my system, Apache and MySQL start successfully but the virtual host cannot be browsed till I stop the WWWPS service (in Windows Services). The virtual host then becomes instantly available. It instantly stops working when the WWWPS is started again.
I fixed the problem by just restarting my computer after setting up the System32 file hosts and the apache vhosts.

apache VirtualHost redirecting to different VirtualHost

I am very new to Apache2 and I am attempting to serve two websites on a server.
I have followed the documentation here: https://httpd.apache.org/docs/current/vhosts/examples.html
and have attempted:
<VirtualHost *:80>
DocumentRoot "var/www/websiteexample1.com"
ServerName www.websiteexample1.com
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "var/www/websiteexample2.com"
ServerName www.websiteexample2.com
</VirtualHost>
However if i attempt to go to www.websiteexample2.com I am instead directed to the content of www.websiteexample1.com
What am i missing?
Your issue may be that you have not setup an associated Directory in the main conf file? Something along the lines of :-
#======================================================================
# Note this one is for the secondary root (for www.websiteexample2.com
# This should be changed to whatever you set DocumentRoot to.
<Directory "Your 2nd Website's Directory here">
Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
You also need to tell Apache to use/look at the virtual hosts file. So if you haven't got the following, ensuring that it is not commented out and that it points to the correct location :-
# Virtual hosts
Include conf/extra/httpd-vhosts.conf
This based upon a 2.2.6 version of Apache

Wampserver shows 404 errors after swapping to using Vhosts

So I was using Wampserver for developing purposes, and I managed to set up the server for developing purposes (it worked with Dreamweaver, PHP worked etc.) but when accessing localhost, I get a 404 error.
I've hardly changed the settings, other than setting it to use that vhosts file, then I made a few new hosts in the hosts file in system 32, which all works fine.
localhost/phpmyadmin does work, however, so I am really stumped as to why it won't load anything. I also tried loading one of the site folders (localhost/Rufus) which also returned a 404. I have all the directories I need pointing to www folder, where all my files are saved.
And before anyone says, no it's not Skype or IIS, as they are both off and Wampserver is running on port 80 (I checked)
My vhosts file:
# Virtual Hosts
#
# Required modules: mod_log_config
# If you want to maintain multiple domains/hostnames on your
# machine you can setup VirtualHost containers for them. Most configurations
# use only name-based virtual hosts so the server doesn't need to worry about
# IP addresses. This is indicated by the asterisks in the directives below.
#
# Please see the documentation at
# <URL:http://httpd.apache.org/docs/2.4/vhosts/>
# for further details before you try to setup virtual hosts.
#
# You may use the command line option '-S' to verify your virtual host
# configuration.
#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for all requests that do not
# match a ServerName or ServerAlias in any <VirtualHost> block.
#
<VirtualHost *:80>
ServerAdmin webmaster#dummy-host.example.com
DocumentRoot "c:/Apache24/docs/dummy-host.example.com"
ServerName dummy-host.example.com
ServerAlias www.dummy-host.example.com
ErrorLog "logs/dummy-host.example.com-error.log"
CustomLog "logs/dummy-host.example.com-access.log" common
</VirtualHost>
<VirtualHost *:80>
ServerAdmin webmaster#dummy-host2.example.com
DocumentRoot "c:/Apache24/docs/dummy-host2.example.com"
ServerName dummy-host2.example.com
ErrorLog "logs/dummy-host2.example.com-error.log"
CustomLog "logs/dummy-host2.example.com-access.log" common
</VirtualHost>
<Directory C:\Users\Tom\sites\wamp\www>
Order Deny,Allow
Allow from all
</Directory>
<VirtualHost *:80>
DocumentRoot "C:\Users\Tom\sites\wamp\www\Rufus"
ServerName rufus.stone
</VirtualHost>
Remove the sample virtual hosts, assuming you aren't using them. You also need a virtual host for 'localhost' and it must be the first virtual host. After that you can include your other, custom vhosts.
#
# Use name-based virtual hosting.
#
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot "C:\Users\Tom\sites\wamp\www"
ServerName localhost
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "C:\Users\Tom\sites\wamp\www\Rufus"
ServerName rufus.stone
</VirtualHost>
If you get a permission denied error try putting the directive I took out into your httpd.conf:
<Directory "C:\Users\Tom\sites\wamp\www">
Order Deny,Allow
Allow from all
</Directory>
It would go right under a similar directive that includes a Deny from all on the root.
Make sure you always restart Apache whenever you make changes to these files.
The error 404 is saying that you have not granted access to those Virtual Hosts, so Apache is kicking you out.
You need to include with your Virtual Host some information to tell Apache which locations are allowed access to each VHOST
You also need to make localhost a Virtual Host and because the first VHOST is the default VHOST it should really be the first you define, and it should always be limited to allow access only from the local machine.
So see if this works any better.
# Virtual Hosts
#
<VirtualHost *:80>
DocumentRoot "C:\Users\Tom\sites\wamp\www"
ServerName localhost
<Directory "C:\Users\Tom\sites\wamp\www">
AllowOverride All
Require local
</Directory>
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "C:\Users\Tom\sites\wamp\www\Rufus"
ServerName rufus.stone
<Directory "C:\Users\Tom\sites\wamp\www\Rufus>
AllowOverride All
Require local
</Directory>
</VirtualHost>
Require local says allow access from the PC running WAMPServer only.
If later you want to allow other PC's on your internal network to see the Apache server you can add to that like this, for example.
Require local
Require ip 192.168.1
This, assuming you are on the subnet 192.168.1.? will allow any PC connected to your router (wired or wifi) access to the server.
You should avoid these, unless you actually want the world to access your site
Require all granted
OR the Apache 2.2 syntax, which still works on Apache 2.4
Order Deny,Allow
Allow from all

Apache 2 Test Page

I dont know why I see this Apache test page when I've configured my virtual host to point to my project?
Server settings
<VirtualHost *:80>
ServerName malltomobiledev.com
DocumentRoot /home/dcms/public/html/dcms/app/web
<Directory "/home/dcms/public/html/dcms/app/web">
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
Apache restarted fine, so it was access the folders fine.
My host file
Ip malltomobiledev.com
Any ideas guys?
If you're using apache 2.2, make sure you have NameVirtualHost *:80 set in your main apache config file, otherwise apache will use the default virtual host, which points to the test page.
You can check the configured virtual hosts using apachectl -S