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>
        <title>Restarter V1.0</title>
        <meta charset="UTF-8">
            #Function Declaration
            function testpy() {
                exec("sudo python /var/www/html/reboot.py"); #just executes "reboot now"

            #Get 'gotbut'
            if (isset($_GET['gotbut'])) {
            window.close(); <!--When i open it from my index.php-->

I have also added a line in my "/etc/sudoers" file with the following:


Please help :D

  • Have you checked your Apache/Web server logs to see what error it is getting?
    – Ian M
    Oct 20, 2016 at 9:47
  • No where is it located? (i am pretty new to those things)
    – dnorhoj
    Oct 20, 2016 at 9:48
  • If you are running apache2 /var/log/apache2/error.log
    – Ian M
    Oct 20, 2016 at 9:48
  • thanks, it says: [Thu Oct 20 09:27:45.015801 2016] [:error] [pid 743] [client] PHP Notice: Undefined index: closetab in /var/www/html/index.php on line 21, referer:
    – dnorhoj
    Oct 20, 2016 at 9:49

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:

    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 ;D
    – dnorhoj
    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.

  • 1
    Could you explain how restricting PHP paths allows to use reboot without sudo? Oct 21, 2016 at 7:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.