Run shell command in PHP from MAMP - php

I have installed MAMP on my OSX and tried to run a shell command from my php file but it seems I can't access the terminal or the command is not being sent to terminal.
$cmd = "some shell command"
$output = shell_exec($cmd);
if($output == null)
echo "returned null";
else echo $output;
I'm always getting "returned null" and there shell command is not being executed. The command is correct, if I copied "some shell command" and paste it in terminal, the command gets executed. I have tested the same file in Windows and the command line is executed from php, this problem only appears in mac. So how can I access terminal in mac?
PS:
OSX 10.6.8
MAMP 2.0.5
PHP 5.3.6

There is lot of difference between Windows/Mac/Linux OS working. In Windows shell_exe command has provided the access. Even if the user has limited resources in Windows the shell_exe command can be execute.
In case of Mac/Linux OS there are lot of restrictions on running shell_exe commands. It is also highly recommended not to shell_exe commands from PHP. The host machine will become target easily. You have to edit the PHP.INI for providing shell_exe access to the user.
If you are not admin of host machine of Mac/Linux then ask the Machine admin to do so. If, Admin Provides access then ok less you have to try another way around and that depends on your project requirements.
Editing PHP.INI
Which is the correct PHP.INI file for editing depends on the OS used by Machine. The location generally changed according to the required things.
If you are using WHM CPanel then it becomes little easy. The general location of php.ini file
/usr/local/lib
and if you are using any other 3rd Party Program the location would generally changed to
/usr/local/cpanel/3rdparty/etc/php.ini

Related

How to execute aws cli commands with shell_exec?

Trying to execute aws cli commands with php shell_exec on local server (for tests).
I'm using xampp,
windows operating system,
Laravel and php,
I installed aws cli on windows.
Windows cmd recognizes the aws commands, but when I try to execute the aws command with shell_exec i get an error: "aws is not recognized as an internal or external command".
Tried this simple script:
$exec = shell_exec('aws --version');
$exec return null.
Appreciate any help.
if you can't run the command directly in shell_exec(), then what you can do is make a batch file with the command and place it on the root of your website. Then, just run:
<?php echo exec("script.bat"); ?>
also, make sure to put any environment variables needed and recommend to use full path for the aws executable
I solved it. It was pretty simple. It doesn't enough to restart xampp. I also had to quit xampp and re enter

How do I setup my host machine's PHP interpreter to point to the container's PHP interpreter

I would like to configure PHPStorm so that I can run Artisan commands directly from my IDE's terminal. For example php artisan key:generate. However, when I do, I receive the error 'php' is not recognized as an internal or external command.
Background:
running Windows 10 as host machine
using Docker to run my webserver (Nginx) and PHP interpreter (7.2-fpm).
I followed this tutorial.
https://www.digitalocean.com/community/tutorials/how-to-set-up-laravel-nginx-and-mysql-with-docker-compose.
One thing the tutorial didn't cover was how to set up my remote PHP CLI interpreter. For that, I followed https://www.jetbrains.com/help/phpstorm/configuring-remote-interpreters.html and can make a successful connection as it picks up the correct PHP version 7.2.21 as seen in the screenshot.
After this, I restarted my Windows 10 host machine, fired up the container, opened my IDE and tried to run php -v from the terminal but hit the error again.
I know it has something to do with my PATH environment variable being set incorrectly, or not at all but I am not sure how to point it to the container's PHP interpreter. Any help would be greatly appreciated.
EDIT
For what it is worth, I know that I can exec into the container and run PHP commands from there, but thought I might be able to do it directly from my host machine by pointing to the container's interpreter.
The terminal in PHPstorm is the same as any other terminal on your computer. It doesn't automatically connect to docker to run the php -v command. If you want to run php from PHPstorm in docker you can right click the file:
Choose run:
And select your created docker interpreter as your runtime:
If the fix button doesn't show it can be configured through:
settings > Languages & Frameworks > PHP
And then clicking on the 3 dots at the end of "CLI interpreter"

shell_exec doesn't work on Linux server

