简体   繁体   中英

Problems with php shell_exec permissions

I'm running as www-data and I'm trying to execute a shell script using shell_exec(/foobar/script/myscript.sh) , but I'm getting the following error when the script attempts to write to a log file

cannot create /foobar/foo.log: Permission denied 

However, I don't run into any problems if I try running the script directly from the terminal. ie

$ sudo su www-data
$ /foobar/script/myscript.sh
$

Any idea what could be going on here?

I should also add that I added www-data to the group mybar and that this is what's showing up when I list the directory

drwxrwxr-x 3 mybar mybar  4096 May 14 14:18 foobar     # ls -l /


-rw-rw-r-- 1 mybar mybar 2824 May 15 09:57 foo.log     # ls -l /foobar

I think there can be two options:

  1. web-server and php-fpm are run under different users (by default that should not be). Try echo shell_exec('whoami');
  2. You have added www-data to mybar after php-fpm process had been started so it still "doesn't know" that it is (then I think restart of fpm should help).

This works for me:

sudo chown www-data:www-data -R foobar/

The directory you are trying to create the file in must belong to whoever is executing the command.

You can type:

ls -la 

to see who it belongs to.

You should see something like this:

drwxr-xr-x  8 jack jack  4096 Jul 22 11:36 application

When I am logged into my ubuntu machine I am logged in as jack@jack so I can create files in the following directory without issuing the sudo command:

drwxr-xr-x  8 jack jack  4096 Jul 22 11:36 application

because it is owned by the current user.

When you try running a script that is executed by visiting a webpage. www-data is the user that is executing the command so any directory or file that you are trying to create/modify/delete must be owned by www-data .

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