Apache permissions to execute the exec function - php

I've been trying to run this php code on CentOS:
<?php
$command = "diff file1 file2 > file3";
exec($command, $output, $error_code);
if ($error_code != 0) {
echo "Error: $error_code";
}
?>
And it always echoes "Error: 1". Error 1 is "Operation not permitted" http://www.pegasoft.ca/resources/boblap/99_b.html. It looks like apache has no permissions to do certain things, right? How can I fix that?

I think it's because you're not using the right command. You're running $command like in a terminal, so you need to add a command indicating that you have permission. I think that on CentOS it's su. In Ubuntu, for example, you would do sudo -command-.
So try to add su before diff.
Edit:
You should check here for proper usage of su:
http://wiki.centos.org/TipsAndTricks/BecomingRoot

Oh >____<
I had this line at the end of my script:
exec("rm -f /var/local/out/upload/example_word/word/diff.diff");
The file was created and then deleted... That's why I could never see it. Sorry.

Related

Executing bash script from PHP on raspbian

I have a script that calls fswebcam to capture a jpg with my USB camera. I've made it executable with "chmod +x webcam.sh" :
File : /var/www/html/webcam.sh
#!/bin/bash
DATE=$(date + "%Y-%m-%d_%H%M")
fswebcam -r 640x480 /home/pi/webcam/$DATE.jpg
This is working fine in command line without sudo, so I've made a small PHP page :
File : /var/www/html/index.php
<?php
$output = shell_exec('sh /var/www/html/webcam.sh');
echo "<pre>$output</pre>";
?>
When I go to the webpage, I just get a blank page and no jpg is created in my webcam folder.
I got the following error :
Apache2 error log
So I've tried modifying my call in PHP, to :
<?php
$output = shell_exec('/usr/bin/sudo /bin/bash /var/www/html/webcam.sh');
echo "<pre>$output</pre>";
?>
I've also add the following to sudoers file
www-data ALL=NOPASSWD: /path/to/script
But I still get the error : apache2 log error
I've tried everything from this thread : How to run .sh script with php?
Do you have any idea ?
Thanks in advance,
Victor
First off:
Don't use sudo if you don't have a very good reason for it.
sh does not necessarily invoke bash.
sudo expects a password, but you didn't provide any hence the error.
I suggest trying with exec instead of shell_exec (there is a difference between the two):
<?php
exec('/var/www/html/webcam.sh', $output, $exitCode);
echo 'Exit code: '.$exitCode.' <hr />';
echo implode('<br />', $output);
Another source of your problem could be permission related:
The webserver usually runs as a different user.
Make sure the webserver can actually write to the output directory.

PHP and Window environment based grep [duplicate]

