PHP shell_exec with parameters is not working - php

I have a problem with the shell_exec option in my server. I am running CentOS 5.7 with CPanel and PHP installed on it.
In our coding we are passing parameters to a php file using shell_exec. Our sample code is like below. We are calling this from a phpfile (background process)
vi test.php
<?php
shell_exec("php -f /home/nikesh/public_html/createtestfile.php 666 >/dev/null &");
?>
where 666 is the parameter which is passing to the php file..
This is working fine in our test server and in live server its not working.
Also when I am executing this using linux terminal it working fine and output is generating.
ie : php -f test.php - is working fine.
But while I try to run this same file throught the browser its not working..
http://example.com/test.php
There is no error messges in my log files and the permission which i set is correct.
Please let me know If anybody faced the same problem before. please help me to fix this issue.
Thanks,
Nikesh
I have given the full permission for the folder and its not working. Safe mode is disabled in our server.
The basic Shell_exec functions are working in my server. But when we are giving parameters to the the php file its not working through the browser. But from the Linux terminal its working and generating the output.
Its not related to the folder permissions, the folder is having the write permission. But some how its not working through the browser. Please let me know if anything need to enable for this..? or need to change any server/php settings ?

Your webserver's php.ini likely has safe mode enabled or this function in the disabled_functions ini setting.
run
<?php
phpinfo();
?>
From a page through your webserver to inspect these variables (assuming that's not disabled)

give 777 permission to your public_html folder with chmod -R 777
try like that
<?php
shell_exec("php -f /home/nikesh/public_html/createtestfile.php 666 >/dev/null 2>&1");
?>

Related

How to give PHP exec() read/write privileges

I can't get exec() to read or write how I need it to:
exec("php -f /root/script/screenshot.php")
Fails with Could not open input file. Must be a permissions problem. Changing the file owner to the php user didn't fix it though.
Can't get this to work either:
exec("xvfb-run -a cutycapt --min-width=1920 --min-height=1920 --url='{$url}' --out='{$path}'");
Fails but doesn't give any error message. cutycapt is a screenshot app to take a screenshot of a website.
Both commands work perfectly from command-line. But it seems that php exec() will neither read nor write files...
watch if your folder path is in the sudoers file
Figured out that it was indeed a permissions error. The second command wasn't working because the folder didn't have write permissions to the apache user.
exec("xvfb-run -a cutycapt --min-width=1920 --min-height=1920 --url='{$url}' --out='{$path}'");
Changed the permissions, and now the above command is saving screenshots no problem at all.

php functions file_exits, copy, shell_exec not working on one machine but same code working on different machine

I am a Centos 7 machine on which I have configured PHP with Apache and Slim Framework.
The simple code to check if the file exists and copy the code into a directory is working fine on one of the machine but not working on another machine. Its really weird.
Actually I am accessing the API through Apache to trigger this function.
Functions like shell_exec alaways return 0
copy function is also not workingg
file_exists also not working.
Any help or clue why this is wrong or debug this issue.
Thanks all for the help.
Problem was file permission issue.
The reason was all of the above function I was performing was on the file. Since the files has different permission for different user.
So If I hit the URL from browser it was through "Apache" user which does not have execute permission on the folder to access the file and read permission on the file.
In short
sudo chmod +rx file
sudo chmod +x "All the directory structure"

PHP script fails when called from a browser but not from CLI

This is driving me nuts.
I've reduced my problem to this: I have a simple file 'test.php' that invokes a php file (this is from a bigger application, so in fact we need to execute many php scripts from a main script):
<?php
exec("php /var/www/setActive.php 273 1 2>&1",$arO,$nO);
echo $nO.'|'.implode(',',$arO);
The original path for setActive.php is way longer, I shortened it for this example.
Now, if I try:
[root#stg]# sudo -u apache php test.php
I get:
0|
Great, this means the setActive.php script ran smoothly.
Now, if I try it from a browser or from curl:
[root#stg]#curl http://my.url/path/test.php
This is what I get:
1|Could not open input file: /var/www/setActive.php
I already reviewed all the permissions. Apache user has access to all directories and files.
This was working ok in another server. I reviewed the php.ini files and there is no difference between them.
Maybe is a option that I'm not seeing, or an apache config option. I'm ran out of ideas.
Please help me!
Thanks in advance.
I have Linux, Cent OS. PHP 5.3.23
If permissions etc.. are all ok, possibly the apache config. in your setup has the 'ChrootDir' set:
http://httpd.apache.org/docs/2.2/mod/mpm_common.html#chrootdir
This uses the syscall chroot() and the view for '/' dir for that process starts from this setup dir, and hence everything should be relative to it.

php shell_exec() command is not working

I am trying to run a .sh file from php.
I tried doing it with shell_exec(). but its not working
I refered many questions related to this in stack overflow but could not solve
my php code is(web.php)
<?php
echo shell_exec('/var/www/project/xxe.sh');
echo "done";
?>
only done is printed. but it is working from terminal(php /var/www/project/web.php)
In xxe.sh I am calling a python file
python vin.py
I have also changed the file permission to 777 for both .sh n .py files
please help
If you say it works on the terminal and not on apache then apache's php.ini file may be disabling the use of shell_exec().
See http://www.php.net/manual/en/ini.core.php#ini.disable-functions
Your apache's php.ini file may look something like
disable_functions=exec,passthru,shell_exec,system,proc_open,popen
Remove shell_exec from this list and restart the web server, although this is a security risk and I don't recommend it.
shell_exec might not know what directory to look in for your executable's location directory. What solved it for me was this before the shell_exec:
putenv('PATH=/usr/local/bin');
Then the terminal can find the executable. Also check permissions on every part of the command to make sure apache user has read and execute permissions.
If it works well in shell, I think apache is chrooted. So php can't find /var/...
Or user of httpd user does not have permission to enter /var/...
If you are good at PHP. Open dir /var/... And readdir() and check dir exists and check file exists.
This question might help you. scanning /home/ with opendir()
The problem is usually that when you exec code from within php it is run as the webservers user www-data in alot of linux distros. Normaly this user does not have an enviroment set up, and because of that no PATH. By using full paths in your files you can usually overcome this.
xxe.sh
/usr/bin/python /path/to/script/vin.py
While trying to run a script triggered by github post-receive webhook.
Here is where my project directory is located(cloned git repo):
/var/www/html/my-repo
I create a script inside the above directory called webhook.php:
<?php
#webhook.php
$cmd = shell_exec("git pull 2>&1");
#for debugging
echo $cmd;
?>
Execute the following command inside /var/www/html
sudo chown www-data:www-data -R my-repo/
Test it by going to http://www.myserver.com/my-repo/webhook.php
Add the path to your script to github webhooks.
I have been stuck in this problem for several hours.
I have thought about a solution.
1. move your script to a python file "script.py" and place this file to your server root.
2. shell_exec("python script.py");
Any way, it works for me.
On my host I had to give a different path for my php file to be executed from shell_exec().
This didn't work shell_exec('/usr/bin/php backgroundtask.php');.
While this did shell_exec('/opt/php/php-5.5.0/bin/php backgroundtask.php');.
You can visit this Reference.
I had the same issue because PHP backslashes.
PHP escapes the backslashes, so the command that reaches the shell
'COPY E:path1\path2\file.prn /B \127.0.0.1\"PRINTER NAME"'
so I gave command like this
'COPY E:\\path1\\path2\\file.prn /B \\\\127.0.0.1\"PRINTER NAME"'.
You have to double-escape the backslashes: once for PHP and once for the shell.

using exec command in php doesnt work

I have to automate a process using php in which I have to append content in a file.
The file does not have any specific permissions specified but the folder 'abc' has read only permissions, so fopen() prompts permission denied when I try to append a file.
But I can edit the file manually and also from the command prompt. So I tried the following:
When I try
echo exec("echo Testing>>\\xx.xx.x.x\C$\abc\test.txt");
in my script, it does not work.
If the same command
echo Testing>>\xx.xx.x.x\C$\abc\test.txt
is run on cmd it works.
I even tried psexec:-
echo exec('C:/psexec \xx.xx.x.x cmd /c \"echo Testing>>C:\abc\test.txt\"');
again when i run
C:/psexec \xx.xx.x.x cmd /c "echo Testing>>C:\abc\test.txt"
on cmd it works fine.
Is it anything to do with exec() that I am doing wrong?
OR Is there any other way I can edit file, because I should not change the folder permissions but still get the process automated.
I assume you are using Windows. On Debian Linux, I would tell you to give write permissions to user www-data on the appropriate directory.
You probably need to give the local IIS worker account write permissions on the directory. The local IIS worker account is likely named something like IUSR_[SERVERNAME].
Some webhosts decides to remove the function exec for security reasons.
view your php info and check if yours is disabled.

Categories