macOS Sierra - localhost stops working when virtual host is enabled

Probably you missed one or more modifications. To enable virtual hosts in macOS 10.12.0 (Client) do the following:

  • edit the file /etc/apache2/httpd.conf:

    uncomment line 160:

    #LoadModule vhost_alias_module libexec/apache2/mod_vhost_alias.so
    

    to

    LoadModule vhost_alias_module libexec/apache2/mod_vhost_alias.so
    

    replace in line 213

    #ServerName www.example.com:80
    

    by

    ServerName localhost
    

    uncomment line 509:

    #Include /private/etc/apache2/extra/httpd-vhosts.conf
    

    to

    Include /private/etc/apache2/extra/httpd-vhosts.conf
    
  • edit the file /etc/apache2/extra/httpd-vhosts.conf and replace the content by

    <VirtualHost *:80>
        ServerName localhost
        DocumentRoot "/path/to/localhost"
    
        <Directory "/path/to/localhost">
            Options Indexes FollowSymLinks
            AllowOverride All
            Order allow,deny
            Allow from all
            Require all granted
        </Directory>
    </VirtualHost>
    

    Your site has to reside in /path/to/localhost/

  • Check the apache config with: sudo apachectl -S. The first two lines should yield:

    VirtualHost configuration:
    *:80                   localhost (/private/etc/apache2/extra/httpd-vhosts.conf:23)
    
  • Start apache with sudo apachectl start
  • Now – after emptying the browser caches – enter localhost in the address bar of your browser.

In my case, OS X Sierra install made copies of my httpd config files with suffix [filename]~previous. Then it disabled lots of modules in httpd.conf (which is typical for some reason after a OS X upgrade). And then Apache kept silently crashing ... totally bewildering me.

$ ps ax | grep http

  [ no http was listed! ]

# tail -f  /var/log/system.log

  [ this showed that apache2 was attempting to restart every 10 seconds ]

# apachectl

  [ this showed the missing log directory error ]

Somehow during OS X upgrade, it kept a vhost I had made directly in httpd.conf (not typically wise, better to put custom stuff extra directory), and that vhost had a non-standard logging folder. OS X upgrade deleted that custom logging folder (odd that it would do that actually), and so upon restart it was silently failing since the log path had become invalid for the custom vhost I had in httpd.conf

I created directories to honor that non-standard logging folder. Huzzah, within 10 seconds apache2 had restarted and normal-ness returned. I still had to enable modules again in httpd.conf, also uncomment several lines to pull in additional configurations from extra. But once web server is running, a quick browser check to see what sites were still unreachable (or being redirected) allowed those config lines to be restored.


Thanks, this works. But if you are trying to access PHP website then you have to load libexec/apache2/libphp5.so module. Just uncomment:

LoadModule php5_module libexec/apache2/libphp5.so