Score:0

How do I configure httpd to host multiple websites on the same web server

mx flag

Using RHEL7.9, httpd 2.4.6 I have three sites in my /var/www/html/: /main /site1 /site2

The httpd.conf DocumentRoot points to /main

Listen 80
ServerName example.com
DocumentRoot "/var/www/html/main"

IncludeOptional conf.d/*.conf

And I have the following .conf files: main.conf

<VirtualHost <server ip address>:80>
    DocumentRoot "/var/www/html/main/"
    ServerName example.com
    ServerAlias test.example.com
    <Directory /var/www/html/main>
        Require all granted
    </Directory>
</VirtualHost>

site1.conf

<VirtualHost <server ip address>:80>
    DocumentRoot "/var/www/html/site1"
    ServerName example.com
    ServerAlias test.example.com
    <Directory /var/www/html/site1>
        Require all granted
    </Directory>
</VirtualHost>

site2.conf

<VirtualHost <server ip address>:80>
    DocumentRoot "/var/www/html/site2"
    ServerName example.com
    ServerAlias test.example.com
    <Directory /var/www/html/site2>
        Require all granted
    </Directory>
</VirtualHost>

So basically, all three are sharing identical Vhost configurations except for the DocumentRoot, which points to the respective directories for each site.

When I try to visit the site, I am greeted with /main's index.php, but when I navigate to example.com/site1, I am still seeing main's index page. Same goes with site 2.

What is wrong with my configuration? Are ServerName and ServerAlias supposed to be different from each VirtualHost?

Score:2
us flag

In your configuration, you are using the same ServerName and ServerAlias for all three virtual hosts, which is causing a conflict. The Apache HTTP Server uses the first VirtualHost that matches the incoming request to determine which document root to serve.

To resolve this issue, you need to specify unique ServerName and ServerAlias values for each VirtualHost. For example:

<VirtualHost <server ip address>:80>
DocumentRoot "/var/www/html/main/"
ServerName main.example.com
ServerAlias main.test.example.com
<Directory /var/www/html/main>
Require all granted
</Directory>
</VirtualHost>

site1.conf

<VirtualHost <server ip address>:80>
DocumentRoot "/var/www/html/site1"
ServerName site1.example.com
ServerAlias site1.test.example.com
<Directory /var/www/html/site1>
Require all granted
</Directory>
</VirtualHost>

site2.conf

<VirtualHost <server ip address>:80>
DocumentRoot "/var/www/html/site2"
ServerName site2.example.com
ServerAlias site2.test.example.com
<Directory /var/www/html/site2>
Require all granted
</Directory>
</VirtualHost>

After making these changes, you'll need to restart the Apache daemon.

demiglace avatar
mx flag
Used the server names and aliases specified here, but still when going to http://example.com/site1, it still loads the index from main. Any other config that I should check in this case? Have already tried restarting httpd service
Zareh Kasparian avatar
us flag
have you changed and updated the ServerName?
demiglace avatar
mx flag
yes, I changed site1's to: ServerName site1.example.com ServerAlias site1.test.example.com
Zareh Kasparian avatar
us flag
@demiglace, my friend when you setup a virtual directory, you can assume its a another website. What I can see in your URL, you are still addressing to your main address which is example.com. your site1 address is site1.example.com as seen in your ServerName configuration.
Score:1
in flag

Short answer is YES, ServerName must be different, otherwise how the client will distinguish them?

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.