Search code examples
macosapacheconfigurationvirtualhost

Apache virtualHost ignored or pointing to the default one


I'm trying to setup my Apache on OSX, but I'm not able to make my local url 'awr.local' point to the correct path. I mean whether I type http://localhost or http://awr.local, it always shows me the index.html page in my 'localhost' vhost path. I've restarted my httpd service countless times, with or without sudo.

Any help would be greatly appreciated, thanks :'|

I've been following a tutorial (https://getgrav.org/blog/macos-sierra-apache-multiple-php-versions), here are the steps about Apache :

Disabling the bundled Apache and installing the one from homebrew :

sudo apachectl stop
sudo launchctl unload -w /System/Library/LaunchDaemons/org.apache.httpd.plist 2>/dev/null
brew install httpd

Then, some httpd.conf modifications :

# changed Listen 8080 to :
Listen 80
[...]
# changed DocumentRoot "/usr/local/var/www" to :
DocumentRoot "/Users/wallace/dev/default"
[...]
# changed <Directory "/usr/local/var/www"> to :
<Directory "/Users/wallace/dev/default">
[...]
# changed AllowOverride None to :
AllowOverride All
# uncommented :
LoadModule rewrite_module lib/httpd/modules/mod_rewrite.so
[...]
# changed
# User _www
# Group _www
# to :
User wallace
Group staff
[...]
# added the missing line :
ServerName localhost

Until this point everything seemed to be working fine, I've installed PHP and MariaDB without any problem.

Then came the virtual hosts part :

Some other httpd.conf modifications :

# uncommenting these lines
LoadModule vhost_alias_module lib/httpd/modules/mod_vhost_alias.so
[...]
Include /usr/local/etc/httpd/extra/httpd-vhosts.conf

Editing the file /usr/local/etc/httpd/extra/httpd-vhosts.conf :

<VirtualHost *:80>
    DocumentRoot "/Users/wallace/dev/default"
    ServerName localhost
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot "/Users/wallace/dev/awr"
    ServerName awr.local
</VirtualHost>

The /etc/hosts file :

127.0.0.1       localhost
255.255.255.255 broadcasthost
::1             localhost
127.0.0.1       awr.local
::1             awr.local

The output of 'httpd -S' :

VirtualHost configuration:
*:80                   is a NameVirtualHost
         default server localhost (/usr/local/etc/httpd/extra/httpd-vhosts.conf:25)
         port 80 namevhost localhost (/usr/local/etc/httpd/extra/httpd-vhosts.conf:25)
         port 80 namevhost awr.local (/usr/local/etc/httpd/extra/httpd-vhosts.conf:30)
ServerRoot: "/usr/local/opt/httpd"
Main DocumentRoot: "/Users/wallace/dev/default"
Main ErrorLog: "/usr/local/var/log/httpd/error_log"
Mutex rewrite-map: using_defaults
Mutex default: dir="/usr/local/var/run/httpd/" mechanism=default
Mutex mpm-accept: using_defaults
PidFile: "/usr/local/var/run/httpd/httpd.pid"
Define: DUMP_VHOSTS
Define: DUMP_RUN_CFG
User: name="wallace" id=501 not_used
Group: name="staff" id=20 not_used

Solution

  • Well, today when I was preparing to try the answer of Juan, I had something new : instead of serving me the localhost, the awr.local url showed me a 403 forbidden. After a new research & doc reading, I managed to make it work like this :

    My /usr/local/etc/httpd/extra/httpd-vhosts.conf file :

    The Require all granted is the most important part.

    <VirtualHost *:80>
        DocumentRoot "/Users/wallace/dev/default"
        ServerName localhost
        <Directory "/Users/wallace/dev/default">
            Options Indexes FollowSymLinks
            AllowOverride All
            Require all granted
        </Directory>
    </VirtualHost>
    
    <VirtualHost *:80>
        DocumentRoot "/Users/wallace/dev/awr"
        ServerName awr.local
        <Directory "/Users/wallace/dev/awr">
            Options Indexes FollowSymLinks
            AllowOverride All
            Require all granted
        </Directory>
    </VirtualHost>
    

    I had to restart Apache with sudo for it to work :

    sudo brew services stop httpd && sudo brew services start httpd