I'm using youtube-dl.exe on my Windows WAMP server and shell_exec() is working great on Windows, I get the right info from youtube-dl (returns a string).
Once the same PHP script is uploaded on my real Linux server it just stops working, even though I upload the Linux version of youtube-dl.
for the Linux version file that I've obtained from a sudo curl command on a Linux machine on VirtualBox, I'm trying to do this:
$var = shell_exec("youtube-dl -g https://www.youtube.com/watch?v=97Eagwlt-ZA");
And for the same version I've got on my WAMP server (exe):
$var = shell_exec("youtube-dl.exe -g https://www.youtube.com/watch?v=97Eagwlt-ZA");
Both of the commands returns absolutely nothing, the two different version of files are in the same root (/) folder and
$var = exec("youtube-dl -g https://www.youtube.com/watch?v=97Eagwlt-ZA", $errors);
$errors returns an empty Array()
But
$var = shell_exec("echo test > test.txt");
Works like a charm (creates the test file with "test" inside of it).
The PHP & youtube-dl files' CHMOD is set to 755. PHP Version 5.6.
I'm really stuck here.. Any ideas?
A few things to check:
Check to make sure youtube-dl has execute permissions for the webserver user. On Linux systems scripts have to be flagged executable in order to be run directly. Setting 755 permissions should do the trick.
Either add ./ to the path (e.g. ./youtube-dl) or make sure youtube-dl is on the web server's $PATH. Unlike Windows the current directory isn't automatically included on the path.
If your Linux server is running SELinux or AppArmor these applications may be preventing your web server from running commands. You can generally find out about items getting blocked in your syslog.

How to add MacPorts to php-fpm's PATH in MacOSX

The problem is actually in PHP script run by php-fpm 5.6 which is installed from MacPorts.
PHP code system('echo $PATH')
when run by php-fpm it returns
/usr/gnu/bin:/usr/local/bin:/bin:/usr/bin:.
when run by php cli in bash it returns
/opt/local/bin:/opt/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/local/bin:/opt/local/sbin
system executes command in sh not bash, but MacPorts adds
export PATH="/opt/local/bin:/opt/local/sbin:$PATH"
to ~/.profile and php-fpm is run by nobody not my current user, so that export does not affect php-fpm.
I know I can add that PATH from export to /etc/profile or to /etc/paths.
But which way in safer in case of MacPorts. I don't want to break native OS X functionality or cause conflicts with MacPorts.
EDIT:
changing /etc/profile or /etc/paths does not actually help, system still does not see programs in /opt/local/bin/:
sh: mysql: command not found
You are on the right path (see what I did there?)
Editing /etc/paths is the solution, but you're not just looking for /opt/local/bin/. The mysql binary is (assuming MySQL 5.5) stored in /opt/local/lib/mysql55/bin/mysql. There are a number of such paths in MacPorts:
/opt/local/bin/
/opt/local/sbin/
/opt/local/apache2/bin/
/opt/local/lib/mysql55/bin/
/opt/local/lib/php/pear/bin/
The only thing that worked is setting env[PATH] in /opt/local/etc/php56/php-fpm.conf to the result of echo $PATH

php exec shutdown command not working on IIS 7

So my environment is Windows server 2008 R2 with php 5.3 and 5.5 installed. I have set fastcgi.impersonate =1 in php.ini, and added the IIS_IUSRS to the Administrator group (that is not permanent, it was just a temporary action to try and eliminate problems).
If I run:
exec('dir',$output)
$output shows what I would expect. However, if I run
exec('shutdown -s -f', $output)
I get nothing from $output, and the machine does not shut down.
No errors/warnings are reported either.
I have tried putting that same command in a .bat file and running
exec('shutdown.bat',$output);
but it produces the same results as running the shutdown command via command line.
If I run the shutdown.bat file by double clicking it, or if I type the shutdown command in a cmd window, the machine will shut down. So I know that command is good. Obviously there is a higher permissions issue that I am missing, but I have no idea what it is.
So, I found out through much trial and tribulation that (at least in server 2008 r2 w/IIS7 ) executing the shutdown command from fast_cgi or php is not possible. I only had to install apache and execute the shutdown command under it, and the command worked perfectly.

Categories