shell_exec doesn't work on Linux server - php

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.

Related

exec() rsync command doesn't work with Laravel8

Hope someone can shed some light on this one, it's driving me nuts. I have a backup web app written in php, this creates an rsync which is run on a remote server and stores the files in a local folder. This function works using the php exec function.
We are migrating this to within a laravel8 app and the identical call to the same rsync and to the same local storage directory fails. The error is a 255.
If i use the same rsync command from command line it works the same as it does in our original app.
A bit more...
The Laravel instance is using Horizon to perform this so i have this in the handle() function of a process file in the jobs folder.
Of note I have a dev version of this laravel app and with this it works correctly and syncs with the remote folder.
My rsync command is using an id_rsa file for the apache user - www-data (the app is not available to the outside world). the folders have the correct permissions and owners (www-data and 755 for directories and 644 for files).
an example (modified version) of the rsync command:
rsync -rLDvcs -e "ssh -p 22 -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i /var/www/.ssh/id_rsa" --no-perms --no-group --no-owner --progress --exclude-from '/var/www/backup/exclude.txt' --delete --backup --backup-dir=/hdd/mnt/incs/deleted_files/the-project/ theproject#188.102.210.41:/home/theproject/files/ /hdd/mnt/incs/theproject/files
the code which calls this is:
$aResult['res'] = exec($rsync . ' 2>&1', $aResult['output'], $aResult['return']);
when run $aResult['return'] has a value of 255 and the process has failed. The docs seem to say this is a failure to connect yet the identical call on another app on the same machine works (as it does if rsync is used in command line)
Any ideas? The php version on this box (the original app and the laravel upgrade) is php8.0 and my dev copy uses php7.4. my dev and this internal box use Ubuntu20.04 and apache2 so besides the php version are pretty identical.
No error is in any logs.
thanks
Craig

Run shell command in PHP from MAMP

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

Adding git-ftp to my environment for php

I've installed git-ftp onto my CentOS Server so that I can do deployments from an activeCollab installation running the ac_gitolite module.
Everything installed fine and the app lives at
/usr/local/bin/git-ftp
When I log into the server via ssh I can run git-ftp commands without the full path, but when attempting to use PHP to run commands with exec ie:
exec ("git-ftp --version 2>&1", $output);
I receive the following error:
sh: git-ftp: command not found
Is there an environment path I need to specify somewhere specifically for PHP, or what am I doing wrong?
The two options discussed in "command not found - error in exec() command" are:
One easy solution would be to put the full path of the git-ftp executable.
The other is to add git-ftp to the php server user's PATH.

Running wget with PHP and apache (MAMP)

I have trouble running wget through php exec or system functions. The configuration is with MAMP.
I successfully run basic commands like ('whoami', 'pwd', etc.).
I even changed the apache user to the root user and still nothing. In the error log it output "wget: command not found".
I can run with the terminal and it works fine. The wget is installed through Macports.
What should I do?
Regards!
From a terminal, run which wget, and use the full path in the call to exec/system.

How can I run a php without a web server?

I would like to ask if I can run a php without having installed a web server. Do I have to use the php like CGI and run my page via command line? And if so, what are the steps that I do I have to choose through the installation of php? I mean the preferences as CGI and the components after that step?
I installed the php 5.3.3 but is seems not working, I get several message that the php5ts.dll is missing and when I put that file in my ext folder other error messages appear. Are there any configuration files or steps that I have to use?
(is php 5.3.3 suitable for doing something like this?)
If I have to have a web server installed how can I run my php code through the command line?
You should normally be able to run a php file (after a successful installation) just by running this command:
$ /path/to/php myfile.php // unix way
C:\php\php.exe myfile.php // windows way
You can read more about running PHP in CLI mode here.
It's worth adding that PHP from version 5.4 onwards is able to run a web server on its own. You can do it by running this code in a folder which you want to serve the pages from:
$ php -S localhost:8000
You can read more about running a PHP in a Web Server mode here.
For windows system you should be able to run php by following below steps:
Download php version you want to use and put it in c:\php.
append ;c:\php to your system path using cmd or gui.
call $ php -S localhost:8000 command in a folder which you want to serve the pages from.
PHP is a normal sripting language similar to bash or python or perl. So a script with shebang works, at least on linux.
Example PHP file:
#!/usr/bin/env php
<?php
echo("Hello World!\n")
?>
How to run it:
$ chmod 755 hello.php # do this only once
$ ./hello.php
You can use these kind of programs to emulate an apache web server and run PHP on your computer:
http://www.wampserver.com/en/
http://www.apachefriends.org/en/xampp.html

Categories