tags:

views:

339

answers:

3

Hi all, I am trying to run a Python program using PHP. Here's the code

$command = '/usr/local/bin/python script.py file';
$temp = exec($command, $output);

This works through the command line but not while running it through the browser. I am using Apache so probably it needs the right privileges? I am pretty new to Linux and have no idea how to get this working.

Any help would be appreciated!

Edit 1:

Tried to use proc_open but nothing happens. I gave the full path to the script. Made the script executable but no luck. Any other things I can try on the server? (It's a CentOS 5)

+2  A: 

Few checkpoints

  • script.py should be pass full path, eg, /home/abhinav/script.py
  • script.py should be executable, chmod +x script.py
S.Mark
"script.py should be executable" not if the interpreter is invoked.
LiraNuna
Thanks, and `#!/usr/local/bin/python` would need in first line of script.py
S.Mark
You don't need the shebang if you use the interpreter to call the script (`python <filename>`)
ZeissS
I tried giving the full path to the script. Doesn't work. The browser just shows a blank page and the script execution ends. The same script runs from the command line.
Abhinav
A: 

You need to pass the full path to the script and you also need to make sure that the script is readable by the user running the web server (which means every directory in the path must be +x to the web user).

jdizzle
Did both the things. Not working. :(
Abhinav
Just to cover all the bases, you're actually printing $output, right? You should also try and find out if your web server is running chrooted.
jdizzle
A: 

Realized what was wrong:

  1. The domain was set up as a virtual host and PHP's safe_mode was enabled. proc_open, exec, system, passthru etc. do not work under safe_mode I guess.

  2. Put the script in the directory accessible by the vhost. Apache wasn't able to access the directories outside the vhost document root.

Thanks for the help!

Abhinav