i want execute bash on server side with php - php

I want to execute bash script with php.
When i launch php script in browser i got an error " permission denied"
when i run php whoami , i get www-data. I granted all permisions for write and read.
When i run bash script as www-data it works. But via php im still getting the same error.Should i check php.ini settings or somethnig?
If i write unknown bash command in php script, using shell_exec(). I got an error sh: command not found.I think it should be "bash: comand not found".
Can u please help me out.
PHP
<?php
$message=shell_exec ("bash bashscript 2>&1");
print_r($message);
?>
bash
#!bin/bash
cp /myfiles/file aa

Related

PHP script will not run bash command

I am having trouble getting my PHP script to run a bash command/script. Here is an export of my PHP script (it is triggered when a HTML form is submitted):
}
chdir('/usr/local/sim/');
shell_exec('killall -9 sim');
shell_exec(./write-sims.sh');
chdir('/var/www/html/');
shell_exec('php -f test.php');
?>
I know that this PHP script is running okay as the 'killall -9 sim' command is executed as expected along with the 'write-sims.sh' script.
I put my bash command inside a PHP script as a test to see if that would work but it doesn't, 'test.php' is as follows:
<?php
shell_exec("find /usr/local/sim/data -maxdepth 1 -type f -name '*.conf' -exec sh -c '/usr/local/sim/build/src/sim --daemon --config {} &' \;");
?>
When I run test.php manually via cli it runs fine however whenever it is called by the first php script it doesn't run.
I have tried switching things around by putting the bash command in a .sh script and calling that from the first php script and also adding the bash command itself inside the first php script but neither work however I don't get any errors in the nginx error log which seems to suggest the script is running okay without errors.
Any idea why this is not running but runs fine when I do it manually? Thanks!

shell script running in php gives error .sh: Permission denied

I am running a php file through browser which run the shell script but i recive the error ".sh: Permission denied"
My Php File is
<?php
$output = shell_exec('/var/www/html/test.sh 2>&1');
echo "$output";
My Shell Script is
#! /bin/bash
scp -r -i /home/ec2-user/key.pem /var/www/html/test ec2-user#172.16.11.12:/var/www/html/
echo "hello world"
Any help will be appreciated
We found that the script, test.sh, would fail with "Permission denied" when executed by php's shell_exec when the script was located in either /var/www/html/ and /root (which was the id commands being executed by shell_exec). It executed just fine when when we moved the script to /usr/local/bin. The script also worked when executed manually on a console in all 3 directories. This also excludes the mount noexec theory. I don't see any restrictions documented in php documentation for shell_exec, or php.ini. Neither AppArmor or SELinux are enabled on the host. There were no errors in /var/log that mentioned the script. int_get('open_basedir') and strace of the the php process when running the .php file (you can do that on the command line) would be the only other thing that has come up. Any other ideas? I am stumped.

run php file from shell script in openwrt

I am trying to run a php file from shell script file or terminal in open-wrt platform. I have executed php files in crontab and those are running perfectly. i need to run a php file without putting it into crontab.I am trying it with the following command
chmod 777 /www/api/*
cd /www/api
php myphp.php
but it showing -ash: php: not found
I have also try it putting the following command on top of the script
#!/usr/bin/php
but it is not working. i could not figure out the problem!!!
You must install php-cli package, after you can run the script by
php-cli script.php

Why site wont identify itself as CLI if it was run as CLI?

the site Im developing has only this:
echo php_sapi_name();
now from CMD, I run this:
php -d display_errors=1 -r "echo file_get_contents('http://site');"
it returns apache2handler instead of cli. Why?
When you run the following command:
php -d display_errors=1 -r "echo file_get_contents('http://site');"
You're actually loading the file from Apache (using the HTTP protocol). That's why you get apache2handler instead of cli. The PHP script is running under Apache. This is the same result you get when accessing http://site via some browser. In this case, your PHP client is acting as your browser.
If you need to run your script from PHP client, you have to call it this way, from command line:
php file.php
You need to have access to the file from your file system. Using the above command, I'm assuming you are in the same directory as the script is.

exec/system() - script being called works until called from PHP

I have a bash script:
run.sh
#!/bin/sh
cd /var/www/project/bin/
CMD="./executable <full_path_to_file>;
$CMD
When I run this program from the terminal. (i.e. ./run.sh, it works fine)
However when I call it from PHP:
system("full_path_to_sh_file", $out);
It calls the script successfully, and even runs the executable, but now the executable throws an error saying that the supplied file can't be found.
Any ideas?
How do you run PHP script, from webserver or command line?
If from webserver, what user is it run (httpd or apache?)
Make sure the environment as same as when you are running from terminal (for example: same user)
Try this if running as different user:
sudo -u apache /fullpath/run.sh

Categories