如何在XAMPP上创建虚拟主机

99
我确定这个问题已经被问了很多次,但是我没有遇到过这个问题。我正在使用XAMPP,在那里我配置了Zend框架。
XAMPP运行在端口8081上,因为80被某个Windows进程占用了,所以我需要使用虚拟主机来配置。我在C:/xampp/apache/config/extra/httpd-vhosts.config(或者在更新的版本中是C:/xampp/apache/conf/extra/httpd-vhosts.conf)中进行了以下代码的配置。
<VirtualHost *:80>
    ServerName comm-app.local
    DocumentRoot "C:/xampp/htdocs/CommunicationApp/public"
    SetEnv APPLICATION_ENV "development"
    <Directory "C:/xampp/htdocs/CommunicationApp/public">
        DirectoryIndex index.php
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>    

并且还要使用127.0.0.1 comm-app.local更新hosts文件,并尝试重新启动Apache,但是出现了错误。
15:03:01  [Apache]  Error: Apache shutdown unexpectedly.
15:03:01  [Apache]  This may be due to a blocked port, missing dependencies, 
15:03:01  [Apache]  improper privileges, a crash, or a shutdown by another method.
15:03:01  [Apache]  Press the Logs button to view error logs and check
15:03:01  [Apache]  the Windows Event Viewer for more clues
15:03:01  [Apache]  If you need more help, copy and post this
15:03:01  [Apache]  entire log window on the forums

2
如果 Apache 运行在端口 8081 上,那么 VirtualHost *:80 不应该改为 VirtualHost *:8081 吗? - TommyBs
4
占用80端口的Windows进程可能是Skype。 - Marek
请参阅更完整的答案:如何在XAMPP(Windows)上设置Apache虚拟主机 - Top-Master
16个回答

93

步骤1)打开路径C:\WINDOWS\system32\drivers\etc\中的"hosts"文件:

127.0.0.1       localhost
127.0.0.1       test.com
127.0.0.1       example.com

第二步) xampp\apache\conf\extra\httpd-vhosts.conf

<VirtualHost *:80>
    DocumentRoot C:/xampp/htdocs/test/
    ServerName www.test.com
</VirtualHost>
<VirtualHost *:80>
    DocumentRoot C:/xampp/htdocs/example/
    ServerName www.example.com
</VirtualHost>

第三步) 打开 C:\xampp\apache\conf\httpd.conf 文件,在文件结尾找到“补充配置”(Supplemental configuration)这一部分,然后定位下面这一段文字(大约在第 500 行左右),将第二行开头的 # 号删除,使得这段文字现在看起来像这样:

#Virtual hosts
Include conf/extra/httpd-vhosts.conf

第四步) 重新启动XAMPP,并在您的浏览器中运行:

www.example.com or www.test.com

2
嘿,阿米特...我的出乎意料地不工作了...我设置中唯一的区别是:127.0.0.4 mycustomdomain,并且它运行在90端口上,所以我使用了<VirtualHost 127.0.0.4:90>。当我在浏览器中尝试访问http://mycustomdomain时,它失败了。 - Jason Krs

76

我看到两个错误:

<VirtualHost *:80> -> Fix to :8081, your POrt the server runs on
    ServerName comm-app.local
    DocumentRoot "C:/xampp/htdocs/CommunicationApp/public"
    SetEnv APPLICATION_ENV "development"
    <Directory "C:/xampp/htdocs/CommunicationApp/public" -> This is probably why it crashes, missing >
        DirectoryIndex index.php
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
 -> MIssing close container: </VirtualHost> 

修复版本:

<VirtualHost *:8081>
    ServerName comm-app.local
    DocumentRoot "C:/xampp/htdocs/CommunicationApp/public"
    SetEnv APPLICATION_ENV "development"
    <Directory "C:/xampp/htdocs/CommunicationApp/public">
        DirectoryIndex index.php
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

需要提及一件事情:

你总是可以尝试并运行命令:

service apache2 configtest

这将告诉您是否存在配置错误,甚至可能会告诉您问题所在。

此外,它有助于避免LIVE系统中的不可用性:

