Run Python Script from PHP on IIS Server - php

I've got IIS 8.5 installed, with PHP installed and running. I've also added a handler mapping (with the %s %s at the end of the python.exe path) to the website. I can run the python script directly from the browser successfully (prints "Hello World").
This is all just configured on the Default Web Site for testing purposes. When I attempt to set a PHP variable to the output of a python script, the variable does not appear to receive the value. IUSR and IIS_IUSRS have modify permissions on the root directory (C:\inetpub\wwwroot), which is where all HTML, PHP and Python files are located.
My code is as follows:
PHP:
<?php
$coutput = shell_exec("/test.py > ~debug.log 2>&1");
include 'header.html';
echo "</br>Python Output:</br>" . $coutput;
var_dump($coutput);
include 'footer.html';
?>
NOTE: I've also tried "python test.py > ~debug.log 2>&1" in the shell_exec function to no avail.
Python (blank print is to display properly in browser when run directly):
print("")
print("Hello World")
Initially, I thought this was a permissions issue with PHP, so I ran the python script from PHP via command line and get the python code:
c:\inetpub\wwwroot>"c:\program files\php\v7.3\php.exe" c:\inetpub\wwwroot\test.py
print("")
print("Hello World")
PHP is not in safe mode.
What am I missing? Any help would be appreciated!

Related

Running PHP Script In Powershell keeps opening NotePad ++ rather than executing the PHP script

Okay, I'm at a bit of a loss here.
I'm testing out running PHP scripts from within powershell and it just keeps opening NotePad ++ rather than executing the script. I cannot figure out why this won't work...
I'm using a pretty basic PHP script to test:
<?php
echo 'Hello, World!';
?>
And I'm calling it using the standard way I run .ps1 files:
PS C:\php> c:\phpfiles\test25.php
The execution policy is set to unrestricted... what am I doing wrong?
You should pass the path of the file as an argument to the PHP executable. If (lets say) PHP is installed in c:\php, then you must do:
PS c:\php\php.exe -f c:\phpfiles\test25.php

Bash script runs from php command line but not from page

I have a bash script that works as expected from both the shell and the PHP command line, but not when called from a PHP page in Apache (Raspbian). I.e, this works (PHP command line):
>php exec('/var/www/html/scripts/myBashScript.sh');
But this doesn't (index.php):
<?php
exec('/var/www/html/scripts/myBashScript.sh');
?>
No error messages are displayed and I can't see anything relevant in the Apache server logs. As suggested by other responses, I've also tried:
exec('sh /var/www/html/scripts/myBashScript.sh')
exec('./scripts/myBashScript.sh')
exec('sh ./scripts/myBashScript.sh')
Both script file and its containing folder have rwx permissions for the Apache user (www-data). The script is set to executable. Built-in bash commands work as expected from the php file, i.e. this works:
<?php
echo exec('whoami');
?>
What am I missing?
The problem turned out to be related to non-Bash commands in the scripts rather than to the script itself. See comments above. Thank you #AlexanderO'Mara.

executing a script via php exec which creates a file on the server

I have a php script which calls a shell script as below -
#!/bin/bash
timestamp=$(date +"%d-%m-%Y %H:%M:%S")
echo $timestamp >> results
The php script -
<?php
$mycmd = exec('/bin/bash exectest.sh',$op,$er);
var_dump($mycmd);
var_dump($op);
echo $er."\n";
?>
The php script returns error code 1 for $er but when i tried to modify the shell script to just print instead of writing to a file. the Php script then returns 0 and succeeds.
Any ideas of where I need to fix this?
I have tried giving the full path for the script and also this is the same case when i tried using a python script in place of a shell script.
Your observation indicates that this is most likely a permission problem, e.g. the user running PHP does not have write permission to either the results file in its current working directory or the directory itself (if the file does not exist yet).
It happened to be running on an AFS machine hence required afs priviledges for httpd.
fs sa . http write
sorted the issue.

Executing a python file from PHP - Linux

I have a python file that I would want to execute whenever a php page is called. The python file is in the same folder as the php file. The python script on execution edits a textfile that the php file accesses.
I have the following code:
<?php
exec("python somefile.py",$output);
$file = fopen("test.txt",'r');
....
For some reason, the python script never gets executed. I know this certainly as I can see it from the changes in the text file.
Since I was not sure if the script was made executable, so I included "python" on the command.
I also ran:
chmod +x somefile.py
just to make sure this was not the reason. But this did not help too.
What should I change to make the php page execute the python script whenever it is called?
This is most likely a permission issue.
Try
echo exec("whoami");
This will let you know who php is running as. Then you need to verify this user can run the python script. If the file was not created by the same daemon that runs python, you will most likely be denied permission.
Update
This will let you know who owns all the files you are working with. The file being written to needs to be writable by the user that is running python. If you are running python from ssh, that is most likely not the same user as when you run python from exec.
echo exec('whoami') . "<br>";
echo exec("ls -l test.txt") . "<br>";
echo exec("ls -l somefile.py") . "<br>";
Update 2
Because I constantly forget this exists.
passthru('python somefile.py 1 2>&1');
This will exec your python file, and output stderr to stdout.

Why doesn't PHP exec() command doesn't run?

I have a very simple PHP script to try to use the exec command. The code is
<?php
// outputs the username that owns the running php/httpd process
// (on a system with the "whoami" executable in the path)
echo("test<br>");
echo exec('whoami');
echo("test");
?>
However when I access the page, it just prints out:
test
test
It does not print the output from "whoami" command, which when run from command line displays
me
What is the problem here? I cannot work it out.
Thanks a lot!
(on a system with the "whoami" executable in the path)
PATH, like all environment variables, is not system-wide but ultimately per-process.
That is, though your typical bash session may well include the path to this executable in PATH, the environment in which your PHP script is running (say, an Apache CGI context) does not. This may be for any reason.
Provide an absolute path to whoami, instead.

Categories