如何在 httpd.conf Amazon linux EC2 中使用 <VirtualHost>

How do I use <VirtualHost> in httpd.conf Amazon linux EC2

我已经尝试了所有可能的解决方案,将 mydomain.com 指向 /var/www/html/mysite 下的特定文件夹。首先 httpd.conf 文件在 /etc/httpd 下丢失,但在 /etc/httpd/conf 下找到。也没有 /etc/apache2 文件夹。

为了将我的域指向 /var/www/html/mysite,我在 /etc/httpd/conf/httpd.conf 下编写了以下代码,就在 ServerAdmin root@localhost

行下方
<VirtualHost *:80>
    ServerAdmin webmaster@mysite.com
    ServerName mydomain.com
    ServerAlias mydomain.com
    DocumentRoot /var/www/html/mysite/
</VirtualHost>

它确实有效,但一切,甚至 IP 都指向 /var/www/html/mysite/。 我无法弄清楚我错在哪里。我只需要 mydomain.com 指向 mysite 目录。

Docs:

If no matching ServerName or ServerAlias is found in the set of virtual hosts containing the most specific matching IP address and port combination, then the first listed virtual host that matches that will be used.

您的 *:80 虚拟主机接受端口 80 上的所有 IP,因此它适合 "first listed virtual host that matches",这就是它得到服务的原因。

因此,如果您不喜欢 mydomain.com 作为默认虚拟主机,请在它上面创建另一个(没有 ServerName),它只指向一个默认位置。

(顺便说一句,你有 /etc/httpd 而不是 /etc/apache2 因为不同的发行版做事不同。通常,httpd 在基于 CentOS 的服务器上,apache2 在 Debian 上family.Configuration布局也有很大的不同。这是正常的,不用担心。)

我假设您实际上已经将 ServerNameServerAlias 设置为适合您的安装?即他们正在服务的真实域...

我认为您可能需要删除 /var/www/html/mysite/

末尾的 /

此外,您可能希望阅读此处:https://httpd.apache.org/docs/2.4/urlmapping.html - 它解释了如何将文档映射到您的 documentRoot 目录下

确保 运行 来自 Apache 的用户有权读取这些文件(如果您可以在 AWS 中进行 chmod,则不确定)。此外,从 DocumentRoot 中删除所有尾部斜杠

提供错误日志路径(例如 ErrorLog /var/log/vhosts/error.log )以进行深入调试。

您在浏览域时遇到什么错误 mydomain.com?

请确保为该 EC2 的传入流量打开端口 80,否则默认情况下将拒绝所有流量

您可以检查几件事:

    - Please confirm you have pointed correct ip in A record under DNS management on domain registrar.

    - Are you trying to access site on www version or non-www version. If you are trying to access on www, probably it may not work as I cannot see any ServerAlias in your vhost block.Make sure to restart your apache after making change in vhost for ServerAlias.

- Remove forward slash at end of DocumentRoot line - it should read as /var/www/html/mysite

    - If possible place and entry for 127.0.0.1 mydomain.com in your /etc/hosts file.