Home > Net >  php web page not executing script
php web page not executing script

Time:10-14

I am very new to php and although with some 'diy' coding experience I am largely a novice! As such, I have a very simple problem...

Basically I have used a raspberry pi loaded with Raspberry Pi OS to create a mini media server in the car for the kids on long road trips. However, when the car is switched off it immediately cuts power to the raspberry pi and this is mostly not a problem but recently corrupted the system which I have since fixed.

But because of this I have decided to add a small script that allows a connected device to open a web page and click a button to shutdown the pi before cutting power. I am using lighttpd as the web server and I have the basics working meaning that the index.php page loads when I put in the IP address and a button is present. My problem is that when I click the button, instead of the script being executed, the contents of the script are printed on the screen?!!

To add, I have followed the solution listed in the link below: https://superuser.com/questions/829243/how-to-shut-down-a-server-from-the-webinterface

Additionally, I have changed the script to bash and PHP unsuccessfully (same result). I have also changed the index from a basic HTML page to a PHP script as well. Currently, the following is the script I am using to generate the button:

<?PHP

?><!DOCTYPE html>
<html lang="en">
  <body>
    <form method="post" action="shutdown.sh">
        <input type="submit" value="Submit" name="Submit">
    </form>
  </body>
</html>

The script is:


#!/bin/bash
echo $("sudo shutdown now");

www-data is the user with root privileges in visudo

And as mentioned the bash script has been edited from the original python example and I tried PHP...

Note too: both files are in the same directory, but I did have them separated in a sub-directory for the shutdown script (as in the example)...

All help is appreciated!!

CodePudding user response:

Your web server is probably not configured to execute shell scripts.

However, you mention that you have also tried the script in a php version.

I think your web server is not configured to execute php scripts. Check the source of the page with the button in your browser by clicking ctrl u or cmd u, if it shows <?PHP ?> is has not been properly configured.

You do not write which os you have installed. But maybe this guide will help you https://wiki.ubuntu.com/Lighttpd PHP or this answer https://raspberrypi.stackexchange.com/questions/1164/how-to-install-lighttpd-with-php

CodePudding user response:

I can't fully say what worked as I have done a few things further. I am not convinced that the server execution of PHP and bash scripts was the issue as I had previously changed the permissions of both files. However it is not impossible and therefore for anyone with similar problems, the following is worth trying:

Change the directory owner and group sudo chown www-data:www-data /var/www

allow the group to write to the directory sudo chmod 775 /var/www

Add the pi user to the www-data group sudo usermod -a -G www-data pi

On completing this, then the following was my code that actually worked:

index.html:

<html lang="en">
  <body>
    <form method="post" action="shutdown.php">
        <input type="submit" value="Submit" name="Submit">
    </form>
  </body>
</html>

shutdown.php:


echo shell_exec("sudo shutdown -h now");

?>```


Make sure they are both executable files (sudo chmod  x ....)
  • Related