Executing a Shell Script from a PHP Script

How to execute shell script using php?

Try this. It issues the command in the same command as the login as I mentioned in my comment above:

 $output1 = shell_exec('service asterisk_ipu_gui start');
$output2 = shell_exec('asterisk -vvvr "manager show connected"');

$output2 should hold your expected results now.

Also, try to add your www-data user to asterisk group, this may be due to permissions and ownership issues. If you can do it locally with 127.0.0.1, that means your local user is allowed to do it and it works, but when php tried via www-data, it fails.

Executing a shell script from a PHP script

I would have a directory somewhere called scripts under the WWW folder so that it's not reachable from the web but is reachable by PHP.

e.g. /var/www/scripts/testscript

Make sure the user/group for your testscript is the same as your webfiles. For instance if your client.php is owned by apache:apache, change the bash script to the same user/group using chown. You can find out what your client.php and web files are owned by doing ls -al.

Then run

<?php
$message=shell_exec("/var/www/scripts/testscript 2>&1");
print_r($message);
?>

EDIT:

If you really want to run a file as root from a webserver you can try this binary wrapper below. Check out this solution for the same thing you want to do.

Execute root commands via PHP

Run bash script from PHP script

I found what the issue was. It was because of Linux SELinux feature. This feature applies a least-privilege policy and denies any unnecessary command from running on Linux. The bash script is running successfully after disabling this feature. To do so, edit the file /etc/selinux/config and change SELINUX=enforcing to SELINUX=disabled and reboot the system. THIS IS NOT RECOMMENDED FOR SECURITY REASONS, however. You may check the link below to only create some exceptions rather than completely disabling SELinux.

https://wiki.centos.org/HowTos/SELinux

Running a shell script in the background from PHP

Your number 5 approach is close but you need to combine it with 6 like this

shell_exec("/script.sh > run.log 2>&1 &");

Or redirect to /dev/null like this

shell_exec("/script.sh > /dev/null 2>/dev/null &")

It appears that the PHP development server cannot fork off commands properly. Using a real web server like Apache or NGinx will resolve the issue.

run shell script from php as a specified user

I think i found a solution ( but still not sure about the security issues).

It only needs to change the default user (owner) and group of the httpd service. This can be done by editing the httpd.conf located in /opt/lampp/etc (if you are using Xampp). The default user, as I mentioned in the question, is daemon. However it has not permissions to run sudo commands, so it only needs to change that user by another one who has the permissions to run sudo commands (obviously the root user or your deafult user in Ubuntu).



Related Topics



Leave a reply



Submit