如何在XAMPP上创建虚拟主机

时间:2014-12-03 09:40:11

标签: php apache zend-framework xampp virtualhost

我确信这个问题被多次询问,但我没有遇到问题。我正在使用XAMPP配置Zend框架。

XAMPP在端口8081 上运行,因为 80 正被我需要使用虚拟主机的某些Windows进程占用,我使用{{1}中的以下代码进行配置}(或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文件并尝试重新启动apache但显示错误。

127.0.0.1 comm-app.local

17 个答案:

答案 0 :(得分:61)

我看到两个错误:

<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

将关闭然后无法启动,这个配置测试你事先知道&#34; oops我做错了什么,我应该先解决这个问题&#34;但是apache本身仍然以旧配置运行。 :)

答案 1 :(得分:49)

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

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。向下滚动到最后的Supplemental配置部分,找到以下部分(第500行左右),从第二行的开头删除#,这样该部分现在看起来像这样:

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

步骤4)重启XAMPP 现在在浏览器中运行:

www.example.com or www.test.com

答案 2 :(得分:5)

将这些代码写在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

答案 3 :(得分:4)

在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

答案 4 :(得分: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>

答案 5 :(得分:2)

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

保存Apache配置文件。

有关详细信息,请参阅this

答案 6 :(得分:2)

Apache虚拟主机documentation设置虚拟主机(vhost)有以下几个好处:

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

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

     

Location Directive仅将附带的指令应用于匹配的网址

Example更改了配置文件 - 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主机文件«打开位于C:\Windows\System32\drivers\etc\hosts.

中的Windows主机文件
# 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

答案 7 :(得分:1)

在您的磁盘驱动器中:\ 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显示。本地项目

<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上运行

答案 8 :(得分: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>

答案 9 :(得分: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的默认安装位置,可以全部更改。

答案 10 :(得分:0)

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

添加

NA_integer

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

添加

127.0.0.1  vipsnum.mk

答案 11 :(得分:0)

如果您需要轻松安装

https://github.com/scyzho/xampp

下载到您的htdocs文件夹

并输入您的主机

答案 12 :(得分: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

答案 13 :(得分:0)

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

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

从那里,您在httpd-vhosts.confhttp引用中所做的任何设置都会按预期更新。

答案 14 :(得分:0)

我一直在寻找解决方案。最后我有答案。如果您的虚拟主机无法在Windows 10或Windows 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"

重启并开心:)

答案 15 :(得分:-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>

答案 16 :(得分:-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

谢谢,