php how do I use fopen() while being able to edit files in the website directory as non-root user?

I'm running a local apache2 web server at /var/www/.

In order to use the fopen() function in php, the user and group of my site directory must both be 'www-data'. Problem is, when I try to edit files as normal user in the website directory, the normal user has insufficient permissions to write.

1 Answer

To enable both your ordinary user and the php interpreter to read and write the same files - and assuming the www-data group already has read + write permissions on the files - you can add your user to the www-data group.

sudo gpasswd -a david www-data

This won't take effect until you logout and log back in again. Until then, you will be able "spoof" being in the www-data group with:

newgrp www-data

This will cause your shell to fork into a newgrp process, during which you'll have a new shell session with belonging to www-data group.
You can exit this with Ctrl+D

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like