service apache2 restart

会关闭然后无法启动,这个configtest可以让你提前知道 "哎呀,我做错了什么,我应该先修复这个问题" 但是Apache本身仍在使用旧的配置文件运行。 :)


这些更正后,Apache开始运行,但服务器上仍找不到comm-app.local,显示“未找到HTTP错误404请求的资源未找到”。 - Paras Arora
1
是的,我只是在hosts文件中添加了“127.0.0.1 comm-app.local”,并重新启动了服务器。 - Paras Arora
嗨!Steini。我遇到了同样的问题。有没有办法在虚拟主机名后避免输入端口号?提前感谢。 - karim_fci
我收到了404错误。 - Moeez
@JasonKrs 你找到解决方案了吗? - Moeez
显示剩余4条评论

12

将此代码添加到 C:\xampp\apache\conf\extra\httpd-vhosts.conf 文件中

<VirtualHost *:80>
DocumentRoot "C:/xampp/htdocs"
ServerName qa-staging.com
ServerAlias www.qa-staging.com
<Directory "c:/xampp/htdocs">
Order allow,deny
Allow from all
</Directory>
</VirtualHost>

现在在下面的文件中添加您的虚拟主机名称。

C:\Windows\System32\drivers\etc\hosts

127.0.0.1 qa-staging.com

如果您无法将此代码保存到主机文件中,请右键单击记事本,选择“以管理员身份运行”,然后您现在就能够保存自定义代码了,现在重启XAMP即可。


获取404错误。 - Moeez

7
将这些代码写入C:\xampp\apache\conf\extra\httpd-vhosts.conf文件的结尾位置。
DocumentRoot "D:/xampp/htdocs/foldername"
ServerName www.siteurl.com
ServerAlias www.siteurl.com
ErrorLog "logs/dummy-host.example.com-error.log"
CustomLog "logs/dummy-host.example.com-access.log" common

在虚拟主机标签之间进行编辑。

以管理员身份使用记事本编辑System32/Drivers/etc/hosts文件。

在文件底部添加内容。

127.0.0.1    www.siteurl.com

5
在您的磁盘驱动器中:\xampp\apache\conf\extra\httpd-vhosts.conf存在一个示例,您可以使用您的配置进行编辑。
 ##<VirtualHost *:80>
 ##ServerAdmin webmaster@dummy-host.example.com
 ##DocumentRoot "C:/xampp/htdocs/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上运行phpmyadmin和其他项目。作为示例,我将展示store.local项目的配置:
<VirtualHost *:80>
    DocumentRoot "C:/xampp/htdocs"
    ServerName localhost
</VirtualHost>

<VirtualHost *:80>
    ServerAdmin webmaster@store.local
    DocumentRoot "c:/xampp/htdocs/store.local/public"
    ServerName www.store.local
    ServerAlias store.local
    <Directory C:/xampp/htdocs/store.local>
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

然后如上所述,您必须在文件底部添加:C:\windows\system32\drivers\hosts
127.0.0.1    store.local
127.0.0.1    www.store.local

重新启动Apache并在浏览器中尝试:
store.local or www.store.local

也许在第一次,你必须像这样添加:
http://store.local or http://www.store.local

要使用其他端口,您必须在VirtualHost之前添加以下内容:
Listen 8081 or another which you prefer

然后只需像这样使用端口作为您的VirtualHost:
<VirtualHost *:8081>
ServerAdmin webmaster@store.local
DocumentRoot "c:/xampp/htdocs/store.local/public"
ServerName store.local
ServerAlias www.store.local
<Directory C:/xampp/htdocs/store.local>
    AllowOverride All
    Require all granted
</Directory>
</VirtualHost>

然后重新启动Apache并在浏览器中尝试。
store.local:8081 or www.store.local:8081

只有您为其添加端口的项目才会在该端口上运行,例如其他项目和phpmyadmin仍将在80端口上运行。

尝试使用您的解决方案,但出现404错误。 - Moeez

5
<VirtualHost *:80>
    DocumentRoot "D:/projects/yourdirectry name"
    ServerName local.yourdomain.com
    <Directory "D:/projects/yourdirectry name">
        Require all granted 
    </Directory>