so I have read about 10 answers and everyone seems to suggest ideas which for some reason don't work.
i am trying to execute a simple command line which is "svn update" but it is not working and it returns NULL
so i have tried trial and error the way and for now this is what i can say;
i have tried several commands like
<?php
exec ("cmd /c ping 127.0.0.1 -n 1 > results.txt ");
?>
and
<?php
exec ("cmd /c chdir > results.txt ");
?>
and both work.. infact chdir says the exact position where the php file executing the line is stored on the pc..
so the problem now is, why do some commands like this:
<?php
exec ("cmd /c dir > results.txt ");
?>
don't work? this results and empty value even though inside the folder i have several files and directories.
and why if i use the command prompt to move into the folder where the php file is store and type svn update it works and doing
<?php
exec ("cmd /c svn update > results.txt ");
?>
return a NULL?
any help is really appreciated.
it feels like i have some restrictions dued to the configuration setup because when i try in local using apache i can get most of the commands to work (shell_exec, system, exec, even without the cmd /c)
Ok.
i have managed to solve the issue..
this is what i did:
first check exactly what username is running for the specific website.. to do so do:
<?php
$out = array();
exec('cmd /c whoami 2>&1',$out,$exitcode);
echo "<br />EXEC: ( exitcode : $exitcode )";
echo "<hr /><pre>";
print_r($out);
echo "</pre>";
?>
this will return the computername followed by the username..
now on the computer running the webserver run
control userpasswords2
and give administrator powers to the username whoami said
this will allow you to finally run any command you want using exec or system_exec
on the other hand continuing with my SVN command i found out that I had another problem which is that when you run it, it will look for the config file which is under the administrator account and will give an error saying:
svn: E125001: Can't determine the user's config path
to solve this issue you simply have to specify in the command the config_dir by doing this:
exec('cmd /c svn update --config-dir C:\Users\Administrator\AppData\Roaming\Subversion C:\\inetpub\\vhosts\\websitename\\httpdocs\\folder 2>&1',$out,$exitcode);
hope this helps others which are having problems like the ones i had!
This is likely a system user permissions issue. I tried your example:
<?php exec ("cmd /c dir > results.txt "); ?>
On my Windows7 with Xampp installed and it worked perfectly fine. However with IIS the "user" may not have permissions to the directory, off the top of my head I think it may be the system user IIS-IUSR or something like that.
Here is a link that might help with user permissions for IIS: http://www.iis.net/learn/get-started/planning-for-security/understanding-built-in-user-and-group-accounts-in-iis
Okay, based on your answers, I think you should try this:
Execute your command using the 'svn.exe' executable (replace the [[reposity location]]).
It is possible that your client has another svn.exe location, but you will have to figure that out yourself :)
exec('cmd /c "c:\\Program Files\\TortoiseSVN\\bin\\svn.exe" up "[[repository location]]"');
What happends now?
There is also a second parameter in exec, maybe you should also take a look at that one.
The only thing that helped me was, complete routes:
$template_file = "C:/archivos/archivos.tex"
$cmd = sprintf("C:/texlive/2020/bin/win32/pdflatex.exe " .$template_file );
$result = exec($cmd, $output, $a);

not able to run this command in php

I am trying to download a file from a remote website. I need to run this command in command line through php. But it is not happening. Please help me.
echo $name;
$param="ftp://ftp.ebi.ac.uk/pub/databases/emdb/structures/EMD-{$name}/map/emd_{$name}.map.gz";
echo $param;
$command="wget ".escapeshellcmd($param)." -O /home/nagarjun/mercurial-1.4.1/clussym/trunk/dataset/emd{$name}.map.gz -e use_proxy=yes -e ftp_proxy=authproxy.serc.iisc.ernet.in:3128";
echo "<br />$command";
//$outputofexecutable = shell_exec($command);
passthru($command);
I can see two explanations:
wget is not found because the environment variables are not the same: use an absolute path
Your Apache (or any other webserver) user does not have permission to execute this command: you can use sudo, please see https://serverfault.com/questions/157272/allow-apache-to-run-a-command-as-a-different-user

PHP exec on a Windows with IIS configuration

