简体   繁体   English

PHP中的“ exec('sh foo.sh')”不起作用

[英]“exec('sh foo.sh')” in PHP not working

I've recently set up my Apache2 Server on my Linux machine. 我最近在Linux机器上设置了Apache2服务器。 Now I've wanted to execute a PHP script (index.php), which runs a shell script (foo.sh), which creates a folder in my home directory, but the directory was not created. 现在,我想执行一个PHP脚本(index.php),该脚本运行一个shell脚本(foo.sh),该脚本在主目录中创建一个文件夹,但未创建目录。

These are the original two files: 这些是原始的两个文件:

foo.sh: foo.sh:

#!bin/bash

mkdir /home/lorenzo/testDir

index.php: index.php文件:

<?php

exec('sh test.sh');

?>

So, I thought maybe the problem occurs because of privileges or something, and indeed after I changed the files to that: 因此,我认为问题可能是由于特权或某些原因而发生的,确实在我将文件更改为以下内容之后:

foo.sh: foo.sh:

#!bin/bash

echo "Hello world"

index.php: index.php文件:

<?php

$temp=exec('sh test.sh');
echo $temp;

?>

I saw the output Hello World on my website. 我在网站上看到了输出Hello World

So the PHP script is executed and it runs the shell script. 这样就执行了PHP脚本并运行了shell脚本。 But why can't the shell script execute the mkdir command? 但是,为什么shell脚本不能执行mkdir命令?

This indeed is most likely a permission issue. 这确实很可能是权限问题。

You first have to figure out which user apache runs at. 您首先必须确定运行哪个用户apache。 This is usually www-data (on Debian-ish Linuxes, such as Ubuntu) or apache (on RedHat-ish Linuxes) or something along the lines. 这通常是www-data (在Debian-ish Linux,例如Ubuntu)上或apache (在RedHat-ish Linuxes上)或类似的东西。 A ps -eF | grep apache ps -eF | grep apache ps -eF | grep apache will reveal the user. ps -eF | grep apache将向用户展示。

After you figured that out, make sure that the apache user has the appropriate rights in your home directory. 在弄清楚之后,请确保apache用户在您的主目录中具有适当的权限。 You can either add it to your user group (using usermod -a -G ... and then chmod g+w ~ ) or allow writing for all users ( chmod o+w ~ ). 您可以将其添加到用户组(使用usermod -a -G ...然后使用chmod g+w ~ ),也可以允许所有用户写入( chmod o+w ~ )。

But , both of this is a bad idea. 但是 ,这都是一个主意。 Your php script (or anything else running as the apache user) can be broken into and cracked, leaving you home directory open for malicious attackers to modify and rm -rf . 您的php脚本(或以apache用户身份运行的任何其他脚本)可以被破解,从而使您的主目录打开,恶意攻击者可以对其进行修改和rm -rf

In addition, if you're running a RedHat-ish Linux, you will run into SELinux which by defaut prevents apache from accessing user directories at all. 另外,如果您运行的是RedHat-ish Linux, 则将运行SELinux,默认情况下,这阻止apache访问用户目录。 In that case, you also have to set setsebool -P httpd_enable_homedirs on . 在这种情况下,还必须在上设置setsebool -P httpd_enable_homedirs on

Instead , I would recommend that you use a different directory and give your user full access to that. 相反 ,我建议您使用其他目录,并为您的用户提供对该目录的完全访问权限。 Something along the lines of /var/www/testDir with the apache as owner and group, and adding yourself to the apache user group is probably a sane idea. /var/www/testDir ,以apache作为所有者和组,并将您自己添加到apache用户组可能是一个明智的想法。

It looks like a permission issue. 它看起来像是权限问题。 Make sure that Apache has write permission to that directory 确保Apache具有对该目录的写权限

You may have permission issues on the server. 您可能在服务器上有权限问题。 Try to use chmod -R 775 <dirname>(or 777) in your ssh command line. 尝试在ssh命令行中使用chmod -R 775 <dirname>(or 777) You can do this in php code with chmod() too but I don't suggest you because it would run it everytime the php code runs and changing it more times is pointless. 您也可以使用chmod()在php代码中执行此操作,但我不建议您这样做,因为它会在每次php代码运行时都运行它,并且更改它多次都没有意义。 It can output to the screen but I bet the directory the script wants to make file has permission 755. Try to check it. 它可以输出到屏幕,但是我敢打赌,脚本要使文件具有目录755的权限。请尝试对其进行检查。

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

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