基于定制亚马逊的Linux AMI启动EC2后二虚拟主机的DocumentRoot改变亚马逊、虚拟主机、Linux、AMI

2023-09-11 23:46:23 作者:时之彼处,

我搜索了两天,但我无法找到此行为的解释。

I've searched for two days, but I couldn't find an explanation for this behavior.

我跟AWS文档。有建议亚马逊的Linux AMI(AKI-c48f51d9)我推出的EC2实例。然后定制我的EC2实例,以我的需求,其中包括Apache的配置:/etc/httpd/conf/httpd.conf中 我的自定义创建了两个虚拟主机,如下图所示:

I have followed AWS docs. I've launched ec2 instance with recommend Amazon Linux AMI (aki-c48f51d9). Then customized my ec2 instance to my needs, including Apache's config: /etc/httpd/conf/httpd.conf My customization created two Virtual Hosts, as indicated below:

<VirtualHost *:80>
    ServerName www.domain1.com
    ServerAlias domain1.com
    ServerAdmin support@domain1.com
    DocumentRoot "/var/www/html/folder1"
</VirtualHost>

<VirtualHost *:80>
    ServerName subdomain.domain1.com
    ServerAdmin support@domain1.com
    DocumentRoot "/var/www/html/folder2"
</VirtualHost>

本进行了测试和预期一样,则路由到/ var / www / html等/ FOLDER2。

This was tested and worked as expected, routing requests for subdomain.domain1.com to /var/www/html/folder2.

于是我接着创建一个自定义的AMI从这个EC2实例。成功的AMI创建后,我用这个AMI启动一个新的EC2实例。 现在,我惊讶的是,在这个新的EC2实例,我的第二个虚拟主机的配置略有更改为:

So I proceeded creating a custom AMI from this ec2 instance. After successful AMI creation, I used this AMI to launch a new ec2 instance. Now, for my surprise, on this new ec2 instance, the configuration of my second virtual host was slightly changed to:

<VirtualHost *:80>
     ServerName subdomain.domain1.com
     ServerAdmin support@domain1.com
     DocumentRoot "/var/www/html/folder1"
</VirtualHost>

注意的DocumentRoot某种程度上改变了由原来的FOLDER2到文件夹1,一样的默认虚拟主机(第一个)。发生了什么?我想AP preciate任何帮助,可以点我对这个问题的解决。在此先感谢!

Notice that DocumentRoot somehow changed from original "folder2" to "folder1", same as default virtual host (first one). What happened? I would appreciate any help that could point me on resolution for this issue. thanks in advance!

推荐答案

我得到了完全相同的问题,当我有我的虚拟主机条目主的的 /etc/httpd/conf/httpd.conf中的文件。所述第二的DocumentRoot物被覆盖为等同于所述第一

I was getting the exact same problem when I had my virtual host entries in the main /etc/httpd/conf/httpd.conf file. The second DocumentRoot was being overwritten to be equivalent to the first.

要解决这个问题,我删除了虚拟主机code(虽然我离开了NameVirtualHost活动)

To solve this problem, I removed the virtual host code (although I left the NameVirtualHost active)

<VirtualHost *:80>
    ServerName www.domain1.com
    ServerAlias domain1.com
    ServerAdmin support@domain1.com
    DocumentRoot "/var/www/html/folder1"
</VirtualHost>

<VirtualHost *:80>
    ServerName subdomain.domain1.com
    ServerAdmin support@domain1.com
    DocumentRoot "/var/www/html/folder2"
</VirtualHost>

和把它添加到位于 /etc/httpd/conf.d/httpd-vhosts.conf 的(注意,这是在conf.d一个新的文件文件夹,而不是conf文件夹)。

and added it to a new file located at /etc/httpd/conf.d/httpd-vhosts.conf (notice that this is in the conf.d folder and not the conf folder).

如果默认的httpd.conf文件仍然会自动添加在conf.d文件夹中的所有文件:

If the default in your httpd.conf file still adds all of the files in the conf.d folder automatically:

# Supplemental configuration
#
# Load config files in the "/etc/httpd/conf.d" directory, if any.
IncludeOptional conf.d/*.conf

所以将新的httpd-vhosts.conf文件。

so will the new httpd-vhosts.conf file.

我使用上的弹性魔豆环境,我的EC2实例,所以以后我创建的AMI,改变了实例配置为使用自定义的AMI ID,EC2实例是基于该AMI仍然有正确的文档根。

I was using my EC2 instance on an Elastic Beanstalk environment, so after I created the AMI and changed the Instance Configuration to use that Custom AMI ID, the EC2 Instance that was based on that AMI still had the correct document roots.