apachevirtualhost

Why is my Apache Virtual Hosts configuration not working correctly?


Background

I have been using an Apache server with only one site and this has been working really well. Recently, I have had to create two more sites locally, and I found this guide that showed how to do this. I also referred to this stack overflow question which also helped me a lot.

My problem is that when I visit the three hosts I made in /etc/hosts and configured in httpd-vhosts.conf, it is not working and says its-site.test refused to connect. when I try to visit it in browser.

I attempted to resolve the issue by referring to this stack overflow question which helped me to understand the configuration much better, but it is still not working and results in the same issue.

Needless to say I am very new to Apache so I might be overlooking something obvious.

My intended setup

I have three different sites which I want to use named virtual hosts to access in browser during my development process. Rather than using one local host and a few named domains, I wanted each of them to be named domains:

Hosts file

/etc/hosts

##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1       localhost
255.255.255.255 broadcasthost
::1             localhost

127.0.0.1       its-site.test
127.0.0.1       its-cab.test
127.0.0.1       its-gallery.test

httpd configuration

/opt/homebrew/etc/httpd/httpd.conf

Listen 80

Include /opt/homebrew/etc/httpd/extra/httpd-vhosts.conf
LoadModule rewrite_module lib/httpd/modules/mod_rewrite.so
ServerName localhost:80

# Also enabled PHP but not including the config here as it may not be relevant

vhosts configuration

/opt/homebrew/etc/httpd/extra/httpd-vhosts.conf

<VirtualHost *:80>
    ServerName its-site.test
    ServerAlias *.its-site.test
    DocumentRoot "/Users/ciesinsg/Documents/Repositories/its-site/web"

    <directory "/Users/ciesinsg/Documents/Repositories/its-site/web">
        Require all granted
        AllowOverride All
    </directory>
</VirtualHost>


<VirtualHost *:80>
    ServerName its-cab.test
    ServerAlias *.its-cab.test
    DocumentRoot "/Users/ciesinsg/Documents/Repositories/its-cab/web"

    <directory "/Users/ciesinsg/Documents/Repositories/its-cab/web">
        Require all granted
        AllowOverride All
    </directory>
</VirtualHost>


<VirtualHost *:80>
    ServerName its-gallery.test
    ServerAlias *.its-gallery.test
    DocumentRoot "/Users/ciesinsg/Documents/Repositories/its-gallery/web"

    <directory "/Users/ciesinsg/Documents/Repositories/its-gallery/web">
        Require all granted
        AllowOverride All
    </directory>
</VirtualHost>

Question

Does anybody see where I made a mistake with this configuration and can point me towards the solution?

Additional Information

Previous Single Site working Config

As stated earlier, I had this working well when I was serving only one site. My only configuration was with httpd.conf and did not configure /etc/hosts or httpd-vhosts.conf. My working httpd.conf file looked like:

Listen 80
DocumentRoot "/Users/ciesinsg/Documents/Repositories/its-gallery/web"
<Directory "/Users/ciesinsg/Documents/Repositories/its-gallery/web">

# Also enabled PHP

I removed the DocumentRoot and <Directory> when I configured VirtualHosts.


Solution

  • I was able to resolve the problem using the below solution:

    Test PHP

    First, I tested my configurations in etc/hosts using ping:

    ciesinsg@NB-00304-H14872 ~ % ping its-site.test
    PING its-site.test (127.0.0.1): 56 data bytes
    64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.051 ms
    

    This confirmed that the site was routing to the right IP matching my config.

    Check Virtual Hosts

    This was done manually by referring to these docs and double checking the guide I followed. I am not aware of any tests to confirm the configuration is working correctly, but after reading a few guides and docs about virtual hosts, I was pretty confident that the configuration did not have any issues and the problem must be elsewhere.

    Check httpd.conf

    This configurations is the longest out of the ones I listed by default. Consequently, this was a manual process. My solution involved opening httpd-default.conf which is an adjacent configuration file that appears to have the default configurations from the httpd.conf file. On the Homebrew installation of Apache, this is located in the /opt/homebrew/etc/httpd/extra/ where the httpd.conf file is also located. I compared these two configurations side by side and removed my configurations from my earlier attempts.

    In my case, I found that I had accidentally duplicated my PHP configuration in the httpd.conf file and in httpd-php.conf file. I removed all of my configurations and followed through the guide once again, ensuring that my Include and LoadModule statements were placed after the same statements.

    After removing my configurations and configuring it more carefully, I tested the sites in the browser and they were all working as expected.