Score:0

How do I add a second virtual host to an Apache server config?

ug flag

I have an Apache server running on an AWS EC2 instance. Currently, one virtual host is running successfully on the server, and I want to add a second virtual host / domain, but I can't figure out how to get it working.

Anonymizing the data, here's the original information I added to httpd.conf to get the first virtual host running:

<Directory "/var/www/site-1/public">
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

<VirtualHost *:80>
    DocumentRoot "/var/www/site-1/public"
    ServerName "site-1.com"
    ServerAlias "*.site-1.com"
    RewriteEngine on
    RewriteCond %{SERVER_NAME} =site-1.com [OR]
    RewriteCond %{SERVER_NAME} =*.site-1.com
    RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

I then have the following registed in Google Domains for site-1 (with a fake IP address):

Host name     Type  TTL      Data
site-1.com    A     1 hour   4.184.56.145
*.site-1.com  A     1 hour   4.184.56.145

I then used Certbot to add an SSL cert, which added the following line to httpd.conf and an ACME challenge to the Google Domains DNS settings:

Include /etc/httpd/conf/httpd-le-ssl.conf

This is all working fine. I then purchased a second domain (site-2.com), and for now, I'm just trying to get the virtual host running with a very simple index.html file in the root of the site directory (i.e., /var/www/site-2).

For that, I added the following at the very bottom of httpd.conf:

<Directory "/var/www/site-2">
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

<VirtualHost *:80>
    DocumentRoot "/var/www/site-2"
    ServerName "site-2.com"
</VirtualHost>

And the following DNS settings were added to Google Domains for site-2:

Host name     Type  TTL      Data
site-2.com    A     1 hour   4.184.56.145

It's been several hours since all these settings were added for site-2, and I restarted the Apache server several times with sudo systemctl restart httpd. However, when I go to site-2.com, I get the following page, which is not the sample index.html file I placed under /var/www/site-2:

enter image description here

It's also worth noting that the favicon for site-2 is actually the favicon for site-1. Not sure why. In addition, the directory/file permissions/ownership for site-2 is the same as for site-1.

Can anyone provide any ideas for why this isn't working? For what it's worth, after doing quite a bit of research, I thought I had to add the following line to httpd.conf, but doing so and restarting the server doesn't make a difference:

NameVirtualHost *:80

Edit: I just figured something out. For whatever reason, site-2 is pointing to the var/www/html directory. If I put a sample index.html file into that directory, then it loads correctly when I go to site-2.com. Why is that? Why is the following virtual-host block not working?

<VirtualHost *:80>
    DocumentRoot "/var/www/site-2"
    ServerName "site-2.com"
</VirtualHost>
dexter avatar
ru flag
Are you actually getting site-1 instead of site-2? Is site-2.com resolvable? If not for testing add it in your testing machine hosts file. According to Apache http docu, put `Listen 80` on top of your configuration.
ug flag
No, I don't get `site-1` instead of `site-2` for site-2.com. I get the Apache Test Page shown above when I go to site-2.com. I have a bunch of virtual hosts running on my local machine, and they all work fine. I already have `Listen 80` at the top of the `httpd.conf` file. Thanks for your suggestions, dexter, but not sure if any of that changes anything.
Score:0
ug flag

Honestly don't know what the deal is, but it just started working. Maybe it was a DNS propagation issue (not sure), but the config I listed in my original question started working.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.