为什么我的Apache虚拟主机配置无法正常工作?

huangapple go评论49阅读模式
英文:

Why is my Apache Virtual Hosts configuration not working correctly?

问题

背景

我一直在使用一个只有一个站点的Apache服务器,一直运行得很好。最近,我不得不在本地创建另外两个站点,我找到了这个指南,它显示了如何操作。我还参考了这个 Stack Overflow 的问题,它也对我有很大帮助。

我的问题是,当我访问我在 /etc/hosts 中创建并在 httpd-vhosts.conf 中配置的三个主机时,它无法工作,并且在浏览器中尝试访问时显示 its-site.test 拒绝连接

我尝试通过参考这个 Stack Overflow 的问题来解决这个问题,它帮助我更好地理解配置,但它仍然无法工作,导致相同的问题。

毫无疑问,我对Apache非常陌生,所以我可能忽略了一些明显的东西。

我的预期设置

我有三个不同的站点,我想在开发过程中使用命名虚拟主机在浏览器中访问它们。与使用一个本地主机和几个命名域不同,我希望它们每个都是命名域:

  • its-site.test
  • its-cab.test
  • its-gallery.test

主机文件

/etc/hosts

##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1       localhost
255.255.255.255 broadcasthost
::1             localhost

127.0.0.1       its-site.test
127.0.0.1       its-cab.test
127.0.0.1       its-gallery.test

httpd 配置

/opt/homebrew/etc/httpd/httpd.conf

Listen 80

Include /opt/homebrew/etc/httpd/extra/httpd-vhosts.conf
LoadModule rewrite_module lib/httpd/modules/mod_rewrite.so
ServerName localhost:80

# 也启用了PHP,但这里不包括配置,因为它可能与问题无关

vhosts 配置

/opt/homebrew/etc/httpd/extra/httpd-vhosts.conf

<VirtualHost *:80>
    ServerName its-site.test
    ServerAlias *.its-site.test
    DocumentRoot "/Users/ciesinsg/Documents/Repositories/its-site/web"

    <Directory "/Users/ciesinsg/Documents/Repositories/its-site/web">
        Require all granted
        AllowOverride All
    </Directory>
</VirtualHost>


<VirtualHost *:80>
    ServerName its-cab.test
    ServerAlias *.its-cab.test
    DocumentRoot "/Users/ciesinsg/Documents/Repositories/its-cab/web"

    <Directory "/Users/ciesinsg/Documents/Repositories/its-cab/web">
        Require all granted
        AllowOverride All
    </Directory>
</VirtualHost>


<VirtualHost *:80>
    ServerName its-gallery.test
    ServerAlias *.its-gallery.test
    DocumentRoot "/Users/ciesinsg/Documents/Repositories/its-gallery/web"

    <Directory "/Users/ciesinsg/Documents/Repositories/its-gallery/web">
        Require all granted
        AllowOverride All
    </Directory>
</VirtualHost>

问题

是否有人看到我在这个配置中犯了什么错误,并可以指引我解决方案?

附加信息

  • M1 MacBook Air
  • 使用Homebrew安装的Apache和PHP
  • 指向我的Drupal站点/ web文件夹

之前的单站点工作配置

如前所述,当我仅提供一个站点时,这个工作得很好。我的唯一配置是在 httpd.conf 中,没有配置 /etc/hostshttpd-vhosts.conf。我的工作 httpd.conf 文件如下所示:

Listen 80
DocumentRoot "/Users/ciesinsg/Documents/Repositories/its-gallery/web"
<Directory "/Users/ciesinsg/Documents/Repositories/its-gallery/web">

# 也启用了PHP

当我配置虚拟主机时,我删除了 DocumentRoot<Directory>

英文:

Background

I have been using an Apache server with only one site and this has been working really well. Recently, I have had to create two more sites locally, and I found this guide that showed how to do this. I also referred to this stack overflow question which also helped me a lot.

My problem is that when I visit the three hosts I made in /etc/hosts and configured in httpd-vhosts.conf, it is not working and says its-site.test refused to connect. when I try to visit it in browser.

I attempted to resolve the issue by referring to this stack overflow question which helped me to understand the configuration much better, but it is still not working and results in the same issue.

Needless to say I am very new to Apache so I might be overlooking something obvious.

My intended setup

I have three different sites which I want to use named virtual hosts to access in browser during my development process. Rather than using one local host and a few named domains, I wanted each of them to be named domains:

  • its-site.test
  • its-cab.test
  • its-gallery.test

Hosts file

/etc/hosts

##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1       localhost
255.255.255.255 broadcasthost
::1             localhost

127.0.0.1       its-site.test
127.0.0.1       its-cab.test
127.0.0.1       its-gallery.test

httpd configuration

/opt/homebrew/etc/httpd/httpd.conf

Listen 80

Include /opt/homebrew/etc/httpd/extra/httpd-vhosts.conf
LoadModule rewrite_module lib/httpd/modules/mod_rewrite.so
ServerName localhost:80

# Also enabled PHP but not including the config here as it may not be relevant

vhosts configuration

/opt/homebrew/etc/httpd/extra/httpd-vhosts.conf

