0

I'm trying to have a button on a webpage, hosted on my raspberry Pi, shutdown a QNAP via a script.

The script works when I run it from the pi command line. I've tested it. I can also see the html page is working, to a point where the key is added to the url when I click the button.

The script does not run when I press the button on the webpage.

The webpage is hosted using NGINX. This is my NGINX.config

http {
    server {
        listen <myip>:80;
        root /var/www/control;
        index index.html;

        server_name <myWebAddress> www.<myWebAddress>;

        location / {
            try_files $uri $uri/ =404;
        }
    }
}

This is my index.html page

<!DOCTYPE html>
<html style="font-size: 16px;">
  <head>
    <title>Home</title>
   </head>
  <body>
  <header>
  
 <?php
if ($_GET['poweroffqnap']) {
  exec("/etc/control/scripts/poweroffqnap.sh");
}
?>
  <h1>control</h1>
    </header>
    <section>
    <div>
        <a href="?poweroffqnap=true">shutdown QNAP</a>
      </div>
    </section>
  </body>
</html>

all folders and scripts are 755 and owned by root

I run 'ps aux' and see the following:

root     28221  0.0  0.1  49472  1172 ?        Ss   13:46   0:00 nginx: master process /usr/sbin/nginx -g daemon on; mas
www-data 28222  0.0  0.3  49628  3372 ?        S    13:46   0:00 nginx: worker process
www-data 28223  0.0  0.2  49628  2616 ?        S    13:46   0:00 nginx: worker process
www-data 28224  0.0  0.2  49628  2616 ?        S    13:46   0:00 nginx: worker process
www-data 28225  0.0  0.2  49628  2616 ?        S    13:46   0:00 nginx: worker process

I think this might be related to user permissions for www-data. I'm not sure how to do any further troubleshooting. I know how to add permissions for a user.

Is my code correct and how can I check what user the web-page us running with, to ascertain if the user can't execute the script?

pwzero
  • 63
  • 6
  • 1
    What **exactly** is not working so far? Is this a bash problem, a PHP problem, or a HTML problem? – Nico Haase Apr 07 '22 at 13:48
  • edited for clarification – pwzero Apr 07 '22 at 13:51
  • 1
    "The script does not run" - what does that mean? Is the script not run at all? Is the script run, but it cannot shutdown the server? Why not check the output of the script for any message? – Nico Haase Apr 07 '22 at 13:52
  • The script shutsdown the nas. if I run it from the command line, so I know it works. Nothing happens on the button click. Apologies, I thought that was clear. – pwzero Apr 07 '22 at 13:56
  • 1
    "Nothing happens" sounds strange, especially if you haven't checked **why** this is the case. Why not check whether the script outputs anything? Whether it asks for elevated permissions when run through PHP? – Nico Haase Apr 07 '22 at 13:56
  • That sounds sensible but that's the question. I don't know how to check the permissions are causing the problem. How would I check it run through php? – pwzero Apr 07 '22 at 13:59
  • Did you check https://www.php.net/manual/en/function.exec.php for details? There are parameters for `exec` that help you to gather the output. `system()` could also help, as it returns the output in a more simple way – Nico Haase Apr 07 '22 at 14:04
  • I edited sudoes with the following: `www-data ALL=(control) NOPASSWD: /etc/control/scripts/* control ALL=(ALL:ALL) NOPASSWD:ALL` and the html to the following: `` The page refreshes with the new url "http:///?poweroffqnap=true" but still nothing happens – pwzero Apr 07 '22 at 14:26
  • 2
    Did you try shell_exec (instead of exec) ? See this [link](https://stackoverflow.com/questions/11052162/run-bash-command-from-php) – Ken Lee Apr 08 '22 at 03:21

1 Answers1

0

This is my index.html page

Should it not be index.php ...

  • 1
    The correct answer is a combination of a couple of the comments and the answer. I hadn't setup php correctly in nginx, the index file did indeed need to be renamed .php, the correct command is `shell_exec` and the ownership of the script files needed to be amended as well as setting permissions using `sudo visudo` as I outlined myself earlier. – pwzero Apr 08 '22 at 13:59