Run PHP command in Centos 7 via shell_exec()

Asked

Viewed 250 times

0

I’m starting in PHP, I want to make a page that triggers some commands on the Linux server. (Ex.: through a button, run a "service dhcpd Restart" or ". /Restart-Tomcat.sh" When I run this application in xampp (Windows), it works well, I open the calculator, notepad and even throw a ping inside a div, as can below.

But when I put there in Linux Centos7 in /var/www/html that field where the ping should appear is empty and no other command is executed.

I already gave the permissions to apache there in the /etc/sudoers.

It will be something specific that needs to change in code to run on Linux?

[HTML] Server Test div { width: 500px; height: 200px; border: Solid black; margin: 100px; }

    img{
        max-width: 500px;
        max-height: 200px;
    }
</style>

<button type="button" id="btn" onclick="geral()">Ping</button>

<div id="campo"></div>



<script>
    function geral() {

var xhttp = new XMLHttpRequest();
var campo = document.getElementById('campo');

xhttp.open("POST", "script.php", false);
xhttp.send();
// console.log(xhttp.responseText);
resposta = xhttp.responseText;

campo.innerHTML = resposta;

} 
</script>

[PHP]

**

header('Content-Type: text/html; charset=utf-8');

$result = shell_exec('ping -C4 www.google.com');

echo '

'.$result.'
';

?>

1 answer

0

Sometimes I use shell_exec + php, but more in Debian-derived systems. To work properly, I often need to configure the permissions of the user that php uses, I think it is the www-data user in debian derivatives. Anyway use:

$result = shell_exec('whoami');

So you should find out which user php is using and then just give the necessary permissions, if applicable.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.