如何在 XAMPP 上创建虚拟主机

2022-08-30 07:51:23

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

XAMPP在端口8081上运行,因为80正在被某些Windows进程占用,我需要使用虚拟主机,我使用以下代码(或较新版本)配置。C:/xampp/apache/config/extra/httpd-vhosts.configC:/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>    

并且还更新主机文件并尝试重新启动apache,但它显示错误。127.0.0.1 comm-app.local

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

答案 1

步骤 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.向下滚动到末尾的“补充配置”部分,然后找到以下部分(第 500 行附近),从第二行的开头删除 # ,使该部分现在如下所示:

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

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

www.example.com or www.test.com

答案 2

我看到两个错误:

<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本身仍然使用旧配置运行。:)


推荐