Unix & Linux: php shell_exec() permission on Linux Ubuntu (6 Solutions!!)
Channel: Roel Van de Paar
Date: 2020-08-10

Unix & Linux: php shell_exec() permission on Linux Ubuntu


The Question: I'm developing a php application using Linux server. My problem is executing
shell_exec() to call some exe file is not work(actually not exe, it's kind of
linux executable file)
echo shell_exec("whoami");
I got daemon
echo shell_exec("ls")
I got some filename but
echo shell_exec("php -v")
I got nothing, a blank page
echo shell_exec("php ....bla bla bla")
a blank page too.
All these command if I type on terminal(user hu) will works. I have searched
google for several hour, people says that because of permission. I don't have
experience on Linux. What do I have to do to execute my program in php?

Solutions: Please watch the whole video to see all solutions, in order of how many people found them helpful

== This solution helped 5 people ==
Try specifying the entire path to the php binary.. Eg, /usr/bin/php
If you don't know it, find it using: which php

== This solution helped 4 people ==
You generally want to specify full paths to applications such as whoami, ls,
and php. If you're unsure what a program's location is (full path) you can find
out like so:
$ type php
php is /usr/bin/php
Then specify it like so in your script.
＜?php
echo shell_exec("/usr/bin/php ....bla bla bla");
?＞

== This solution helped 2 people ==
Does your php.ini restrict the available command set ?
This is from my /etc/php5/php.ini
; When safe_mode is on, only executables located in the safe_mode_exec_dir
; will be allowed to be executed via the exec family of functions.
; http://php.net/safe-mode-exec-dir
safe_mode_exec_dir =

== This solution helped 10 people ==
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

With thanks & praise to God, and with thanks to the many people who have made this project possible! | Content (except music & images) licensed under cc by-sa 3.0 | Music: https://www.bensound.com/royalty-free-music | Images: https://stocksnap.io/license & others | With thanks to user X Tian (https://unix.stackexchange.com/users/31760), user Thusitha Sumanadasa (https://unix.stackexchange.com/users/41966), user slm (https://unix.stackexchange.com/users/7453), user ngoaho91 (https://unix.stackexchange.com/users/60305), user Keiran Holloway (https://unix.stackexchange.com/users/60303), user Arenim (https://unix.stackexchange.com/users/28447), user Anthon (https://unix.stackexchange.com/users/33055), user Alex (https://unix.stackexchange.com/users/188865), and the Stack Exchange Network (http://unix.stackexchange.com/questions/115054). Trademarks are property of their respective owners. Disclaimer: All information is provided "AS IS" without warranty of any kind. You are responsible for your own actions. Please contact me if anything is amiss at Roel D.OT VandePaar A.T gmail.com.