如何在XAMPP上创建虚拟主机


82

我确信这个问题已经被问过很多次了,但是我没有遇到任何问题。我在配置Zend框架的地方使用XAMPP。

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>    

并使用来更新hosts文件,127.0.0.1 comm-app.local并尝试重新启动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正在运行,则VirtualHost *:80不应该是VirtualHost *:8081
TommyBs 2014年

4
Windows进程占用端口80可能是Skype。
2014年

Answers:


70

我看到两个错误:

<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

将关闭,然后无法启动,您事先知道的此配置测试“糟糕,我做错了什么,我应该先解决此问题”,但是apache本身仍在使用旧配置运行。:)


经过这些更正后,Apache开始运行,但是在显示未找到HTTP错误404的服务器上仍未找到comm-app.local 。找不到请求的资源。
Paras Arora 2014年

您是否已设置comm-app.local以重定向到主机文件中的127.0.0.1?并且确定您的路径正确吗?哦,当然,如果您没有这样做,就必须关闭VirtualHost容器...(更新我的帖子)
Steini 2014年

1
是的,我只是将127.0.0.1 comm-app.local放置在hosts文件中,甚至重新启动服务器
Paras Arora

您在浏览器中输入comm-app.local:8081吗?(也许忘记端口)?Rmember更改配置后重新启动apache。但是,如果仍然无法正常工作,则说明您的配置文件中出现了其他错误...
Steini 2014年

嗨!Steini。我遇到了同样的问题。有什么方法可以避免在虚拟主机名之后键入端口号。在此先感谢
karim_fci 2015年

61

步骤1) C:\ WINDOWS \ system32 \ drivers \ etc \打开“主机”文件:

127.0.0.1       localhost
127.0.0.1       test.com
127.0.0.1       example.com

步骤2) 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>

步骤3) C:\ xampp \ apache \ conf \ httpd.conf。向下滚动到末尾的“补充配置”部分,然后找到以下部分(在第500行附近),从第二行的开头删除#,因此该部分现在如下所示:

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

步骤4)重新启动XAMPP,现在在您的浏览器中运行:

www.example.com or www.test.com

1
嗨,阿米特...我的机器令人惊讶地无法正常工作。...我的设置唯一的区别是:127.0.0.4 mycustomdomain它运行在端口90上,所以我使用了<VirtualHost 127.0.0.4:90>。当我在浏览器中尝试时它失败http://mycustomdomain
Jason Krs 18'Mar

8

将此代码添加到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

如果您无法将此代码保存在主机文件中,请右键单击notpad,然后选择以管理员身份运行,然后就可以保存您的自定义代码,然后重新启动XAMP


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

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

保存Apache配置文件。

有关详细信息,请参阅


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 2014年

经过这些更正后,Apache开始运行,但是在服务器上仍未显示comm-app.local,显示未找到HTTP错误404。找不到请求的资源。
Paras Arora 2014年

3

在您的磁盘驱动器中:\ 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>

就像这样,例如,不要忘记为localhost本身添加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>

然后重新启动Apache并在浏览器中尝试

store.local:8081 or www.store.local:8081

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


2

Apache虚拟主机文档设置虚拟主机(vhost)有以下好处:

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

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

位置指令仅将随附的指令应用于匹配的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主机文件«打开位于以下位置的Windows主机文件 C:\Windows\System32\drivers\etc\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 =====

@看到


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

我参加聚会有点晚了,但是我为Mac编写了这个bash脚本,它通过终端创建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

我确定可以进行一些改进,并且它仅对vhost提供两个必需的选项(服务器名称和文档根目录),但是与打开和编辑所有文件相比,它可以更快,更有效地完成工作。手动,然后自动重新启动XAMPP。

假定您具有XAMPP的默认安装位置,可以将其全部更改。


0

步骤1)在“ C:\ Windows \ System32 \ drivers \ etc”下打开主机文件

127.0.0.1  vipsnum.mk

步骤2)在“ C:\ xampp \ apache \ conf \ extra”下打开httpd-vhosts.conf文件

<VirtualHost vipsnum.mk:80>
    ServerName vipsnum.mk
    DocumentRoot "C:/xampp/htdocs/vipnum/"
    SetEnv APPLICATION_ENV "development"
    <Directory "C:/xampp/htdocs/vipnum/">
        DirectoryIndex index.php
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost> 

