Apache virtualHost 被忽略或指向默认虚拟主机

Alf*_*ace 7 apache macos configuration virtualhost

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

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

我一直在关注教程(https://getgrav.org/blog/macos-sierra-apache-multiple-php-versions),以下是有关 Apache 的步骤:

禁用捆绑的 Apache 并从 homebrew 安装 Apache:

sudo apachectl stop
sudo launchctl unload -w /System/Library/LaunchDaemons/org.apache.httpd.plist 2>/dev/null
brew install httpd
Run Code Online (Sandbox Code Playgroud)

然后,对 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
Run Code Online (Sandbox Code Playgroud)

到目前为止,一切似乎都工作正常,我已经安装了 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
Run Code Online (Sandbox Code Playgroud)

编辑文件 /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>
Run Code Online (Sandbox Code Playgroud)

/etc/hosts 文件:

127.0.0.1       localhost
255.255.255.255 broadcasthost
::1             localhost
127.0.0.1       awr.local
::1             awr.local
Run Code Online (Sandbox Code Playgroud)

'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
Run Code Online (Sandbox Code Playgroud)

Alf*_*ace 3

好吧,今天当我准备尝试 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>
Run Code Online (Sandbox Code Playgroud)

我必须使用sudo重新启动 Apache才能正常工作:

sudobrew 服务停止 httpd && sudobrew 服务启动 httpd