Modify DocumentRoot in Apache (Mac)

After you've installed Apache with Homebrew on macOS, you might want to change the DocumentRoot which points to the directory where your website resources reside (e.g. HTML, CSS, images). By default, DocumentRoot is located in /usr/local/var/www 1.

Here are the steps to follow to change DocumentRoot to map to a custom directory path:
  1. Place your website folder to the desired place.
    For example: /usr/local/var/www/my_website
  2. Open the file /usr/local/etc/httpd/httpd.conf with your favorite editor, search for DocumentRoot, and modify the following line by substituting your directory path within the double quotes:
    DocumentRoot "/your/directory/path"
    Also change the next line:
    <Directory "/your/directory/path">
  3. Restart your Apache server (if you want Apache to restart at login):
    $ sudo brew services restart httpd
  4. or if you don't want Apache to run as a background service:
    $ sudo apachectl restart
  5. Test the changes to DocumentRoot by opening http://localhost with your browser. You should see your local site's homepage.
Note: If I don't start Apache with sudo, I have problems accessing some pages of my local site with the following error message shown (on Chrome):
"This site can’t be reached: ERR_CONNECTION_REFUSED"
I tried to change the DocumentRoot directory owner to _www, and tried to set the directory permission to 777. But to no avail, I still had the same problem where some pages could be accessed and others not. It's only by restarting Apache with sudo that I was able to resolve this connection issue:
$ sudo brew services restart httpd

Notes

1. ^/Library/WebServer/Documents is also another default directory location used by Apache on macOS, see reference.

Comments

Popular posts from this blog

Deactivate conda's base environment on startup

Product review: SMONET wireless security camera system

Draw arrows with GIMP plugins