【问题标题】:Apache virtualHost ignored or pointing to the default oneApache virtualHost 忽略或指向默认的
【发布时间】:2018-04-13 06:42:16
【问题描述】:

我正在尝试在 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 安装一个:

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

【问题讨论】:

    标签: macos apache configuration virtualhost


    【解决方案1】:

    这就是我的设置方式(我已根据您的配置进行了更改)。
    此外 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>
    

    【讨论】:

      【解决方案2】:

      好吧,今天当我准备尝试 Juan 的答案时,我有了一些新的东西:awr.local 网址没有为我提供本地主机,而是向我显示了 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

      【讨论】:

        【解决方案3】:

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

        【讨论】:

          猜你喜欢
          • 2016-08-08
          • 2020-02-06
          • 1970-01-01
          • 1970-01-01
          • 2016-10-02
          • 2021-01-28
          • 2020-07-05
          • 2016-06-19
          • 1970-01-01
          相关资源
          最近更新 更多