how to start an aws instance using php from browser - php

When I try to execute php file from browser which have a command to start AWS ec2 instance I am getting error as ec2start command not found, but when I execute this file from command prompt in Ubuntu it works fine as I am executing this as a root but when I execute this file from browser it get execute with the apache user,
anybody knows the solution for this,
Thanks,

It is quite probable that your command line environment is different from PHP's.
In order to do what you asked for, you need to specify ec2start with an absolute filename.
First run the following from your command prompt to find the path:
whereis ec2start
It should return a list of paths, just find the one containing ec2start.
Next, simply use that path you found inside your PHP script, like so:
shell_exec('/path/to/ec2start');

Related

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 environment variable is not working

I've followed tutorials and answer here on Stackoverflow, but I still can't access php.exe from command line.
All I get is:
php is not recognized as an internal or external command...
How can I make php.exe accessible from anywhere in the command line?
What else can I try to execute PHP from command line?
Path should be
C:\wamp\bin\php\php5.4.12
without php at end

Cannot execute command using exec() in php

Ive tried searching for the answer but cant find anything proper in relation to my case.
What i want to execute is very simple...its a call to the SPMF framework jar file.
here is the code -
<?php
exec('java -jar spmf.jar run Apriori /opt/lampp/htdocs/rrugd/ip.txt /opt/lampp/htdocs/rrugd/output.txt %20');
?>
why is this not executing?
it obviously runs perfectly using cli.
the folder having all the files (jar file,ip.txt,output.txt) have permissions set to r/w to all users
I am on Linux - Ubuntu 13.10
I found the solution.
The problem was in the path to the spmf zip file.
Either it needs a full path or should be in the same folder as the script.
I solved it by placing it in the folder same as the script.( under htdocs as i am using lampp)
Also, the folder needs to have permissions for other to be set to "Create and Delete" for php to be able to access and write output to the folder.
Basic debugging: If exec() doesn't work, then you need to check return values:
exec('some command here', $output, $return_var);
var_dump($output, $return_var);
NEVER assume that an external resource succeeded. ALWAYS check return values and output for error messages.
Most likely java isn't in the path of whatever shell PHP is using to handle the exec() call, meaning you'll need exec('/full/path/to/java ...') instead.

PHP exec() not working properly

I am having difficulty with the PHP exec() function. It seems to not be calling certain functions. For instance, the code echo exec('ls'); produces no output whatsoever (it should, there are files in the directory). That main reason this is a problem for me is that I'm trying execute a .jar from a PHP exec() call.
As far as I know I'm calling the java program properly, but I'm not getting any of the output. The .jar can be executed from the command line on the server. (For the record, it's an apache server).
My php for the .jar execute looks like this:
$output = array();
exec('java -jar testJava.jar', $output);
print_r($output);
All I get for output from this exec() call is Array().
I have had success with exec() executing 'whoami' and 'pwd'. I can't figure out why some functions are working and some aren't. I'm not the most experienced person with PHP either, so I'm not too sure how to diagnose the issue. Any and all help would be appreciated.
The reason why you are not able to execute ls is because of permissions.
If you are running the web server as user A , then you can only ls only those directories which have permissions for user A.
You can either change the permission of the directory or you can change the user under which the server is running by changing the httpd.conf file(i am assuming that you are using apache).
If you are changing the permissions of the directory, then make sure that you change permissions of parent directories also.
To change the web server user, follow following steps:
Open the following file:
vi /etc/httpd/conf/httpd.conf
Search for
User apache
Group apache
Change the user and group name. After changing the user and group, restart the server using following command.
/sbin/service httpd restart
Then you will be able to execute all commands which can be run by that user.
EDIT:
The 'User' should be a non-root user in httpd.conf. Apache by default doesnot serve pages when run as root. You have to set user as a non-root user or else you will get error.
If you want to force apache to run as root, then you have to set a environment variable as below:
env CFLAGS=-DBIG_SECURITY_HOLE
Then you have to rebuild apache before you can run it as root.
I have found the issue - SELinux was blocking PHP from accessing certain functions. Putting SELinux into permissive mode has fixed the issues (although, I'd rather not have to leave SELinux in permissive mode; I'd rather find a way of allowing certain functions if I can).
I have a solution:
command runs from console, but not from php via exec/system/passthru.
The issue is the path to command. It works with the absolute path to command
So that:
wkhtmltopdf "htm1Eufn7.htm" "pdfIZrNcb.pdf"
becomes:
/usr/local/bin/wkhtmltopdf "htm1Eufn7.htm" "pdfIZrNcb.pdf"
And now, it's works from php via exec
Where command binary you can see via whereis wkhtmltopdf
Tore my hair out trying to work out why PHP exec works from command line but not from Apache. At the end, I found the following permissions:
***getsebool -a | grep httpd*** ---->
**httpd_setrlimit --> off
httpd_ssi_exec --> off
httpd_sys_script_anon_write --> off**
USE: setsebool -P httpd_ssi_exec 1
SEE: https://linux.die.net/man/8/httpd_selinux
Your problem is not an execution issue but the syntax of the exec command. The second argument is always returned as an array and contains a single line of the output in each index. The return value of the exec function will contain the final line of the commands output. To show the output you can use:
foreach($output as $line) echo "$line\n";
See http://php.net/manual/en/function.exec.php for details. You can also get the command's exit value with a third argument.

Scheduled task says it works but it doesn't

I have a windows server, and I' trying to use scheduled tasks. When I create the job and try to run it through the interface, it says it works, but when I look at the DB it shows me that nothing happend. This is what I placed in the Path to executable file
"D:\Parallels\Plesk\Additional\PleskPHP5\php.exe" -f "D:\inetpub\vhosts\fab-offers.com\httpdocs\cms-sites\vipstore\users\index.php"
Check for:
Is the script working if you run it manualy?
Does php error logs contain anything related to the script?
Does the script contain paths like $_SERVER['DOCUMENT_ROOT'] that it won't have when run not from browser?

Categories