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.
Related
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!
I am using a php script on my apache/ubuntu server to call a bash script that triggers an application taking a python script as an argument (IDAPro).
PHP Code
chdir('/var/www/dashboard/team/static/sql');
$output = exec('sudo -u rohan ./start.sh');
Now, the above code works fine if I run the PHP file from the terminal - but only if I run it as the root user. Needless to say, if I execute the bash file directly it runs too.
But when I run the PHP file on the browser, it doesn't work and I get the following error in the apache error log:
QXcbConnection: Could not connect to display
Aborted
I understand that Apache/php runs as 'www-data' user (used the 'whoami' to verify), and that is why I have the sudo in my exec. I have tweaked and tinkered the permissions for both users to no avail. When I run the php file from the terminal as the 'www-data' user, it throws no error but does not do anything except display the random echo tags I at the start and end of the script to debug it.
I am a linux novice, so any help is greatly appreciated.
Okay, I finally managed to solve it.
The issue is not with the permissions, but it is with the environment variables.
I had to include the following line in my bash script
export DISPLAY=':0.0'
Note that setting the variable in the terminal and running the script does not work. The line needs to be inside the script.
I assume this is because the DISPLAY variable is not set if you run the script as any user other than root, which is what happens in case of Apache/PHP where the script is executed as the 'www-data' user.
perhaps you could use something like the following at the top of your script:
if [ "$(id -un)" != "rohan" ]; then
exec sudo -u rohan $0 "$#"
fi
export XAUTHORITY=/home/rohan/.Xauthority
export DISPLAY=:0
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.
In my php code:
exec(test.sh);
and test.sh has code:
echo "Hi this is test?" | espeak --stdout > demo.wav
But nothing happen. No error, No output.
If i try to execute test.sh from terminal that it will work perfectly. So why it not run on my php.
Can someone help me?
How do you run your PHP script? With php-cli (in shell mode)? HTTP (Apache, ...)?
It could be a path problem. Could you give us the path of your test.sh, the path of your php script or the URI which is called to run the PHP if you use apache or other HTTP server?
the apache user will need write access to the current working directory (presumably the same directory that contains your php script and test.sh).
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.
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.
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.
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
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