如何在EC2 Linux上执行shell_exec [英] How to execute shell_exec on EC2 Linux

查看:48
本文介绍了如何在EC2 Linux上执行shell_exec的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在EC2 Linux实例上运行API.我尝试从PHP文件执行Python脚本. PHP文件的路径为/var/www/html/droptop/api/event/test.php. Python脚本的路径为/var/www/html/droptop/blacklist/profanity.py. Python脚本接收两个字符串,并通过亵渎性检查检查这两个字符串之一是否包含令人反感的内容.如果未找到令人反感的内容,则返回 0 ,否则返回 1 .但是,shell_exec似乎总是返回NULL. 亵渎检查需要Python 3才能正常工作.

I am running an API on a EC2 Linux instance. I try to to execute a Python script from a PHP file. The path to the PHP file is /var/www/html/droptop/api/event/test.php. The path to the Python script is /var/www/html/droptop/blacklist/profanity.py. The Python script recieves two strings and checks whether one of these two strings contain objectionable content via Profanity Check. Then it returns 0 if no objectionable content was found, otherwise it returns 1. However, shell_execalways seems to return NULL. Profanity Check needs Python 3 to work properly.

在命令行上执行时,PHP文件和Python脚本可以完美地协同工作.

When executed on command line, the PHP file and Python script perfectly work fine together.

我认为它与用户的权限有关. apache而不是www-data用户正在执行文件.但是,我还根据此处更改了sudoers文件.我包括以下几行:

I assume it has something to do with the user's permissions. apache and not www-data user is executing the files. However, I also changed the sudoers file according to here. I included following lines:

apache ALL=NOPASSWD:/var/www/html/droptop/blacklist/profanity.py
apache ALL=NOPASSWD:/usr/bin/python3.6

我还检查了apache是否属于groups.

我还尝试了其他功能,例如exec()system()passthru().

I also tried other functions like exec(), system() or passthru().

仍然总是返回NULL.

test.php

$command = escapeshellcmd('python3 /var/www/html/droptop/blacklist/profanity.py Some BadWord');
$output = shell_exec($command);

if($output == 0) {
  echo json_encode(array("message" => "No Badword found."));
}

profanity.py

#!/usr/bin/python36

from profanity_check import predict, predict_prob
import sys

# get title
title = sys.argv[1]
pred_title = predict([title])
pred_details = 0

if(len(sys.argv) == 3):
    details = sys.argv[2]
    pred_details = predict([details])

if((pred_title == 0) and (pred_details == 0)):
    print(0)
else:
    print(1)

这里可能是什么问题?

推荐答案

您应该删除这些sudoers条目,在您的情况下这些条目不是必需的,并且存在安全隐患.

You should remove these sudoers entries, they shouldn't be necessary in your case and it's a security risk.

我还建议不要中继输出,而应使用退出状态:

I would also recommend to not relay on the output and use the exit status instead:

test.php

<?php
$command = escapeshellcmd('python3 /var/www/html/droptop/blacklist/profanity.py Some BadWord');
exec($command, $output, $return_var);

//echo $output;

if(!$return_var) {
  echo json_encode(array("message" => "No Badword found."));
}
?>

profanity.py

import sys

from profanity_check import predict


def profanity(words):
    return predict(words)

def main():
    words = sys.argv[1:]
    ret = profanity(words)
    sys.exit(any(ret))

if __name__ == '__main__':
    main()

现在,回到您的原始问题,文档

Now, back to your original issue, the docs says

此函数可以在发生错误或程序时都返回NULL 不产生任何输出.

This function can return NULL both when an error occurs or the program produces no output.

您可以取消注释//echo $output;行,并假设您正在使用以apache用户身份运行的Apache Web服务器,则可以尝试运行以下命令,希望这会为您提供一些线索:

You can un-comment the //echo $output; line and assuming you're using Apache web server running as apache user you can try to run the command below, hopefully this will give you some clue:

$ runuser -l apache -s /bin/bash -c "php -f /var/www/html/droptop/api/event/test.php; echo"
{"message":"No Badword found."}

可能的问题:

  • 对于apache用户,您的python安装不在$PATH

  • your python installation is not in the $PATH for apache user

profanity_check模块未全局安装

这篇关于如何在EC2 Linux上执行shell_exec的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