Can anyone of you tell me why this doesn't work?
When i open the file doesnt run anything :(
This is my php file:
<!DOCTYPE html>
<html>
<head>
<title>Restarter V1.0</title>
<meta charset="UTF-8">
</head>
<body>
<?php
#Function Declaration
function testpy() {
exec("sudo python /var/www/html/reboot.py"); #just executes "reboot now"
}
#Get 'gotbut'
if (isset($_GET['gotbut'])) {
testpy();
}
?>
<script>
window.close(); <!--When i open it from my index.php-->
</script>
</body>
</html>
I have also added a line in my "/etc/sudoers" file with the following:
www-data ALL=(ALL) NOPASSWD: ALL
Please help :D
Thanks!
2 Answers 2
I see no reason for your configuration not to work, so I expect there's a silly problem sitting somewhere. I suggest you add the following lines to the <body>
of your index.php
:
<?php
echo "Current user is: " . get_current_user();
echo "Command output is " . shell_exec("sudo python /var/www/html/reboot.py");
?>
Hopefully the HTML output you'll get will provide enough insight into the problem you're having.
Aslo note that it's much more secure to have www-data ALL=NOPASSWD: /path/to/reboot.py
in sudoers, and locate the script in a place where your webserver doesn't have persmissions to replace it with whatever hackers will manage to upload. Otherwise you risk letting people executing random python code on your RPi with root permissions.
-
Thanks! It worked! I did somehow change the user that php runs on, and then i gave that user perms ;Ddnorhoj– dnorhoj2016年10月20日 10:58:49 +00:00Commented Oct 20, 2016 at 10:58
Pls dont give the php user perms to sudo. This is a highly security risk. The propper way is to add the file to your php config.
php_admin_value[open_basedir] = "/var/www/html/reboot.py:/usr/share/pear:/usr/share/php:/tmp:/usr/local/lib/php"
should look like this. In case you use fpm add this just to your pool config file.
-
1Could you explain how restricting PHP paths allows to use
reboot
withoutsudo
?Dmitry Grigoryev– Dmitry Grigoryev2016年10月21日 07:52:02 +00:00Commented Oct 21, 2016 at 7:52
/var/log/apache2/error.log
[Thu Oct 20 09:27:45.015801 2016] [:error] [pid 743] [client 192.168.0.19:55998] PHP Notice: Undefined index: closetab in /var/www/html/index.php on line 21, referer: http://192.168.0.13/?closetab=1