2

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.

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

I also tried disabling SELINIX.

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

I have followed this AWS TUTORIAL. 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..

EDIT Python script

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

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

Php file:

<?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");
?>
driftking9987
  • 1,673
  • 1
  • 32
  • 63
  • 1
    without the script or access to your server it is impossible to guess what's going wrong. When you print a simple HelloWorld HTML page in your script, does it work? – Maximilian Peters May 01 '16 at 14:30
  • Added the scripts. Maybe that can help. When am executing in terminal with `php try.php` it's printing "Hello world". But in browser, it's empty. – driftking9987 May 01 '16 at 14:37
  • did you check the log files and which user owns the python scripts? – Maximilian Peters May 01 '16 at 14:54
  • Why on earth do you want to run a python (or any other script) from a web page? That's suicidal. Better to use a task queue. And anyway are you quite sure this is something that the php page can't do on it's own? – e4c5 May 01 '16 at 14:55
  • @Ashafix, root is the owner of the python script. #e4c5, i need to execute a python script for my android app. I only found this way which worked for me. – driftking9987 May 01 '16 at 14:58
  • check phpinfo to find out what user is running the server, most likely it is www-data. Change the ownership to www-data and see if it works. – Maximilian Peters May 01 '16 at 15:13

2 Answers2

2

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. One can either check out php.info file or use

$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).

After that you will need to edit the sudoers file (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.

driftking9987
  • 1,673
  • 1
  • 32
  • 63
  • For some reason it is not working, `escapeshellcmd` is not executing/outputting at all. – Volatil3 Apr 11 '18 at 11:45
  • This is the wrong answer don't do it if you like the next two hours. https://stackoverflow.com/a/56110051/1984636 – sivi Oct 16 '19 at 18:33
0

I guess first place one should look is the apache error log at. You don't want to enable sudo. Please consider checking the error log of apache to understand what is the underlying problem at:

/var/log/apache or similar directory

Please review these posts for a better solution and review of the problem:

How to execute shell_exec on EC2 Linux

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

php shell_exec() command is not working

sivi
  • 10,654
  • 2
  • 52
  • 51