1

I have a PHP file on my web server that is supposed to call a python script (same folder) that writes out to a file and then get its contents and displays the data.

The PHP file is called q.php

it contains

<?php
   $tmp = exec("trivia.py");
   sleep(4);
   $homepage = file_get_contents('./testfile.txt', true);
   echo $homepage + '<p>exec ret:' + $tmp;
   echo exec("whoami");
?>

This file calls a trivia.py which writes out a file ("./testfile.txt") and then php gets the data from the file and displays it. I added a variable to see if the exec is working and it returns 0. The PHP server is being executed by user http.

now for trivia.py, I have the following line at the top of the file

#!/usr/bin/env python

and it executes perfectly fine when I SSH into the server. From SSH I run the script and it creates the file specified above and the web page works fine. However, if I used the PHP file to create it, it will not work from the web.

I am pretty sure this has something to do with permissions somewhere but I am not that great on permissions for Linux.

SYSTEM INFO: Synology Diskstation, DSM5, PHP5, Python 2.7

EDIT:

trivia.py currenlty has 777 as permissions with admin owner and group users

ObieMD5
  • 2,684
  • 1
  • 16
  • 26
  • Do you know how to set permissions for all the relevant files? See https://www.linux.com/learn/tutorials/309527-understanding-linux-file-permissions for a decent intro. – Hektor Dec 28 '14 at 21:07
  • Have you made your trivia.py file executable? Does the webserver have access and execute rights on python? on trivia.py? Can you see (and maybe share here?) what linux error logs tell you? – fpierrat Dec 28 '14 at 21:09
  • @user2356315 the permissions right now are set to 777 just to try to get them to work. – ObieMD5 Dec 28 '14 at 21:12
  • @fpierrat It is executable. I do not know how to see if the webserver has access and execute rights to python. How do I do that? Also I checked dmesg and nothing was there and the only thing in the php logs is there error of the testfile.txt not existing because the python code is not executing and creating it. – ObieMD5 Dec 28 '14 at 21:18
  • `ls /var/log` in ssh to see if other logfiles would be more appropriate to check (/var/log/messages? /var/log/httpd? I'm no linux logs specialist but you can google to find out what's logged where). Besides, it would be interesting not just to get the return value of exec() but also what's printed on stdout and stderr, you might have a look here: http://stackoverflow.com/questions/2320608/php-stderr-after-exec – fpierrat Dec 28 '14 at 21:28

2 Answers2

0

When running a file via exec you need to provide the full path to the file, it doesn't matter if the file is in the same directory.

Please try it with (assuming the file is in /var/www/public)

exec('/var/www/public/trivia.py');
baao
  • 71,625
  • 17
  • 143
  • 203
-1

For Windows User - Problem was resolved here PHP exec python

I suggest to replace $tmp = exec("trivia.py"); for this one

$tmp = exec("C:\\Python27\\python.exe trivia.py");

// Add your python.exe route before the file

Hope it works!!

  • You resurrected a 3 year old question to put an example that did not even pertain to the question. The system clearly has links from the bash example and the OS is Synology. – ObieMD5 Jul 13 '17 at 11:41