Sunday, April 3, 2022

[SOLVED] How do I use <VirtualHost> in httpd.conf Amazon linux EC2

Issue

I have tried all the possible solution to point mydomain.com to a specific folder under /var/www/html/mysite. First of all httpd.conf file is missing under /etc/httpd but it is found under /etc/httpd/conf. Also there is no /etc/apache2 folder.

To point my domain to /var/www/html/mysite I wrote the below code under /etc/httpd/conf/httpd.conf just below the line which says ServerAdmin root@localhost

<VirtualHost *:80>
    ServerAdmin [email protected]
    ServerName mydomain.com
    ServerAlias mydomain.com
    DocumentRoot /var/www/html/mysite/
</VirtualHost>

It did work but everything, even the IP is pointing to /var/www/html/mysite/ . I am not able to figure out where I am wrong. I just need mydomain.com to point to mysite directory.


Solution

Docs:

If no matching ServerName or ServerAlias is found in the set of virtual hosts containing the most specific matching IP address and port combination, then the first listed virtual host that matches that will be used.

Your virtual host of *:80 accepts all IPs on port 80, so it fits the "first listed virtual host that matches", and that's why it's getting served.

So if you don't like mydomain.com being the default virtual host, make another one above it (with no ServerName) that just points to a default location.

(BTW, you have /etc/httpd instead of /etc/apache2 because different distributions do things differently. Typically, httpd is on CentOS-based servers, apache2 on Debian family. Configuration layout is also fairly different. This is normal and nothing to worry about.)



Answered By - Amadan
Answer Checked By - David Goodson (WPSolving Volunteer)