0
1. C:\xampp\apache\conf\https.conf
Virtual hosts
Include conf/extra/httpd-vhosts.conf


2. C:\Windows\System32\drivers\etc\hosts
127.0.0.1       localhost
127.0.0.1       helpdesk.local


3. C:\xampp\apache\conf\extra\httpd-vhosts.conf
<VirtualHost *:80>
    DocumentRoot "C:/xampp/htdocs/helpdesk/public"
    ServerName helpdesk.local
</VirtualHost>


Now, Restart Apache and go through the link. 
URL : http://helpdesk.local

0

在我的情况下,xampp的问题是在指定使用htdocs以外的其他文件夹时,尤其是在多个域和专用文件夹的情况下。这是因为httpd-ssl.conf也在引用<VirtualHost>

为此,请<VirtualHost>在下面删除整个条目httpd-ssl.conf

从那里,您所做的任何设置httpd-vhosts.conf都将按预期httphttps参考进行更新。


0

我一直在寻找解决方案。最后我有答案。如果您的虚拟主机无法在Windows 10或7等操作系统上运行,则是由于这种新的https炒作,所有站点都路由到https。

XAMPP遵循相同的趋势,因此由于严格的https重定向,仅修复虚拟主机,主机文件等是不够的。这是完整的解决方案。

我想要的站点位于D:\ xampp \ htdocs \ ikeen。

1)将此添加到您的httpd-vhosts.conf中:

<VirtualHost *:80>
     ServerName localhost
     DocumentRoot "D:\xampp\htdocs"
     <Directory "D:\xampp\htdocs">
         DirectoryIndex index.php
     </Directory>
 </VirtualHost>

<VirtualHost *:80>
     ServerName ikeen.localhost
     DocumentRoot "D:\xampp\htdocs\ikeen"
     SetEnv APPLICATION_ENV "development"
     <Directory "D:\xampp\htdocs\ikeen">
         DirectoryIndex index.php
         AllowOverride All
         Order allow,deny
         Allow from all        
     </Directory>
 </VirtualHost>

2)将其添加到httpd.conf的末尾:

<Directory />
    AllowOverride none
    Require all granted
</Directory>

3)将此行添加到Windows目录中的主机文件中

127.0.0.1      ikeen.localhost

4)最后一步是将httpd-ssl.conf文件的VirtualHost部分更改为以下内容

<VirtualHost _default_:443>

#   General setup for the virtual host
DocumentRoot "D:/xampp/htdocs"
#ServerName www.example.com:443
ServerName localhost
ServerAdmin admin@example.com
ErrorLog "D:/xampp/apache/logs/error.log"
TransferLog "D:/xampp/apache/logs/access.log"

#   General setup for the ikeen host
DocumentRoot "D:/xampp/htdocs/ikeen"
#ServerName www.example.com:443
ServerName ikeen.localhost
ServerAdmin admin@example.com
ErrorLog "D:/xampp/apache/logs/error.log"
TransferLog "D:/xampp/apache/logs/access.log"

重新启动并感到高兴:)


-1

我将以下配置添加到httpd.conf中,并重新启动了lampp服务,它开始工作。感谢上述所有帖子,这些帖子帮助我一步一步解决了问题。

Listen 8080
<VirtualHost *:8080>
    ServerAdmin webmaster@dummy-host2.example.com
    DocumentRoot "/opt/lampp/docs/dummy-host2.example.com"
    ServerName localhost:8080
    ErrorLog "logs/dummy-host2.example.com-error_log"
    CustomLog "logs/dummy-host2.example.com-access_log" common
    <Directory "/opt/lampp/docs/dummy-host2.example.com">
        Require all granted 
    </Directory>
</VirtualHost>

-1

很简单, 您可以看到以下模板并相应地使用它。创建虚拟主机非常普遍而且非常简单。当然,下面的模板将起作用。

<VirtualHost *:8081>
DocumentRoot "C:/xampp/htdocs/testsite"
ServerName testsite.loc
ServerAlias www.testsite.loc
<Directory "c:/xampp/htdocs/testsite">
Order allow,deny
Allow from all
</Directory>
</VirtualHost>

另外,有关虚拟主机的更多参考,请访问此站点。 http://www.thegeekstuff.com/2011/07/apache-virtual-host

谢谢,

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.