【问题标题】:how to create virtual host on XAMPP [duplicate]如何在 XAMPP 上创建虚拟主机 [重复]
【发布时间】:2015-01-31 19:24:13
【问题描述】:

我确信这个问题被问了很多次,但我没有遇到任何问题。我在配置 Zend 框架时使用 XAMPP。

XAMPP 正在 port 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 更新主机文件并尝试重新启动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

【问题讨论】:

标签: php apache zend-framework xampp virtualhost


【解决方案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>
</VirtualHost>

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

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

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

【讨论】:

    【解决方案2】:

    我一直在寻找解决方案。最后我得到了答案。如果您的虚拟主机无法在您的 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 目录中的 hosts 文件中

    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"
    

    重启,开心就好:)

    【讨论】:

      【解决方案3】:

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

      为此,请删除 httpd-ssl.conf 下的整个 &lt;VirtualHost&gt; 条目

      从那里开始,您在 httpd-vhosts.conf 中所做的任何设置都将按预期更新 httphttps 引用。

      【讨论】:

        【解决方案4】:
        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
        

        【讨论】:

          【解决方案5】:

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

          【讨论】:

            【解决方案6】:

            我参加聚会有点晚了,但我为 Mac 编写了这个通过终端创建 VirtualHost 的小 bash 脚本:

            #!/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 的默认安装位置,该位置都可以更改。

            【讨论】:

              【解决方案7】:

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

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

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

              Location Directive 仅将包含的指令应用于匹配的 URL

              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

              【讨论】:

                【解决方案8】:

                简单, 您可以查看以下模板并相应地使用它。创建虚拟主机很常见,也很简单。当然下面的模板会起作用。

                <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

                谢谢,

                【讨论】:

                  【解决方案9】:

                  在 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

                  【讨论】:

                    【解决方案10】:

                    只需将端口更改为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 服务启动,这只会引发警告并且 vhost 将无法工作
                    • Apache 在这些更正后开始运行,但在显示 Not Found HTTP 错误 404 的服务器上仍然找不到 comm-app.local 请求的资源没有找到。
                    【解决方案11】:

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

                    【讨论】:

                    • 嘿,阿米特...我的意外无法正常工作...。我的设置的唯一区别是:127.0.0.4 mycustomdomain,它在端口 90 上运行,所以我使用了&lt;VirtualHost 127.0.0.4:90&gt;。当我在浏览器中尝试时失败http://mycustomdomain
                    【解决方案12】:

                    我已将以下配置添加到 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>
                    

                    【讨论】:

                      【解决方案13】:

                      我使用以下配置修复了它。

                       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>
                      

                      【讨论】:

                        【解决方案14】:

                        我看到两个错误:

                        <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 在这些更正后开始运行,但在显示 Not Found HTTP 错误 404 的服务器上仍然找不到 comm-app.local。未找到请求的资源。
                        • 您是否在您的 hosts 文件中设置了 comm-app.local 以重定向到 127.0.0.1?你确定你的路径是正确的吗?哦,当然,如果你不这样做,你必须关闭 VirtualHost 容器......(更新了我的帖子)
                        • 是的,我只是将 127.0.0.1 comm-app.local 放在 hosts 文件中,甚至重启服务器
                        • 你在浏览器中输入了 comm-app.local:8081 吗? (可能忘记了端口)? Rmember 在更改配置后重新启动您的 apache。但是,如果这仍然不起作用,您的配置文件中还有其他问题...
                        • 嗨!斯坦尼。我遇到了同样的问题。有什么办法可以避免在虚拟主机名之后输入端口号。提前致谢
                        【解决方案15】:
                        <VirtualHost *:80>
                            DocumentRoot "D:/projects/yourdirectry name"
                            ServerName local.yourdomain.com
                            <Directory "D:/projects/yourdirectry name">
                                Require all granted 
                            </Directory>
                        </VirtualHost>
                        

                        保存 Apache 配置文件。

                        详细信息请参考this

                        【讨论】:

                          【解决方案16】:

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

                          【讨论】:

                            猜你喜欢
                            • 2016-05-11
                            • 2019-01-15
                            • 2012-09-05
                            • 2020-06-10
                            • 2023-03-18
                            • 2016-06-10
                            • 2019-04-19
                            • 2020-07-29
                            • 2011-04-26
                            相关资源
                            最近更新 更多