</VirtualHost>

保存Apache配置文件。

详细信息请参考此网页


在浏览器上出现404错误。 - Moeez

4

只需将端口更改为8081,以下虚拟主机即可正常工作:

<VirtualHost *:8081>
ServerName comm-app.local
DocumentRoot "C:/xampp/htdocs/CommunicationApp/public"
SetEnv APPLICATION_ENV "development"
    <Directory "C:/xampp/htdocs/CommunicationApp/public">
        DirectoryIndex index.php
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory> 
</VirtualHost>

这是一个错误,但这不会阻止Apache服务启动,这只会抛出一个警告,虚拟主机将无法工作。 - Steini
这些更正后,Apache开始运行,但服务器上仍找不到comm-app.local,显示未找到HTTP错误404。请求的资源未找到。 - Paras Arora
@ParasArora,你找到解决方案了吗? - Moeez

2

Apache虚拟主机文档的设置提供了几个好处:

  • 虚拟主机使URL更加清晰 - localhost/mysite vs mysite.local。
  • 虚拟主机使权限更容易 - 限制本地网络上单个vhost的访问权限,而不是允许访问本地网络上的所有站点。
  • 一些应用程序需要在URL中使用“.”(比如Magento)。虽然您可以通过编辑Windows hosts文件来设置localhost.com/mysite,但创建虚拟主机是更好的解决方案。

VirtualHost指令包含仅适用于特定主机名或IP地址的指令

Location指令仅将封闭指令应用于匹配的URL

示例更改配置文件- D:\xampp\apache\conf\extra\httpd-vhosts.conf

<VirtualHost *:80>
    ServerAdmin localhost
    DocumentRoot "D:/xampp/htdocs"
    ServerName localhost
</VirtualHost>

<VirtualHost localhost:80>
  ServerAdmin webmaster@host.example.com
  DocumentRoot "/www/docs/host.example.com"
  #DocumentRoot "D:\xampp\htdocs\phpPages"
  ServerName host.example.com
  ErrorLog "logs/host.example.com-error_log"
  TransferLog "logs/host.example.com-access_log"
</VirtualHost>

# To get view of PHP application in the Browser.
<VirtualHost *:80>
    ServerAdmin postmaster@dummy-host.localhost
    DocumentRoot "D:\xampp\htdocs\app1"
    ServerName app1.yash.com
    ServerAlias app1.yash.com
    ErrorLog "logs/app1.yash.com-error.log"
    CustomLog "logs/app1.yash.com-access.log" combined
    
    # App1 communication proxy call to Java War applications from XAMP
    <Location /ServletApp1>
            ProxyPass  http://app1.yashJava.com:8080/ServletApp1
            ProxyPassReverse  http://app1.yashJava.com:8080/ServletApp1
            Order Allow,Deny
            Allow from all
    </Location>
</VirtualHost>

<VirtualHost *:80>
    ServerAdmin postmaster@infotreesolutions.com
    DocumentRoot "D:\xampp\htdocs\app2"
    ServerName app2.yash.com
    ErrorLog "logs/app2.yash.com-error.log"
    CustomLog "logs/app2.yash.com-access.log" combined
    
    # App1 communication proxy call to Java War applications from XAMP
    <Location /ServletApp2>
            ProxyPass  http://app1.yashJava.com:8080/ServletApp2
            ProxyPassReverse  http://app1.yashJava.com:8080/ServletApp2
            Order Allow,Deny
            Allow from all
    </Location>
</VirtualHost>

更新您的Windows Hosts文件 « 打开位于C:\Windows\System32\drivers\etc\hosts的Windows hosts文件。

# localhost name resolution is handled within DNS itself.
#   127.0.0.1       localhost
#   ::1             localhost

127.0.0.1       test.com
127.0.0.1       example.com
127.0.0.1       myssl.yash.com

