简体   繁体   English

在 Amazon EC2 上通过 php 的 Python 脚本

[英]Python script through php on Amazon EC2

I am trying to run a simple python script through php on Amazon EC2 instance.我正在尝试通过 php 在 Amazon EC2 实例上运行一个简单的 python 脚本。

The file is working fine on terminal ie am getting the output.该文件在终端上运行良好,即正在获取输出。 But when am trying the same in browser, am getting a blank page.但是当我在浏览器中尝试相同时,我得到一个空白页面。

I tried few things, such as adding www-data ALL=(ALL) ALL in sudoers file in etc folder.我尝试了几件事,例如在 etc 文件夹的 sudoers 文件中添加www-data ALL=(ALL) ALL etc/sudoers . etc/sudoers

I also tried disabling SELINIX .我也尝试禁用SELINIX

I checked with the php's safe mode and few other things which I found over here(SO).我检查了 php 的安全模式和我在这里找到的其他一些东西(SO)。

I have followed this AWS TUTORIAL .我遵循了这个AWS 教程 I did everything exactly what's written in there.我所做的一切都与那里写的完全一样。 I believe there is some permission issue.我相信存在一些许可问题。

Can anyone please check it out.任何人都可以请检查一下。 Am stuck with this for 2 days..我坚持了2天..

EDIT Python script编辑Python 脚本

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

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

Php file: 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");
?>

After a lot of "scratching my head", I finally figured it out.经过多次“挠头”,我终于想通了。

First of all you one need to figure out the current user who is executing the php.首先,您需要找出正在执行 php.ini 文件的当前用户。 One can either check out php.info file or use可以查看 php.info 文件或使用

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

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).在我的情况下,它是apache而不是www-data (我不应该首先假设)。

After that you will need to edit the sudoers file (etc/sudoers)之后,您将需要编辑sudoers文件(etc/sudoers)

Add the lines over there.在那里添加线条。

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

or you can simply add或者你可以简单地添加

apache ALL=(ALL)        NOPASSWD:ALL

(You probably should just specify the path). (您可能应该只指定路径)。

Then execute the script through php.然后通过php执行脚本。

I guess first place one should look is the apache error log at.我想第一个应该看的是 apache 错误日志。 You don't want to enable sudo.希望启用sudo的。 Please consider checking the error log of apache to understand what is the underlying problem at:请考虑检查 apache 的错误日志以了解潜在问题是什么:

/var/log/apache or similar directory /var/log/apache或类似目录

Please review these posts for a better solution and review of the problem:请查看这些帖子以获得更好的解决方案并查看问题:

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

https://forums.aws.amazon.com/thread.jspa?messageID=692394 https://forums.aws.amazon.com/thread.jspa?messageID=692394

php shell_exec() command is not working php shell_exec() 命令不起作用

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

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