简体   繁体   English

从php执行Shell脚本,但是sh脚本中的命令无法运行

[英]Shell script executed from php, but commands in sh script wont run

I'm making a PHP page with the purpose of creating and activating Apache VirtualHost files. 我正在制作一个PHP页面,目的是创建和激活Apache VirtualHost文件。

The pages generates the files and places it in /etc/apache2/sites-available/. 这些页面生成文件并将其放置在/ etc / apache2 / sites-available /中。 After that a shell script is called by with: 之后,通过以下命令调用shell脚本:

shell_exec("/bin/sh /usr/local/bin/myscript.sh"); shell_exec(“ / bin / sh /usr/local/bin/myscript.sh”);

myscript.sh: myscript.sh:

#!/bin/sh
file=$(ls -1t /etc/apache2/sites-available/ | head -1)
a2ensite "$file" 2>&1 >/dev/null
service apache2 reload 2>&1 >/dev/null
sleep 5

The script seems to be executed (the sleep time corresponds to the amount of time it takes to run and if I don't use 2>&1 >/dev/null I get the output from a2ensite). 该脚本似乎已被执行(睡眠时间对应于运行所花费的时间,如果我不使用2>&1> / dev / null,我将从a2ensite获取输出)。 But the site is never enabled. 但是该站点从未启用。

It works fine if I run the script from terminal, so I'm guessing it's some sort of permission issue. 如果我从终端运行脚本,效果很好,所以我猜想这是某种权限问题。 I've been playing around with sudoers and file permissions for two days now, but always with the same results. 我已经玩了两天的sudoers和文件权限,但是总是得到相同的结果。

Been adding stuff like 正在添加类似

www-data ALL=NOPASSWD: /usr/local/bin/myscript.sh www-data ALL = NOPASSWD:/usr/local/bin/myscript.sh

and chmod 777 for testing purposes, but nothing. 和chmod 777用于测试目的,但一无所获。

Is there any definite way to do this? 有什么确定的方法吗? I'm running Ubuntu 16.04 and PHP7. 我正在运行Ubuntu 16.04和PHP7。

I think its because www-data don't have the right to execute the service and a2ensite commands. 我认为这是因为www-data没有执行服务和a2ensite命令的权利。

Try this : 尝试这个 :

#!/bin/sh
file=$(ls -1t /etc/apache2/sites-available/ | head -1)
sudo a2ensite "$file" 2>&1 >/dev/null
sudo service apache2 reload 2>&1 >/dev/null
sleep 5

And then, edit the sudo file with sudo visudo and add 然后,使用sudo visudo编辑sudo文件并添加

www-data  ALL=NOPASSWD : /usr/sbin/service, /usr/sbin/a2ensite

I think you need a dot in between: 我认为您需要在两者之间加一个点:

shell_exec('/bin/sh' . '/usr/local/bin/myscript.sh'); shell_exec('/ bin / sh'。'/usr/local/bin/myscript.sh');

Also, I am using single quotes... as above. 另外,我使用单引号...如上所述。

or you can try: 或者您可以尝试:

shell_exec('/usr/local/bin/myscript.sh'); shell_exec('/ usr / local / bin / myscript.sh');

This is solved. 解决了。 The problem was not sudoers or file permissions. 问题不在于sudoers或文件权限。 The commands were not executed correctly because Apache module mpm-itk was activated. 由于激活了Apache模块mpm-itk,命令未正确执行。 Worked perfectly after I deactivated it. 我将其停用后,效果很好。

I didn't need mpm-itk, but if anyone with similar problems needs it activated you could try this: https://askubuntu.com/questions/491624/setresuid-operation-not-permitted-when-calling-via-php 我不需要mpm-itk,但是如果有人遇到类似问题需要将其激活,则可以尝试以下操作: https : //askubuntu.com/questions/491624/setresuid-operation-not-permitted-when-calling-via-php

(Thanks Myran) (感谢迈兰)

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

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