I already said that I was new to exec() function. After doing some more digging, I came upon 2>&1 which needs to be added at the end of command in exec().

Thanks @mattosmat for pointing it out in the comments too. I did not try this at once because you said it is a Linux command, I am on Windows.

So, what I have discovered, the command is actually executing in the back-end. That is why I could not see it actually running, which I was expecting to happen.

For all of you, who had similar problem, my advise is to use that command. It will point out all the errors and also tell you info/details about execution.

exec('some_command 2>&1', $output);
print_r($output);  // to see the response to your command

Thanks for all the help guys, I appreciate it ;)

Answer from Brian on Stack Overflow
🌐
CyberPanel Community
community.cyberpanel.net › support and discussion › general discussion
PHP exec() not working | php.ini is fine - General Discussion - CyberPanel Community
March 7, 2023 - Hi, I’ve seen few topics but nothing worked. My php.ini doesn’t block exec() or shell_exec(), but yet the shell command from php doesn’t execute. It works only from the terminal. I gave folder permissions to user: cy…
🌐
PHP
php.net › manual › en › function.exec.php
PHP: exec - Manual
So the exec() command will not work. You will get a 127 (command not found) result code. The reason is, the shell (/bin/sh) is missing in chroot, but the exec() command requires the shell.
🌐
Stack Overflow
stackoverflow.com › questions › 41391824 › php-exec-doesnt-execute-but-output-works
php exec() doesn't execute but output works - Stack Overflow
It might be operated by a different user with different privileges, and it may have some other environment variables. Try comparing the exposed environment variables, and try running $(whoami) from using exec.
Top answer
1 of 6
12

The Apache’s user www-data need to be granted privileges to execute certain applications using sudo.

  1. Run the command sudo visudo. Actually we want to edit the file in etc/sudoers.To do that, by using sudo visudo in terminal ,it duplicate(temp) sudoers file to edit.
  2. At the end of the file, add the following ex:-if we want to use command for restart smokeping and php command for another action in your question,

www-data ALL=NOPASSWD: /etc/init.d/smokeping/restart, usr/bin/php

(This is assuming that you wish to run restart and php commands using super user (root) privileges.And you use php command in usr/bin/ path )

However, if you wish to run every application using super user privileges, then add the following instead of what’s above.You might not want to do that, not for ALL commands, very dangerous.

www-data ALL=NOPASSWD: ALL

