Virtual host not working

Problems with the Windows version of XAMPP, questions, comments, and anything related.

Virtual host not working

Postby Ennex » 07. April 2015 02:23

Hi,

I installed XAMPP and confirmed that Apache and MySQL were working fine. Then I made a change to move the website files outside of the XAMPP program folder. My understanding is that the best way to do this is by setting a virtual host. I did the following:

• Stopped Apache and shut down XAMPP.
• Made a backup copy of “…\XAMPP\apache\conf\extra\httpd-vhosts.conf” and “C:\Windows\System32\drivers\etc\hosts”.
• In “…\XAMPP\apache\conf\extra\httpd-vhosts.conf”:
1. Removed the "## " at the beginning of the line, "## NameVirtualHost *:80"
2. Added the following lines at the end of the file:
<VirtualHost *:80>
DocumentRoot "C:/.../Web root/XAMPP/htdocs"
ServerName localhost
<Directory C:/.../Web root/XAMPP/htdocs>
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
(I tried four different versions of the above: with versus without the quotation marks in the DocumentRoot setting and with forward versus backward slashes in both DocumentRoot and <Directory ...>. Same results all four ways.)
• In “C:\Windows\System32\drivers\etc\hosts”, added the line: "127.0.0.1 localhost", which is the only uncommented line in the file.
• Put a copy of “htdocs” in "C:/.../Web root/XAMPP".
• Renamed the original “htdocs” folder to “htdocs_bak” and added a shortcut in it to the new location (for documentation).
• Started up XAMPP.

MySQL started, but Apache did not. The XAMPP Control Panel log reported the following:

16:56:22 [main] Initializing Control Panel
16:56:22 [main] Windows Version: Windows 7 Professional SP1 64-bit
16:56:22 [main] XAMPP Version: 5.6.3
16:56:22 [main] Control Panel Version: 3.2.1 [ Compiled: May 7th 2013 ]
16:56:22 [main] Running with Administrator rights - good!
16:56:22 [main] XAMPP Installation Directory: "c:\<install root>\xampp\"
16:56:22 [main] Checking for prerequisites
16:56:22 [main] All prerequisites found
16:56:22 [main] Initializing Modules
16:56:22 [main] Enabling autostart for module "Apache"
16:56:22 [main] Enabling autostart for module "MySQL"
16:56:22 [main] Starting Check-Timer
16:56:22 [main] Control Panel Ready
16:56:23 [Apache] Autostart active: starting...
16:56:23 [Apache] Attempting to start Apache app...
16:56:23 [mysql] Autostart active: starting...
16:56:23 [mysql] Attempting to start MySQL app...
16:56:23 [mysql] Status change detected: running
(Nothing happened after that. MySQL was running, but Apache was not. I clicked "Start" for Apache.)
16:57:12 [Apache] Attempting to start Apache app...
16:57:12 [Apache] Status change detected: running
16:57:13 [Apache] Status change detected: stopped
16:57:13 [Apache] Error: Apache shutdown unexpectedly.
16:57:13 [Apache] This may be due to a blocked port, missing dependencies,
16:57:13 [Apache] improper privileges, a crash, or a shutdown by another method.
16:57:13 [Apache] Press the Logs button to view error logs and check
16:57:13 [Apache] the Windows Event Viewer for more clues
16:57:13 [Apache] If you need more help, copy and post this
16:57:13 [Apache] entire log window on the forums

I undid the changes described above and Apache started up okay. Then I made the changes again and got the same results shown above.

I checked the folder "C:\<install root>\xampp\apache\logs" and no file has a timestamp later than today at 16:50, so it can't be reporting anything about the above activity. I also checked Event Viewer. There is one error listed within the last hour and it is from the DNS Client, but it is timestamped 16:44, so it must also have been from an earlier problem that I fixed by changing to the procedure described above.

So I don't see any error data telling me why Apache failed to start.

Can anyone tell me what is wrong with the procedure described and why Apache is failing on startup?

Thanks for your help.
Ennex
 
Posts: 6
Joined: 07. April 2015 01:38
Operating System: Windows 7

Re: Virtual host not working

Postby Altrea » 07. April 2015 05:30

Hi,

please execute the apache_start.bat script and tell us what this script responded.

Best wishes,
Altrea
We don't provide any support via personal channels like PM, email, Skype, TeamViewer!

It's like porn for programmers 8)
User avatar
Altrea
AF Moderator
 
Posts: 11935
Joined: 17. August 2009 13:05
XAMPP version: several
Operating System: Windows 11 Pro x64

Re: Virtual host not working

Postby Ennex » 07. April 2015 09:01

Altrea,

Thank you for your question, which led me to the solution, and also taught me something about troubleshooting XAMPP, which is that if something doesn't work in the Control Panel, then try to execute the operation with the equivalent batch file to possibly get a more helpful error message.

Here's what I got when I ran "apache_start.bat" (leaving out the German lines):

==================================================
Please close this command only for Shutdown
Apache 2 is starting ...
AH00526: Syntax error on line 243 of C:/[install root]/XAMPP/apache/conf/httpd.conf:
DocumentRoot must be a directory

