Fix permissions in Apache
I'm trying to use the built-in Apache server in MacOS mojave.
In my browser, accessing http://localhost/~myusername/mypage.html (in case of user dir) or http://localhost/mypage.html (in case of default dir) both throw the same error in the browser:
Forbidden You don't have permission to access /mysite/ on this server.
I' trying to make both the default dir /Library/WebServer/Documents
and an user dir ~/myusername/Sites
works.
I tried 2 solutions:
-
I tried to add a vhost in
/etc/apache2/extra/httpd-vhosts.conf
<VirtualHost *:80> ServerName localhost DocumentRoot /Library/WebServer/Documents/ </VirtualHost>
-
I tried to add a
myusername.conf
file in/etc/apache2/users
with the following content<Directory "/Users/myusername/Sites/"> Options Indexes MultiViews AllowOverride All Order allow,deny Allow from all </Directory>
Is there a guide to understanding permission errors on Mojave using Apple shipped Apache?
EDIT: It looks like the accepted answer here solved the reading permission for me. From the answer:
- Load the module
mod_userdir
in/etc/apache2/httpd.conf
- At the end of the
httpd.conf
make sure to loadhttpd-userdir.conf
- In
httpd-userdir.conf
include a local file in/etc/apache2/users/<username>.conf
Put a directory section into that file, containing the rules for the directory where your webserver files are located:
<Directory "/Users/<myusername>/Sites/"> AllowOverride All Options Indexes FollowSymLinks Require all granted </Directory>
Writing access to files is still a problem: Apache cannot edit a file if the ownership is mine (the admin user). I'm still wondering if there is a proper way to fix the permission without messing up each directory permissions every new project.
This is the script I'm using for testing write permission:
<?php
echo 'Current script owner: ' . get_current_user();
echo 'Current user:' . system('whoami');
$current = file_get_contents('people.txt');
$current .= "John Smith\n";
file_put_contents($file, $current);
I get the error:
Warning: file_put_contents(people.txt): failed to open stream: Permission denied
Solution 1:
Apache in macOS Server runs as the _www
user. Try granting this user write access to the directory with an ACL entry.
chmod +a "_www allow list,search,add_file,add_subdirectory,delete_child,write,append,file_inherit,directory_inherit" "/Users/<user>/Sites"