3.After edit the sudoers file(by visudo we edit the temp file of sudoers so save and quit temp file(visudo) to write in sudoers file.(wq!)

4.That’s it, now use exec() or shell_exec in the following manner inside your xxx.phpscript.keep remember to use sudo before the command use in the php script.

ex:-

exec ("sudo /etc/init.d/smokeping restart 2>&1");

or

shell_exec("sudo php -v"); 

So in your problem,add the commands that you wish to use in to the step no (2.) as I add and change your php script as what you want.

here is the same problem as yours https://stackoverflow.com/a/22953339/1862107

2 of 6
5

Try specifying the entire path to the php binary.. Eg, /usr/bin/php

If you don't know it, find it using: which php

🌐
Server Fault
serverfault.com › questions › 793238 › php-exec-not-working-via-browser
ubuntu - php exec not working via browser - Server Fault
August 1, 2016 - But convert command does not gets executed. ... Permissions issue, almost certainly. Turn error reporting on or check the server logs. ... Is that all that you have in your script? If not, make a stripped down copy that serves only at demonstrating the issue and show it completely here. ... OMG NO - please learn how permissions work, how code injection occurs and implement a minimal set of permissions.
🌐
Server Fault
serverfault.com › questions › 730864 › bash-script-working-in-command-line-but-not-through-php
Bash script working in command line but not through PHP - Server Fault
I checked the PHP error log but it does not show anything unfortunately. Could it be a user permissions issue? ... You could create a logfile somewhere, e.g. /tmp/debuglogfile and make it writeable by all: chmod 666 /tmp/debuglogfile. Then change the PHP code to shell_exec('sudo /Users/Derk/Code/automation/psc/pdfs.sh 1 2>/tmp/debuglogfile') and see if any error output is written to the logfile. As you write the first command is executed (make sure it is! "seems to work" doesn't cut it), then sudo is apparently set up correctly, so permissions shouldn't be the problem...
Find elsewhere
🌐
Super User
superuser.com › questions › 92625 › command-working-from-terminal-but-not-from-php
linux - Command working from terminal but not from PHP - Super User
Solved it! The -v option was indeed useful, i used ffmpeg -v 5 and got useful information. The problem was that ffmpeg terminated after a while, maybe phps' max_exection time, dont know. However, I now run nohup php myfile.php.
🌐
Stack Overflow
stackoverflow.com › questions › 9364025 › why-isnt-my-php-exec-working-when-trying-to-call-a-python-function
Why isn't my PHP exec() working when trying to call a Python function? - Stack Overflow
Do you get output? Also try putting error_reporting(-1); ... According to this http://www.php.net/manual/en/function.passthru.php, you're using passthrough in a wrong way. It is for binary data...
🌐
Stack Overflow
stackoverflow.com › questions › 39371210 › exec-is-not-working
php - exec() is not working - Stack Overflow
... First, store your command in a variable and try echoing it and runnining in a terminal to see if it's valid at all: $command = "youtube-to-gif -u https://www.youtube.com/watch?v={$vidID} -b $start_second -d $different_second -s 480x?
🌐
Raspberry Pi Forums
forums.raspberrypi.com › board index › using the raspberry pi › beginners
unable to exec a script via php [SOLVED] - Raspberry Pi Forums
Also, a better location for the two python scripts would be outside the document root so they are not visible to the world. The two python scripts only need to be readable from somewhere by the PHP script. If you change the first line of your python scripts to ... #!/usr/bin/python3 and then make those scripts executable using chmod, then you can invoke them more precisely with sudo ... $cmd = exec("sudo /some/path/to/trigger.py a ".$x); Then that allows the main point to be addressed, which would be your sudoers configuration. Your work-around in the post above makes www-data effectively root.
🌐
Stack Overflow
stackoverflow.com › questions › 22789849 › exec-command-not-working-in-php-but-works-in-terminal-putty
pdf - exec command not working in php but works in terminal/putty - Stack Overflow
April 1, 2014 - It is all working fine from terminal. And the user is same in both putty and connecting credentials ... yeah........ no manage your special chars external to the exec command exec("cp $source $destination"); after you've escaped properly using \ for command line.
Top answer
1 of 1
6

By the looks of it your PATH variable only includes /bin. This only allows you to run executables within that directory. There are a few ways to fix this.

Method 1: Configure the web server environment varibles

If you are running apache, you can simply edit /etc/apache2/envvars to include a PATH varibale definition. Edit the file and add a new line to the bottom (if it doesn't already exist):

# /etc/apache2/envvars
...

export PATH="/bin:/usr/local/bin"

Method 2: Configure the PATH for the user

Alternatively, if you are running the web server as a user other than a service user, that user may not have their PATH properly configured. This is as simple as changing their environment variables for the user and the web server will inherit it (unless defined otherwise in the web server's configuration).

First step is figure out which user your web server is running as. If you don't know, you can check list the running processes to find the user. This can be accomplished by running the following command:

ps aux|grep {webserver}|grep -v grep Where {webserver} is replaced with the web server you are currently running. (apache/httpd, nginx)

Alternatively, you can check in of the following config files:

  • /etc/httpd/conf/httpd.conf - CentOS Apache
  • /etc/apache2/apache2.conf - Ubuntu/Debian Apache
  • /etc/nginx/nginx.conf - nginx config

(There are many other possible configurations, but these are the most common)

Once you've found out which user you're running as, you will need to then set the PATH variable for that user. This could be as simple as exporting the PATH in their home bash configuration. This could be /home/bob/.bashrc for example. Service users without a home will be different however.

Method 3: Declare the PATH within your PHP script

You can manually specify the PATH variable within your PHP script. This can be accomplished by adding the following line to your script:

<?php

putenv('PATH=/bin:/usr/local/bin');
...

You will need to change the PATH to suit your needs, and it will need to be declared before you call shell_exec().

This method isn't preferred as you will need to specify this for each PHP script you execute that makes use of the shell_exec() call to binaries outside of /bin, but it is a quick one off solution that will work.

More importantly, you are writing code that is not portable and is dependent on a specific system. This is bad coding practice and is not recommended/frowned upon.

🌐
Stack Overflow
stackoverflow.com › questions › 28069744 › php-exec-works-in-command-line-but-not-when-executed-via-browser
apache - PHP exec() works in command line but not when executed via browser - Stack Overflow
January 21, 2015 - Remember that via the web apache launches a process that has PHP in it. via command line there is no apache process. ... There are separate php.ini files for php lounched by browser (with apache) and for cli (terminal - without apache) maybe you have different values for max_execution_time or memory_limit - usual php.ini for cli has more memory and it could run for longer period of time.