Apache virtualHost 被忽略或指向默认的

Apache virtualHost ignored or pointing to the default one

我正在尝试在 OSX 上设置我的 Apache,但我无法使我的本地 url 'awr.local' 指向正确的路径。我的意思是无论我输入 http://localhost or http://awr.local,它总是在我的 'localhost' 虚拟主机路径中显示 index.html 页面。 无论是否使用 sudo,我都无数次重启了我的 httpd 服务。

任何帮助将不胜感激,谢谢:'|

我一直在学习教程 (https://getgrav.org/blog/macos-sierra-apache-multiple-php-versions),这里是关于 Apache 的步骤:

禁用捆绑的 Apache 并从自制程序安装一个:

sudo apachectl stop
sudo launchctl unload -w /System/Library/LaunchDaemons/org.apache.httpd.plist 2>/dev/null
brew install httpd

然后,一些httpd.conf修改:

# changed Listen 8080 to :
Listen 80
[...]
# changed DocumentRoot "/usr/local/var/www" to :
DocumentRoot "/Users/wallace/dev/default"
[...]
# changed <Directory "/usr/local/var/www"> to :
<Directory "/Users/wallace/dev/default">
[...]
# changed AllowOverride None to :
AllowOverride All
# uncommented :
LoadModule rewrite_module lib/httpd/modules/mod_rewrite.so
[...]
# changed
# User _www
# Group _www
# to :
User wallace
Group staff
[...]
# added the missing line :
ServerName localhost

到目前为止,一切似乎都运行良好,我已经安装了 PHP 和 MariaDB,没有任何问题。

然后是虚拟主机部分:

一些其他 httpd.conf 修改:

# uncommenting these lines
LoadModule vhost_alias_module lib/httpd/modules/mod_vhost_alias.so
[...]
Include /usr/local/etc/httpd/extra/httpd-vhosts.conf

正在编辑文件 /usr/local/etc/httpd/extra/httpd-vhosts.conf :

<VirtualHost *:80>
    DocumentRoot "/Users/wallace/dev/default"
    ServerName localhost
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot "/Users/wallace/dev/awr"
    ServerName awr.local
</VirtualHost>

/etc/hosts 文件:

127.0.0.1       localhost
255.255.255.255 broadcasthost
::1             localhost
127.0.0.1       awr.local
::1             awr.local

'httpd -S'的输出:

VirtualHost configuration:
*:80                   is a NameVirtualHost
         default server localhost (/usr/local/etc/httpd/extra/httpd-vhosts.conf:25)
         port 80 namevhost localhost (/usr/local/etc/httpd/extra/httpd-vhosts.conf:25)
         port 80 namevhost awr.local (/usr/local/etc/httpd/extra/httpd-vhosts.conf:30)
ServerRoot: "/usr/local/opt/httpd"
Main DocumentRoot: "/Users/wallace/dev/default"
Main ErrorLog: "/usr/local/var/log/httpd/error_log"
Mutex rewrite-map: using_defaults
Mutex default: dir="/usr/local/var/run/httpd/" mechanism=default
Mutex mpm-accept: using_defaults
PidFile: "/usr/local/var/run/httpd/httpd.pid"
Define: DUMP_VHOSTS
Define: DUMP_RUN_CFG
User: name="wallace" id=501 not_used
Group: name="staff" id=20 not_used

这就是我的设置方式(我已进行更改以匹配您的配置)。
此外 awr.local 必须在您的 /etc/hosts 文件中指向 127.0.0.1 或您要使用的任何其他网络接口。

<VirtualHost awr.local:80>
    DocumentRoot "/Users/wallace/dev/awr"
    ServerName awr.local
    <Directory "/Users/wallace/dev/awr">
            AllowOverride All
            Order allow,deny
            allow from all
            Require all granted
    </Directory>    
</VirtualHost>

好吧,今天当我准备尝试 Juan 的回答时,我有了一些新东西:awr.local url 没有为我提供本地主机,而是向我显示了 403 禁止。经过新的研究和文档阅读后,我设法让它像这样工作:

我的 /usr/local/etc/httpd/extra/httpd-vhosts.conf 文件:

要求全部授予是最重要的部分。

<VirtualHost *:80>
    DocumentRoot "/Users/wallace/dev/default"
    ServerName localhost
    <Directory "/Users/wallace/dev/default">
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot "/Users/wallace/dev/awr"
    ServerName awr.local
    <Directory "/Users/wallace/dev/awr">
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

我必须用 sudo 重启 Apache 才能工作:

sudo brew services stop httpd && sudo brew services start httpd

我遇到了同样的问题,无论我怎样尝试都无法正常工作。停止、启动或重新启动 httpd 无效。尽管 httpd -S 总是报告正确的设置,但所有浏览器都加载了默认的 docroot,而不管虚拟主机设置如何。然后我注意到了一些奇怪的事情。使用 brew services stop httpd 停止 httpd 后,我在浏览器中重新加载了 localhost,它仍然加载没有问题。这给了我 sudo killall httpd 然后用 brew services start httpd 重新开始的想法。之后,一切都很好。

考虑到我在这上面花了多少时间,我也将放弃自己的修复。 首先我注意到所有使用 127.0.0.1 IP 的服务器名称总是指向默认目录。我编辑了自定义域以在 /etc/hosts 中使用 127.0.1.1。 在 /usr/local/etc/httpd/extra/httpd-vhosts.conf 中,根据 Alfred 的回答对其进行了编辑,因为我最初也得到了 403。

文件看起来像这样;

/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
127.0.1.1       customdomain.local # note: 127.0.1.1
127.0.1.2       customdomain2.local # note: 127.0.1.2 (will increment for each new domain)
255.255.255.255 broadcasthost
::1             localhost
::1             customdomain.local
::1             customdomain2.local # this part is equally important though I am not sure why

/usr/local/etc/httpd/extra/httpd-vhosts.conf:

<VirtualHost customdomain.local:80>
    ServerAdmin webmaster@customdomain.local
    DocumentRoot "/Users/user/Projects/customdomain/public"
    ServerName customdomain.local
    <Directory "/Users/user/Projects/customdomain/public">
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

确认您在 /usr/local/etc/httpd/httpd.conf 中的 Listen 值为 80。

这可能不是最佳解决方案,但应该可以解决问题。