概述
I need to start php process from shell on remove server with some arguments, so i thought that it should be a nice idea to make REST API, that executes some function when user performs GET request.
我需要从删除服务器上的shell启动php进程和一些参数,所以我认为创建REST API应该是个好主意,当用户执行GET请求时执行某些功能。
I wrote a simple bash script for testing and figured out that command-line argument is not being specified, when calling this script from website:
我编写了一个简单的bash脚本进行测试,并在从网站调用此脚本时发现没有指定命令行参数:
shell_exec('/var/www/test.sh 123')
Bash script source:
Bash脚本源:
#!/bin/sh
echo $1;
When calling this bash script from root (or other existing user) it correctly shows argument it has received. When i call this script from website (that is running under user www-data under apache2), it returns nothing:
从root(或其他现有用户)调用此bash脚本时,它会正确显示已收到的参数。当我从网站调用此脚本(在apache2下的用户www-data下运行)时,它不返回任何内容:
Also, if i execute this bash script in my console under www-data user, it also returns nothing:
另外,如果我在我的控制台中的www-data用户下执行这个bash脚本,它也不会返回任何内容:
su -c '/var/www/test.sh 123' www-data
Also i've tried to start process from different user from php (is supposed that this will not work for security reasons, but just in case):
此外,我已经尝试从不同的用户从PHP开始进程(假设这不会出于安全原因,但以防万一):
$result = system("su -c '/var/www/test.sh 123' sexyuser", $data);
// var_dump($result): string(0) ""
// var_dump($data): int(1)
So, what privileges should i give to www-data user to run process under php?
那么,我应该赋予www-data用户在php下运行进程的权限?
1 个解决方案
#1
1
You should let php run the script and handle the results
你应该让php运行脚本并处理结果
检查exec上的php.net,例如http://www.php.net/manual/en/function.exec.php
//called by example.com/myshell.php?day=today&k=y&whatever=youwant
$arguments = implode(" ", $_GET);
$lastline_of_exec_result = exec ( "/your/command.sh ".$arguments); //sh called with today y youwant
echo $lastline_of_exec;
Where $arguments are the stringified list of ALL information your script got from GET arguments
其中$ arguments是脚本从GET参数获取的所有信息的字符串化列表
if you want a ore precise in and output, try this:
如果你想要精确的输出和输出,试试这个:
//called by example.com/myshell.php?day=today&k=y&whatever=youwant
$argument = $_GET['whatever'];
$output = array();
$last_line = exec("your/command.sh ".$argument, &$output); //sh called with youwant
foreach($output as $line)
echo $line."
".PHP_EOL;
or of course (with shell_exec)
当然(使用shell_exec)
$argument = $_GET['whatever'];
$output = shell_exec("your/command.sh ".$argument);
echo "
".$output."";
make sure (shell_)exec is not listed under disable_functions in your php.ini
确保(shell_)exec未列在php.ini中的disable_functions下
最后
以上就是帅气胡萝卜为你收集整理的apche php 进程启动,在www-data(apache2,php)下启动shell进程的全部内容,希望文章能够帮你解决apche php 进程启动,在www-data(apache2,php)下启动shell进程所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复