skip to Main Content

Is there a way to make PHP CLI-invoked scripts to fwrite() as www-data?

Currently, if I create a text file and write to it using my own custom script php writeToLogFile.php, the file is created as root, and with very minimal permissions: rw- r– r–

This means then if I want to write further data to this file in apache (running as www-data), I get permission denied.

This is part of a larger system, so it wouldn’t be practical for me to chmod() or chown() each file I create. I also don’t want to set the umask() in PHP as this may interfere with other threads.

I should also mention the file is being written to an NFS share on a different server (all servers are Ubuntu). The folder is shared as: /myfolder/logsfiles 01.02.03.04(rw,sync,no_subtree_check,no_root_squash)

Thank you in advance.

2

Answers


  1. execute sudo su www-data && php writeToLogFile.php this will first switch user and than execute your code as www-data user only…

    Login or Signup to reply.
  2. php can’t change the userid that the process runs as, only privileged (e.g. setuid) programs can do this. You need to run the script as www-data.

    sudo -u www-data php writeToLogFile.php
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search