so I have read about 10 answers and everyone seems to suggest ideas which for some reason don't work.
i am trying to execute a simple command line which is "svn update" but it is not working and it returns NULL
so i have tried trial and error the way and for now this is what i can say;
i have tried several commands like
<?php
exec ("cmd /c ping 127.0.0.1 -n 1 > results.txt ");
?>
and
<?php
exec ("cmd /c chdir > results.txt ");
?>
and both work.. infact chdir says the exact position where the php file executing the line is stored on the pc..
so the problem now is, why do some commands like this:
<?php
exec ("cmd /c dir > results.txt ");
?>
don't work? this results and empty value even though inside the folder i have several files and directories.
and why if i use the command prompt to move into the folder where the php file is store and type svn update it works and doing
<?php
exec ("cmd /c svn update > results.txt ");
?>
return a NULL?
any help is really appreciated.
it feels like i have some restrictions dued to the configuration setup because when i try in local using apache i can get most of the commands to work (shell_exec, system, exec, even without the cmd /c)
Ok.
i have managed to solve the issue..
this is what i did:
first check exactly what username is running for the specific website.. to do so do:
<?php
$out = array();
exec('cmd /c whoami 2>&1',$out,$exitcode);
echo "<br />EXEC: ( exitcode : $exitcode )";
echo "<hr /><pre>";
print_r($out);
echo "</pre>";
?>
this will return the computername followed by the username..
now on the computer running the webserver run
control userpasswords2
and give administrator powers to the username whoami said
this will allow you to finally run any command you want using exec or system_exec
on the other hand continuing with my SVN command i found out that I had another problem which is that when you run it, it will look for the config file which is under the administrator account and will give an error saying:
svn: E125001: Can't determine the user's config path
to solve this issue you simply have to specify in the command the config_dir by doing this:
exec('cmd /c svn update --config-dir C:\Users\Administrator\AppData\Roaming\Subversion C:\\inetpub\\vhosts\\websitename\\httpdocs\\folder 2>&1',$out,$exitcode);
hope this helps others which are having problems like the ones i had!
This is likely a system user permissions issue. I tried your example:
<?php exec ("cmd /c dir > results.txt "); ?>
On my Windows7 with Xampp installed and it worked perfectly fine. However with IIS the "user" may not have permissions to the directory, off the top of my head I think it may be the system user IIS-IUSR or something like that.
Here is a link that might help with user permissions for IIS: http://www.iis.net/learn/get-started/planning-for-security/understanding-built-in-user-and-group-accounts-in-iis
Okay, based on your answers, I think you should try this:
Execute your command using the 'svn.exe' executable (replace the [[reposity location]]).
It is possible that your client has another svn.exe location, but you will have to figure that out yourself :)
exec('cmd /c "c:\\Program Files\\TortoiseSVN\\bin\\svn.exe" up "[[repository location]]"');
What happends now?
There is also a second parameter in exec, maybe you should also take a look at that one.
The only thing that helped me was, complete routes:
$template_file = "C:/archivos/archivos.tex"
$cmd = sprintf("C:/texlive/2020/bin/win32/pdflatex.exe " .$template_file );
$result = exec($cmd, $output, $a);

Why can't I execute ffmpeg using shell_exec in a MAMP environment?

I'm trying to run ffmpeg commands from php in my MAMP environment and it won't work. I've already added it to my path. I can run shell commands like whoami and ls -la with success but ffmpeg doesn't seem to work. I've also installed ffmpeg-php but it doesn't support the functions i need—Video > Image stack.
Example code:
<?php
if (shell_exec("/usr/local/bin/ffmpeg"))
{ echo "Success"; }
else
{ echo "No good"; }
?>
You should try to open the file /Applications/MAMP/Library/bin/envvars and then comment the four lines of if and else. This will looks like this:
#if test "x$DYLD_LIBRARY_PATH" != "x" ; then
# DYLD_LIBRARY_PATH="/Applications/MAMP/Library/lib:$DYLD_LIBRARY_PATH"
#else
# DYLD_LIBRARY_PATH="/Applications/MAMP/Library/lib"
It works fine for me! In my case I also setted up all project permission like 777. If you need to do this you need execute the terminal command
chmod -R 777 projectDirectory
on file htdocs directory.
I hope that you can enjoy it. Good Lucky!
1.Find the file /Applications/MAMP/Library/bin/envvars_
2.Rename this file to /Applications/MAMP/Library/bin/envvars
3.Comment all lines of this file.
4.Write this:
PATH="/Applications/MAMP/bin/php5.6.10/bin:usr/local/bin:$PATH"
export PATH
Notice: php5.6.10 - you have to write your current php version
This link on stackexchange works https://superuser.com/questions/404344/ffmpeg-works-on-terminal-not-with-php-exec
Add '2>&1' to the end of your command like this
<?php
$info = shell_exec('/usr/local/bin/ffmpeg -i '.$inputMp3Path.' 2>&1');
var_dump($info);
?>

Categories