D:\xampp\apache\conf\httpd.conf,[httpd-ssl.conf](http://httpd.apache.org/docs/2.2/mod/mod_ssl.html)

# Listen: Allows you to bind Apache to specific IP addresses and/or
# ports, instead of the default. See also the <VirtualHost> directive.
# Listen 0.0.0.0:80 | [::]:80
Listen 80

LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule speling_module modules/mod_speling.so

# ServerAdmin: Your address, where problems with the server should be e-mailed.
# This address appears on some server-generated pages, such as error documents.
#  e.g. admin@your-domain.com
ServerAdmin postmaster@localhost
ServerName localhost:80
DocumentRoot "D:/xampp/htdocs"

<Directory "D:/xampp/htdocs">
    Options Indexes FollowSymLinks Includes ExecCGI
    AllowOverride All
    Require all granted
</Directory>

# Virtual hosts
Include "conf/extra/httpd-vhosts.conf"

# ===== httpd-ssl.conf - SSL Virtual Host Context =====
# Note: Configurations that use IPv6 but not IPv4-mapped addresses need two
#       Listen directives: "Listen [::]:443" and "Listen 0.0.0.0:443"
Listen 443
## SSL Virtual Host Context
<VirtualHost _default_:443>
    DocumentRoot "D:\xampp\htdocs\projectFolderSSL"
    ServerName myssl.yash.com:443
    ServerAlias myssl.yash.com:443
        
    ServerAdmin webmaster@localhost
    ErrorLog "logs/error.log"
    <IfModule log_config_module>
        CustomLog "logs/access.log" combined
    </IfModule>
    
    ## Redirecting URL from Web server to Application server over different machine.
    # myssl.yash.com:443/ServletWebApp
    <Location /path>
        ProxyPass  http://java.yash2.com:8444/ServletWebApp
        ProxyPassReverse  http://java.yash2.com:8444/ServletWebApp
        Order Allow,Deny
        Allow from all
    </Location>
    
    #SSLCertificateFile "conf/ssl.crt/server.crt"
    SSLCertificateFile "D:\SSL_Vendor\yash.crt"
    
    #SSLCertificateKeyFile "conf/ssl.key/server.key"
    SSLCertificateKeyFile "D:\SSL_Vendor\private-key.key"
    
    #SSLCertificateChainFile "conf/ssl.crt/server-ca.crt"
    SSLCertificateChainFile "D:\SSL_Vendor\intermediate.crt"
</VirtualHost>
# ===== httpd-ssl.conf - SSL Virtual Host Context =====

@see


1
我使用以下配置修复了它。
 Listen 85
 <VirtualHost *:85>
           DocumentRoot "C:/xampp/htdocs/LaraBlog/public"
           <Directory "C:/xampp/htdocs/CommunicationApp/public">
           DirectoryIndex index.php
           AllowOverride All
           Order allow,deny
           Allow from all
           </Directory>
 </VirtualHost>

0

虽然我来晚了,但我写了一个小的bash脚本,可以通过终端创建Mac上的VirtualHost:

#!/bin/bash

echo "Welcome to the VirtualHostCreator! Press <RETURN> to continue."

read

echo "Enter the name the VirtualHost you would like to create. No spaces or dashes, please."

read hostname

echo "Enter the document root of the VirtualHost."

read doc_root

echo "Creating VirtualHost \"$hostname\". You may be prompted for your password."

hosts_file="/etc/hosts"
vhosts_file="/Applications/XAMPP/xamppfiles/etc/extra/httpd-vhosts.conf"
restart_command="sudo /Applications/XAMPP/xamppfiles/xampp restart"

cat >> $vhosts_file << EndOfMessage
<VirtualHost ${hostname}>
    ServerName ${hostname}
    DocumentRoot "/Applications/XAMPP/xamppfiles/htdocs/${doc_root}"
</VirtualHost>
EndOfMessage

sudo sh -c "echo \"127.0.0.1       $hostname\" >> $hosts_file"

$restart_command

我相信还有一些可以改进的地方,它只有两个必需的虚拟主机选项(服务器名称和文档根目录),但是它比手动打开和编辑所有文件更快更高效地完成工作,并且还会自动重新启动XAMPP。

这假定您具有XAMPP的默认安装位置,这些都可以更改。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接