&lt;VirtualHost *:80&gt;
    ServerName its-site.test
    ServerAlias *.its-site.test
    DocumentRoot &quot;/Users/ciesinsg/Documents/Repositories/its-site/web&quot;

    &lt;directory &quot;/Users/ciesinsg/Documents/Repositories/its-site/web&quot;&gt;
        Require all granted
        AllowOverride All
    &lt;/directory&gt;
&lt;/VirtualHost&gt;


&lt;VirtualHost *:80&gt;
    ServerName its-cab.test
    ServerAlias *.its-cab.test
    DocumentRoot &quot;/Users/ciesinsg/Documents/Repositories/its-cab/web&quot;

    &lt;directory &quot;/Users/ciesinsg/Documents/Repositories/its-cab/web&quot;&gt;
        Require all granted
        AllowOverride All
    &lt;/directory&gt;
&lt;/VirtualHost&gt;


&lt;VirtualHost *:80&gt;
    ServerName its-gallery.test
    ServerAlias *.its-gallery.test
    DocumentRoot &quot;/Users/ciesinsg/Documents/Repositories/its-gallery/web&quot;

    &lt;directory &quot;/Users/ciesinsg/Documents/Repositories/its-gallery/web&quot;&gt;
        Require all granted
        AllowOverride All
    &lt;/directory&gt;
&lt;/VirtualHost&gt;

Question

Does anybody see where I made a mistake with this configuration and can point me towards the solution?

Additional Information

  • M1 Macbook Air
  • Apache and PHP installed using homebrew
  • Points to my drupal site/web folders

Previous Single Site working Config

As stated earlier, I had this working well when I was serving only one site. My only configuration was with httpd.conf and did not configure /etc/hosts or httpd-vhosts.conf. My working httpd.conf file looked like:

Listen 80
DocumentRoot &quot;/Users/ciesinsg/Documents/Repositories/its-gallery/web&quot;
&lt;Directory &quot;/Users/ciesinsg/Documents/Repositories/its-gallery/web&quot;&gt;

# Also enabled PHP

I removed the DocumentRoot and &lt;Directory&gt; when I configured VirtualHosts.

答案1

得分: 1

我成功解决了以下问题:

测试 PHP

首先,我使用ping命令在etc/hosts中测试了我的配置:

ciesinsg@NB-00304-H14872 ~ % ping its-site.test
PING its-site.test (127.0.0.1): 56 数据字节
来自127.0.0.1的64字节 icmp_seq=0 ttl=64 时间=0.051 毫秒

这确认了站点路由到了与我的配置匹配的正确IP。

检查虚拟主机

这是通过参考这些文档并仔细检查我遵循的指南来手动完成的。我不知道有什么测试可以确认配置是否正确工作,但在阅读了一些关于虚拟主机的指南和文档之后,我相当有信心配置没有任何问题,问题一定出在其他地方。

检查httpd.conf

这个配置是我列出的默认配置中最长的一个。因此,这是一个手动过程。我的解决方案涉及打开httpd-default.conf,它是一个相邻的配置文件,似乎包含了httpd.conf文件的默认配置。在Homebrew安装的Apache中,它位于/opt/homebrew/etc/httpd/extra/httpd.conf文件也位于同一位置。我将这两个配置文件并排比较,然后从我之前的尝试中删除了我的配置。

在我的情况下,我发现我在httpd.conf文件和httpd-php.conf文件中意外地重复了我的PHP配置。我删除了所有我的配置,然后再次按照指南进行配置,确保我的IncludeLoadModule语句放置在相同的语句之后。

在删除我的配置并更仔细地配置后,我在浏览器中测试了站点,它们都按预期工作。

英文:

I was able to resolve the problem using the below solution:

Test PHP

First, I tested my configurations in etc/hosts using ping:

ciesinsg@NB-00304-H14872 ~ % ping its-site.test
PING its-site.test (127.0.0.1): 56 data bytes
64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.051 ms

This confirmed that the site was routing to the right IP matching my config.

Check Virtual Hosts

This was done manually by referring to these docs and double checking the guide I followed. I am not aware of any tests to confirm the configuration is working correctly, but after reading a few guides and docs about virtual hosts, I was pretty confident that the configuration did not have any issues and the problem must be elsewhere.

Check httpd.conf

This configurations is the longest out of the ones I listed by default. Consequently, this was a manual process. My solution involved opening httpd-default.conf which is an adjacent configuration file that appears to have the default configurations from the httpd.conf file. On the Homebrew installation of Apache, this is located in the /opt/homebrew/etc/httpd/extra/ where the httpd.conf file is also located. I compared these two configurations side by side and removed my configurations from my earlier attempts.

In my case, I found that I had accidentally duplicated my PHP configuration in the httpd.conf file and in httpd-php.conf file. I removed all of my configurations and followed through the guide once again, ensuring that my Include and LoadModule statements were placed after the same statements.

After removing my configurations and configuring it more carefully, I tested the sites in the browser and they were all working as expected.

huangapple
  • 本文由 发表于 2023年7月13日 00:01:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76672484.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定