Second Virtual Host's DocumentRoot is changed after launching ec2 based on customized Amazon Linux AMI

人盡茶涼 提交于 2020-01-24 23:05:14

问题


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

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>

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

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>

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!


回答1:


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.

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>

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).

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

so will the new httpd-vhosts.conf file.

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.



来源:https://stackoverflow.com/questions/19120113/second-virtual-hosts-documentroot-is-changed-after-launching-ec2-based-on-custo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!