0

This is really simple but I cannot get it to work at all. Spent many hours and I've always give up. I created php script called copy.php and it should call a python script called copy.py.

I want to execute a command line like this

 <?php exec('/var/www/html/copy.py'); ?>

Really simple.

Why cannot I get the python script executed from php exec()? The function inside python script is to get a copy of error_log from a different directory (outside of Apache) into html directory.

If I run that from a terminal

 > php copy.php

It did execute the function and made a copy. Why is that the web browser isn't doing it?

Let me simplify this:

why cannot exec("cp /var/log/httpd/error_log /var/www/html/path/to/php/script") work?

it works fine if I type it in terminal but not when run in a browser.

netrox
  • 5,224
  • 14
  • 46
  • 60
  • 5
    user permission issues –  Feb 22 '13 at 00:08
  • what Dagon is hinting at is when you run the page on the command line it is being run under your username. So the permissions php has are the same as you. If you can run the script, php can. When called from apache, php runs under the apache user (which is usually "apache"). the user apache has to have execute permissions for exec to run the python script and most likely permission to access whatever the python script is trying to access. Also, I don't know what the python script does, but if it is just copy, php has a copy function. – Jonathan Kuhn Feb 22 '13 at 00:23
  • I echo'd "whoami" and it said Apache but what doesn't make sense though is that I used exec("python /usr/diskpurge/script.py") to execute a python script outside of Apache directory without problems with one of my php scripts that also echo'd "Apache" as whoami. And there's no php errors or anything. – netrox Feb 22 '13 at 17:00

4 Answers4

1

As others have alluded to, the difference is probably permissions. When you run a command from the command line, you're generally not the same users as your apache script is running as.

Put another way, if from the command line you type whoami, you'll probably get whatever name your user account is.

The echo exec('whoami'); from within php shows who the script is running as, which is Apache.

So, whatever command you're trying to run from your web server isn't available to run as the Apache user. You mentioned you've been able to have exec("python /usr/diskpurge/script.py") work, but not to have exec('/var/www/html/copy.py') doesn't. This is due to in one instance you're running python, in the other you're trying to execute your copy.py script. If copy.py doesn't have execute permissions for the Apache user, you're not going to be able to run it from the browser.

ernie
  • 6,356
  • 23
  • 28
0

Perhaps different settings apply for the Apache environment versus the command line.

Use error_reporting(E_ALL); and ini_set('display_errors', true) to see what errosr may come up.

It is possible that the Apache environment is prohibited from using exec or the fact that Apache runs under a different user that does not have execute rights on the python script.

Mihai Stancu
  • 15,848
  • 2
  • 33
  • 51
  • no errors whatsoever. one of my php scripts execute a python script outside of apache without problems even though its runnung as Apache – netrox Feb 22 '13 at 17:00
0

sounds like a permission error. Check if your server is running with sufficient rights.

echo exec('whoami');

Set your error reporting to report all:

ini_set('display_errors', true);
error_reporting(E_ALL);

and check for errors..

If your whoami returns a user which is not a member of the SU family (linux) or administration (windows) then resite your permissions..

Linux: Assign the user returned by whoami correct permissions to run python scripts.. Do not allow the resulted username to run as root with total administration powers.. This is a big no no

Daryl Gill
  • 5,464
  • 9
  • 36
  • 69
0

The only reason its not working is because you didn't set the write permissions!

Do:

sudo nano /etc/sudoers 

And then put the following:

www-data ALL=(root) NOPASSWD:ALL
jherran
  • 3,337
  • 8
  • 37
  • 54