'Apache change home directory get Permission Forbidden error

Error

Changed Apache directory from /Library/WebServer/Documents to /Users/my_username/Documents/web-apache, typed localhost or localhost/index.php in the browser will get Forbidden error.

Environment

  • Mac:
    MacOS is Monterey, Chip is Apple M1, MacBook Air (M1, 2020)
  • Mysql: Ver 8.0.28 for macos11 on arm64 (MySQL Community Server - GPL)
  • PHP: PHP 8.1.5 (cli) (built: Apr 16 2022 00:03:52) (NTS)
  • Apache: 2.4.51 (Unix)

Tried Methods

  1. change Apache config at /etc/apache2/httpd.conf
    uncomment Include /private/etc/apache2/extra/httpd-vhosts.conf
    change vhosts config at /etc/apache2/extra/httpd-vhosts.confg
    add this block

<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot "/Users/my_username/Documents/web-apache"
    ServerName localhost
    ServerAlias www.web-apache.com
    ErrorLog "/private/var/log/apache2/web-apache.com-error_log"
    CustomLog "/private/var/log/apache2/web-apache.com-access_log" common

    <Directory "/Users/my_username/Documents/web-apache">
        Options Indexes FollowSymLinks Multiviews
        AllowOverride None
        Require all granted
    </Directory>

</VirtualHost>

run apachectl -S to verify, get

VirtualHost configuration:
ServerRoot: "/usr"
Main DocumentRoot: "/Library/WebServer/Documents"
Main ErrorLog: "/private/var/log/apache2/error_log"
Mutex rewrite-map: using_defaults
Mutex default: dir="/private/var/run/" mechanism=default 
Mutex mpm-accept: using_defaults
PidFile: "/private/var/run/httpd.pid"
Define: DUMP_VHOSTS
Define: DUMP_RUN_CFG
User: name="_www" id=70 not_used
Group: name="_www" id=70 not_used

vhost error log is working
tried apachectl -t get Santax OK

  1. change the directory file permission chmod -R 777 /Users/my_username/Documents/web-apache

and change above directories too chmod 777 /Users, chmod 777 /Users/my_username

  1. change httpd.conf DocumentRoot and Directory directory without including vhost.conf

  2. use userdir instead of vhost.

  3. change User in httpd.conf to my_usernmae

  4. change both User and Group in httpd.conf

  5. set the /Users/my_username/Documents/web-apache as Shared Folder



Solution 1:[1]

change the directory file permission chmod -R 777 /Users/my_username/Documents/web-apache

Lets start there - its where the problem is and where the really bad thing is.

Take some time to learn how Unix permissions work. Your webserver should have the minimum permissions necessary to do its job - but you've given it EVERYTHING on these files. The recent that you're still getting an error in Apache is because you didn't fix the permissions on the directories above the document root:

# fix the really dangerous permissions first....

chmod 0755 /Users
chmod 0775 /Users/my_username
chmod -R 0770 /Users/my_username/Documents/web-apache

# Now set the permissions properly....
chmod 0775 /Users/my_username/Documents

# This appears to be what you missed last time
chmod 0775 /Users/my_username/Documents/web-apache

# but 0777 permissions are ALWAYS BAD
chmod -R o+r /Users/my_username/Documents/web-apache/*
find /Users/my_username/Documents/web-apache -type d -exec chmod o+x {} \;

This will give the apache uid read permissions on the files in the new document root and execute permissions on the directories.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 symcbean