如何在单个 AWS EC2 实例上托管多个域和子域

How to host multiple domains and subdomains on single AWS EC2 instance

我正在尝试在 AWS 中设置 2 个域(domain1.com 和 domain2.com)以及几个子域 (app.domain1.com) 并 运行 它们在单个实例(亚马逊 Linux、PHP、MySQL)。

我已经使用以下配置在 AWS Route53 中设置了 3 个托管区域。

Hosted zone 1:
domain1.com
Type A
52.108.XX.YY

Hosted Zone 2
domain2.com
Type A
52.108.XX.YY

Hosted Zone 3
app.domain1.com
Type A
52.108.XX.YY

此外,我在 VirtualHost 标签的 http.conf 文件中添加了以下代码。

<VirtualHost *:80>   
     ServerName domain1.com   
     DocumentRoot "/var/www/html/domain1"   
     ErrorLog "logs/domain1-error_log"  
     CustomLog "logs/domain1-access_log" common  
     </VirtualHost>
     
     <VirtualHost *:80>   
     ServerName domain2.com   
     DocumentRoot "/var/www/html/domain2"  
     ErrorLog "logs/domain2-error_log"  
     CustomLog "logs/domain2-access_log" common  
     </VirtualHost>
     
     <VirtualHost *:80>  
     ServerName app.domain1.com   
     DocumentRoot "/var/www/html/app"  
     ErrorLog "logs/app.domain1-error_log"  
     CustomLog "logs/app.domain1-access_log" common  
     </VirtualHost>

但是,只有 domain1.com 和 domain2.com 得到解决。当我访问 app.domain1.com 时,它给我一个“找不到服务器”的错误。请帮助如何设置子域 - 托管区域设置或 httpd.conf 有问题吗?

您可以按照link上的教程进行操作:http://brianshim.com/webtricks/host-multiple-sites-amazon-ec2/

根据 link,一个常见的错误是:

Did it work? If not, here is one possible cause. There might be another configuration file interfering with this one. Check for another .conf file in /etc/httpd/conf.d. Often your primary httpd.conf will include another .conf file in this directory. Make sure it doesn’t have some Virtual Host settings which are interfering with yours.

设置配置后,您应该运行:

sudo service httpd restart

好的,在浏览了各种站点并进行了大约 2 小时的修改之后,我已经准备就绪。这是如何做到这一点。

基本上,每个域名的托管区域 (HZ) 不应超过 1 个,否则情况会很糟糕。如果您的域名超过 1 HZ,请删除为子域创建的 HZ。

每个HZ会有4条记录-

Following two records are created by default. Do not edit/delete them.
NS - This is the name server record. If AWS Route53 is not your registrar, use ns-servernumber.awsdns-number.com. and other three (4 total) records to change name servers for your registrar.
SOA - Let this record be. DO NOT TOUCH THIS.

Create following two Record Set (blue button).
A - Leave Name blank. Select A-IPv4 address for Type. In Value enter the IP address for your Elastic Load Balancer or EC2 instance.
CNAME - Add * (asterisks/wildcard) in the name field. Select CNAME from the drop down for Type In Value enter the domain name.

现在创建 http.conf 文件并像我在问题中那样构建虚拟主机。

现在应该可以了:)

这可以通过在 AWS-EC2 实例上安装 Apache HTTP Server 并配置 VirtualHost 对于 Amazonian

建议的每个 DNS 或子 DNS

为简洁起见,请阅读此 thread discussion, this official example and techrepublic post


希望对您有所帮助!