Score:2

Why is my Apache Virtual Hosts configuration not working correctly?

mv flag

Background

I have been using an Apache server with only one site and this has been working really well. My working single-site config is at the bottom of the question. 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.

Attempted Solutions

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.

After spending a few days trying solutions and going through the Apache docs, I opened a new Stack Overflow question and was subsequently directed to this site for further help.

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-based virtual hosts, I wanted all three to be name-based virtual hosts with the below URLs:

  • its-site.test
  • its-cab.test
  • its-gallery.test

If this isn't recommended for some reason, I am flexible on the names, but I wanted to use name-based virtual hosts and three different URLs as the sites are unrelated.

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

  • M1 Macbook Air
  • Apache and PHP installed using homebrew

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.

djdomi avatar
za flag
is this a Mac Laptop?
HBruijn avatar
in flag
An error "refused to connect" suggest that nothing is listening (on port 80) . IIRC by default apache httpd packaged by brew only opens port 8080 and not the default http port 80. https://httpd.apache.org/docs/2.4/bind.html
GeorgeCiesinski avatar
mv flag
Yes this is a mac laptop. And I also configured `Listen 80` in `httpd.conf` which worked in my single-site config when I visited https://localhost:80 as the post I was following informed me of the same issue with port 8080.
Score:0
mv flag

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.

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.