简体   繁体   中英

How to create directory with owner permission on LAMP server using PHP?

I am currently running a PHP application on a Bitnami LAMP Stack. I am creating a directory to store files. The code is as follow:-

$directory = "../data/folder";

if (!is_dir($directory)) {
    $owner = "bitnami";
    $group = "bitnami";
    mkdir($directory, 0777, TRUE);

    exec("sudo chown -R ".$owner.":".$group." ".$directory);
}

When the directory is created on server the owner/group is daemon/daemon. I want the owner and group when directory is created should be bitnami/bitnami. I tried runnig the sudo chown command but it doesn't seem to be working. When i run the same command from shell the command makes bitnami the owner.

Please let me know if its possible to make bitnami the owner when PHP is creating the directory.

The current user when this code is beeing run is daemon.

exec tries to run the command-line sudo command, as daemon which quite possibly is either not setup or asking for password. I want to first off note, that php has chown ( http://php.net/manual/en/function.chown.php ), however only a super-user can run that command, and I assume the instance is not beeing run by a super-user.

I see a few suggestions through history of adding the users to the same group to handle permissions issues. Another option is to setup the sudoers file (/etc/sudoers) to allow daemon to run chown without password. I am not super uptodate on how to update this, but from https://unix.stackexchange.com/questions/18830/how-to-run-a-specific-program-as-root-without-a-password-prompt something like this

       daemon ALL = (root) NOPASSWD: /usr/bin/chown

should work, and let your exec should succed in that case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM