简体   繁体   English

php shell_exec权限问题

[英]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 我正在以www-data身份运行,并尝试使用shell_exec(/foobar/script/myscript.sh)执行Shell脚本,但是当脚本尝试写入日志文件时出现以下错误

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 我还应该补充一点,我将www-data添加到了mybar组中,这就是我列出目录时显示的内容

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). Web服务器和php-fpm在不同的用户下运行(默认情况下不应如此)。 Try echo shell_exec('whoami'); 试试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). 您已经在启动php-fpm进程之后将www-data添加到mybar中,因此它仍然“不知道”它是什么(然后我认为重新启动fpm应该会有所帮助)。

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: 当我登录到ubuntu机器时,我以jack@jack身份登录,因此我可以在以下目录中创建文件,而无需发出sudo命令:

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 . www-data是执行命令的用户,因此您试图创建/修改/删除的任何目录或文件都必须由www-data拥有。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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