1

I've been really struggling with this for the past few days, and I'm at a lost. Here's what's going on.

I have an EC2 instance (Amazon Linux 2023) behind an ELB and a CloudFront distribution that I want to host two (or, eventually, more) subdomains using the VirtualHost directive. However, no matter what I do, any request, no matter what the subdomain is, always goes right to the default document root (/var/www/html).

All of the information I can find on similar setups always note to set Route 53 to the public IP address for the server, but, again, I'm trying to do this through CloudFront/an ELB. I don't want to use the public IP address for my instance; I want to use the regular subdomains I have picked out.

Here's the setup I have:

  • Route 53 as an A record for both subdomains, both going to the CloudFront distribution URL
  • CloudFront is pointed at my ELB
  • ELB has rules to send either firstsd.mysite.com or secondsd.mysite.com to the EC2 instance in question
  • I have a security certificate setup for the mysite.com domain

I've tried a number of different approaches.

First, I tried adding the VirtualHost directives straight to httpd.conf in a bunch of different configurations. I tried keeping the VirtualHost blocks simplistic, like this:

<VirtualHost *:80>
    ServerName firstsd.mysite.com
    DocumentRoot "/var/www/html/firstsd"
</VirtualHost *:80>

<VirtualHost *:80>
    ServerName secondsd.mysite.com
    DocumentRoot "/var/www/html/secondsd"
</VirtualHost>

When this didn't work, I tried modifying it to this:

<VirtualHost *:80>
    ServerName firstsd.mysite.com
    DocumentRoot "/var/www/html/firstsd"
    <Directory "/var/www/html/firstsd">
        DirectoryIndex index.html index.php
        Require all granted
        AllowOverride all
        Options Indexes FollowSymLinks
    </Directory>
</VirtualHost *:80>

<VirtualHost *:80>
    ServerName secondsd.mysite.com
    DocumentRoot "var/www/html/secondsd"
    <Directory "/var/www/html/secondsd">
        DirectoryIndex index.html index.php
        Require all granted
        AllowOverride all
        Options Indexes FollowSymLinks
    </Directory>
</VirtualHost>

I also tried the above by changing the *:80 to *:443 and also duplicating it to cover both *:80 and *:443.

The next thing I tried was to create new .conf files, as per the guide located here. I still got the same result: I ended up in the default home directory for my server.

Finally, I tried creating a brand new CloudFront distribution for each subdomain, adding /firstsd and /secondsd to the origin path option. Instead of hitting the default directory, I got a 403 error (which, admittedly, felt like progress on some level).

Any help on this would be greatly appreciated. Thanks!

1 Answer 1

0

Ensure that your Route 53 records are correctly set up to point to your CloudFront distribution. Confirm that the CloudFront distribution is correctly configured to forward requests to the ELB. Your Apache VirtualHost configuration should include both *:80 and *:443 for both subdomains. You can use the following template.

<VirtualHost *:80>
    ServerName firstsd.mysite.com
    DocumentRoot "/var/www/html/firstsd"
    # Additional configuration for the first subdomain
</VirtualHost>

<VirtualHost *:80>
    ServerName secondsd.mysite.com
    DocumentRoot "/var/www/html/secondsd"
    # Additional configuration for the second subdomain
</VirtualHost>

<VirtualHost *:443>
    ServerName firstsd.mysite.com
    DocumentRoot "/var/www/html/firstsd"
    # Additional configuration for the first subdomain over HTTPS
</VirtualHost>

<VirtualHost *:443>
    ServerName secondsd.mysite.com
    DocumentRoot "/var/www/html/secondsd"
    # Additional configuration for the second subdomain over HTTPS
</VirtualHost>

Check your security groups on the ELB and EC2 instances to make sure they allow traffic on ports 80 and 443. Additionally, ensure that your EC2 instance's firewall is not blocking incoming traffic.
Make sure that your SSL certificate is correctly associated with your CloudFront distribution.(It can be quite challenging for beginners, I've been stuck there many times myself)
At the end if the problem i still there, check Apache error logs (/var/log/httpd/error_log) and access logs (/var/log/httpd/access_log) for any errors or unexpected behavior. Also, check the CloudFront and ELB logs for any issues.(If I were in your shoes, I'd begin by examining the logs, it's a practice that can enhance your expertise)

2
  • 1
    I sincerely appreciate the info. We ended up just creating a separate instance for the subdomain, due to a lack of time. That said, I'm going to keep this information at hand, just in case it comes up again in the future. Most of what you're suggesting I've checked, but I always forget about those damn logs! Anyway, thanks again!!! Oct 17 at 20:28
  • @wholelottabob You're welcome :-) , Good luck
    – R1w
    Oct 17 at 21:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .