通过Amazon EC2上的php编写Python脚本 [英] Python script through php on Amazon EC2

查看:101
本文介绍了通过Amazon EC2上的php编写Python脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过Amazon EC2实例上的php运行一个简单的python脚本。

I am trying to run a simple python script through php on Amazon EC2 instance.

该文件在终端上运行良好,即正在获取输出。但是,当在浏览器中尝试同样的操作时,却得到了一个空白页。

The file is working fine on terminal i.e am getting the output. But when am trying the same in browser, am getting a blank page.

我尝试了一些操作,例如添加 www-data ALL =( ALL)ALL 在etc文件夹中的sudoers文件中。 etc / sudoers

I tried few things, such as adding www-data ALL=(ALL) ALL in sudoers file in etc folder. etc/sudoers.

我还尝试禁用 SELINIX

我检查了php的安全模式以及在此处发现的其他一些东西(SO)。

I checked with the php's safe mode and few other things which I found over here(SO).

我已遵循 AWS教程。我所做的一切都完全按照那里写的去做。我相信存在一些权限问题。

I have followed this AWS TUTORIAL. I did everything exactly what's written in there. I believe there is some permission issue.

任何人都可以查看一下。坚持了2天。

Can anyone please check it out. Am stuck with this for 2 days..

编辑
Python脚本

EDIT Python script

#!/usr/bin/env python
# -*- coding: UTF-8 -*-

# enable debugging
import cgitb
print "Hello  world"
cgitb.enable()

Php文件:

<?php header("Content-Type:text/plain");
$output = escapeshellcmd('/home/ec2-user/anaconda/envs/xyz/bin/python2.7 /var/www/html/xyz/a.py');
$a=shell_exec($output);
echo ($a);
//echo file_get_contents("/var/www/html/xyz/a.py");
?>


推荐答案

经过很多 s头之后,我

After a lot of "scratching my head", I finally figured it out.

首先,您需要弄清楚正在执行php的当前用户。既可以签出php.info文件,也可以使用

First of all you one need to figure out the current user who is executing the php. One can either check out php.info file or use

$processUser = posix_getpwuid(posix_geteuid());
print $processUser['name'];

这将为您提供执行代码的用户。就我而言,它是 apache 而不是 www-data (我本来不应该这样假设)。

This will give you the user who is executing the code. In my case it was apache rather than www-data (I shouldn't have assumed so in first place).

之后,您将需要编辑 sudoers 文件(etc / sudoers)

After that you will need to edit the sudoers file (etc/sudoers)

在那儿添加行。

apache ALL=NOPASSWD:/var/www/myfile.py
apache ALL=NOPASSWD:/usr/bin/pythondestination

或者您可以简单地添加

apache ALL=(ALL)        NOPASSWD:ALL

(您可能应该只指定路径)。

(You probably should just specify the path).

然后通过php执行脚本。

Then execute the script through php.

这篇关于通过Amazon EC2上的php编写Python脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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