Apache could not be started
Press any key to continue . . .
==================================================

This was interesting to see. The source of the procedure I had followed was the selected answer in
http://stackoverflow.com/questions/1408/make-xampp-apache-serve-file-outside-of-htdocs
That answer offered three methods, the first of which was to use a virtual host. The third method was to change the doc root in "httpd.conf". Well, what the above response from the batch file showed me was that in order for method 1 to work, I had to also use method 3. This was not the case for the problem being posed there because that person was *adding* content from outside of "htdocs" while leaving the original "htdocs" in use, whereas I wanted to shut down the original "htdocs" and move it to a new path.

Okay, so I changed the doc root in "httpd.conf" and then ran the batch file again. This time I got:

==================================================
Please close this command only for Shutdown
Apache 2 is starting ...
AH00548: NameVirtualHost has no effect and will be removed in the next release C:/[install root]/XAMPP/apache/conf/extra/httpd-vhosts.conf:25
AH00112: Warning: DocumentRoot [C:/[install root]/XAMPP/htdocs] does not exist
==================================================

So I learned two things here:

1. The method that I got from StackOverflow is out-dated, since the "NameVirtualHost" command it told me to use is no longer in use. So if someone can point me to more up-to-date instructions for this, that would be appreciated.

2. Apache was picking up the default doc root location from somewhere else. So I did a search of the whole XAMPP install root and found additional instances in:

C:\[install root]\XAMPP\apache\conf\extra\httpd-ssl.conf
C:\[install root]\XAMPP\apache\conf\extra\httpd-xampp.conf

I changed those instances to the new path and then started up XAMPP. Bingo! Apache started and localhost is functioning!

However, there's something else going on here. Because of all those changes I made to the doc root everywhere in the Apache config files, the virtual host I set up is superfluous. The change of doc root is already telling Apache to load content from the new path. Somebody correct me if I'm wrong, but it looks like the virtual host setting is just redirecting the server to get its content files from the same place it's already getting them from. I'm not going to bother fixing this now because I'll be needing to set up virtual hosts for multiple sites when I get Drupal running, so I'll reconfigure the virtual host settings when I do that.

Okay, this has been an interesting exercise that helped me get familiar with the guts of Apache. Thank you Altrea for pointing me in the direction that let me figure out the problem.
Ennex
 
Posts: 6
Joined: 07. April 2015 01:38
Operating System: Windows 7

Re: Virtual host not working

Postby JJ_Tagy » 07. April 2015 14:09

Virtualhost works fine. To me, it looks like you are trying to use spaces or something which can break the path. Here is my example where localhost still uses the normal htdocs and test1 and test2 are outside the original document root:

Code: Select all
# 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>
    ServerAdmin webmaster@localhost
    DocumentRoot "C:/xampp/htdocs"
    ServerName localhost
    ErrorLog "logs/localhost-error.log"
    CustomLog "logs/localhost-access.log" common
    <Directory "C:/xampp/htdocs">
        Require all granted
        AllowOverride All
        Options Indexes FollowSymLinks
    </Directory>
</VirtualHost>

<VirtualHost *:80>
    ServerAdmin webmaster@test1.com
    DocumentRoot "C:/xampp/vhosts/test1"
    ServerName test1.com
    ErrorLog "logs/test1.com-error.log"
    CustomLog "logs/test1.com-access.log" common
    <Directory "C:/xampp/vhosts/test1">
        Require all granted
        AllowOverride All
        Options Indexes FollowSymLinks
    </Directory>
</VirtualHost>

<VirtualHost *:80>
    ServerAdmin webmaster@test2.com
    DocumentRoot "C:/xampp/vhosts/test2"
    ServerName test2.com
    ErrorLog "logs/test2.com-error.log"
    CustomLog "logs/test2.com-access.log" common
    <Directory "C:/xampp/vhosts/test2">
        Require all granted
        AllowOverride All
        Options Indexes FollowSymLinks
    </Directory>
</VirtualHost>
Last edited by JJ_Tagy on 07. April 2015 20:36, edited 1 time in total.
JJ_Tagy
 
Posts: 788
Joined: 30. January 2012 13:44
XAMPP version: 5.5.15
Operating System: Windows 10 Pro x64

Re: Virtual host not working

Postby Altrea » 07. April 2015 16:39

Just one correction to JJ_Tagys good example:
ServerAlias is not needed if the value is the same as ServerName
We don't provide any support via personal channels like PM, email, Skype, TeamViewer!

It's like porn for programmers 8)
User avatar
Altrea
AF Moderator
 
Posts: 11935
Joined: 17. August 2009 13:05
XAMPP version: several
Operating System: Windows 11 Pro x64

Re: Virtual host not working

Postby JJ_Tagy » 07. April 2015 20:35

Thanks for that Altrea. I will update my example here and on my machine.
JJ_Tagy
 
Posts: 788
Joined: 30. January 2012 13:44
XAMPP version: 5.5.15
Operating System: Windows 10 Pro x64


Return to XAMPP for Windows

Who is online

Users browsing this forum: No registered users and 116 guests