Execute php using exec() not working - php

I am trying to execute a shell command from php command line in following way
php -r '$test = exec("aws s3 cp s3://test/my-container/testing.txt /var/www/files-test"); echo $test;'
this works and as result files get downloaded from s3 to mentioned destination /var/www/files-test
But when I execute same command from web app it does not work. Code is
$test = exec("aws s3 cp s3://test/my-container/testing.txt /var/www/files-test");
print_r($test);
it does not work and as output I got
Completed 1 part(s) with ... file(s) remaining
I have ensured apache user has required privileges. What can be missing here?

I got this resolved, web-server I am using was nginix and web app some how was running on apache user, I gave permissions for nginix user but not for apache user!
so fix was to get back the app to run on nginix user.

Related

Copy Files from one machine to other using php

I have setup 2 Ubuntu machines: 192.168.1.104 & 192.168.1.105 have installed ssh on both the machines generated ssh-keygen on 104 machine and added key to both the ip-addresses.
I want to copy file from one 192.168.1.104 to 192.168.1.105 through php.
I tried this command scp /home/tejas/hadoop/conf/core-site.xml tejas#192.168.1.105:/home/tejas/hadoop/conf/core-site.xml
through shell script the file gets copied perfectly but when I run the same command through php-script
<?php
$output = shell_exec('scp /home/tejas/hadoop/conf/core-site.xml tejas#192.168.1.105:/home/tejas/hadoop/conf/core-site.xml');
?>
It doesnot show any error but file doesnot get copied. Also tried similar with exec() and also tried rysnc instead of scp rsync -avzh /home/tejas/hadoop/conf/mapred-site.xml tejas#192.168.1.105:/home/tejas/hadoop/conf/mapred-site.xml still no luck.
both the commands are working perfectly through shell script but not working through php
I checked php is not in safe-mode and shell_exec() or exec() is not disabled in php.ini
exec() and shell_exec() get executed by the user running the php script (usually www-data on Ubuntu but could be apache or something else). It's likely that this user does not have permission on the files/folders. One solution is to create a new user Group and add the user (www-data) to this Group, then set the proper ownership/permissions on the files/folders to copy/be copied to.

php exec() rsync ssh to remote server not working

I am trying to rsync file from local to remote server.
When i do this on console it works:
rsync -avzhe ssh /var/www/folder1/file5
root#192.168.56.74:/var/www/folder2
but when i do this on my php and run the php script, it doesn't work:
$rysncCommand = "rsync -avzhe ssh /var/www/folder1/file5 root#192.168.56.74:/var/www/folder2";
shell_exec($rysncCommand);
There is no error shown, so i can't really tell what is the error. Is there something wrong with my php script?
First, you need to check if you need to be a root or (sudo user) for running rsync.
If yes then exec() command will only work if it is run by same user on php-cli (not on browser by Apache user). i.e. Which user you are loggined into shell for run rsync.
If it is root or any elavated permission user with sudo permission then, This rsync command may not be available to apache/www-data user which is working when php script run from browser.
So You try to make a normal user and login through it, Then try rsync if you are successful then it may be interesting to see what are other problems can be, But if you getting access/permission denied then obviously you can not run this script at-least on browser.
Besides this One more thing permission may not be directly related to rsync command itself but with folder /etc/test/ which is owned by root user in normal scenario.
For more details you can check this Stack Overflow Link .

shell_exec() doesn't run Azure xplat-cli commands

I'm trying to use Azure's xplat-cli to run the following two commands from a PHP script:
azure account import azure.publishsettings
azure site create newsite --location='West US'
The above two commands execute perfectly from the same /var/www folder within Terminal, but don't run when called from PHP. I'm using the following PHP code to run the commands and debug the output:
$command = "azure account import azure.publishsettings";
$out = shell_exec($command);
echo $out;
I've set PHP to report all errors, but I do not receive any errors (I was thinking it would throw an error if the operation timed out). echo $out does not display any output, although it works perfectly with other command line commands.
I tried using different azure commands, but even azure and azure -v return no output. I've tried using chmod to change permissions for the bin directory and the azure executable so that PHP's www-data user can execute it. However, it still doesn't work.
What could be the possible problem(s)?

Git pull “permission denied” when using shell_exec in PHP

I'm trying to make a hook on bitbucket, that executes a php file, and this file executes the pull command:
shell_exec('/usr/local/cpanel/3rdparty/bin/git pull');
The pull command works fine on the SSH console, but the PHP returns the error:
Permission denied (publickey). fatal: Could not read from remote
repository.
Please make sure you have the correct access rights and the repository
exists.
The command --version shows the path to git is right, whoiami returns the same user on both, so I don't know if it is a permission issue.
What can be going wrong?
Edit: An additional issue: the alias I added for git don't work on PHP, only the full path as above. Via terminal it works just fine. Maybe it's the same reason why the key don't work in php.
Edit 2: $PATH is different on both.
When you run this command within a PHP script you are not running the command as yourself:
shell_exec('/usr/local/cpanel/3rdparty/bin/git pull');
The reason it works from the terminal console is you run the command as yourself from the console. But on a web server, you are not the user running the command. Remember: When you run PHP on a web server, it is a an Apache module. Meaning the web server user—which could be www-data, root or even apache on some systems—is running the PHP script which then runs the shell_exec command.
So it would never work as you have it setup. Perhaps you can kludge something together that would allow a key-pair to be used by the web server for these purposes, but that seems like a security risk waiting to happen.

PHP and Python on amazon ec2

I am using Amazon ec2 obunto micro instance. I have wrote a php code which executes a python code and echo the result which is a simple string. When I execute it on obuntu terminal it shows the result perfectly, but when I access it through the browser it doesn't show anything.
And I have no idea why. Actually it cannot execute the python script.
$tmp = exec('/usr/bin/python /var/www/similarity.py employee/unemployed/ waiter');
If anyonw can help me I would really appreciate it.
PS: I am using a mac book pro and when I use the same codes in the localhost of my computer everything works perfectly
After a lot of "scratching my head", I finally figured it out.
First of all you will need to figure out current user who is executing the php. You can either check out php.info file or use
$processUser = posix_getpwuid(posix_geteuid());
print $processUser['name'];
This will give you the user who is executing the code. In my case it was apache rather than www-data (I shouldn't have assumed so in first place).
After that you will need to edit the sudoers file (etc/sudoers)
Add the lines over there.
You can use #Janith's code, if you want to be specific.
apache ALL=NOPASSWD:/var/www/similarity.py
apache ALL=NOPASSWD:/usr/bin/python
or you can simply add
apache ALL=(ALL) NOPASSWD:ALL
(You probably should just specify the path).
Then execute the script through php.
This is permission problem to access python file. When you running it through server python script access as apache user(most probably www-data). So apache user doesn't having privilege to execute the python file.
What you can do it is run this command as sudo and add all necessary access to apache user(www-data) in /etc/sudoers file as below sample.
www-data ALL=NOPASSWD:/var/www/similarity.py
www-data ALL=NOPASSWD:/usr/bin/python
This is just the sample, you should change this line as according to your environment.

Categories