beemurt
Posts: 16
Joined: Fri May 24, 2019 4:27 pm

Cant run python script using php on web browser?

Mon May 27, 2019 7:31 pm

Good Evening

I have been trying to make a simple webpage as a GUI to execute my python script(s). Using apache on Pi 3 B+. The script executes in the terminal fine, turning a philips hue bulb on or off. However I cannot get it to exectue from the webpage.

The php also writes out to a text file to record the status of the bulb, and this part works fine. It just wont execute the python. I am using a simple self processing form, with two html buttons; when on is selcted switch.py should run. When off is selected, switchOff.py should run.

After days of research I believe it may be an issue with permissions and users, but I still cant get it working. This might not even be the problem?

I have given www-data ALL=(ALL) NOPASSWD: ALL in the sudo visudo file.
The python scripts are located in the /var/www/html file along with all my php/html/txt files.
The python scripts have the shebang #!/usr/bin/env python

I have included images of the code and the file permissions etc.

Has anyone else has this problem? How did you fix it? Any and all help is welcome.
Attachments
sudo visudo.jpeg
sudo visudo.jpeg (54.15 KiB) Viewed 1071 times
lights.jpeg
lights.jpeg (97.2 KiB) Viewed 1071 times
file permissions.jpeg
file permissions.jpeg (96.29 KiB) Viewed 1071 times

User avatar
topguy
Posts: 6497
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: Cant run python script using php on web browser?

Tue May 28, 2019 9:18 am

Adding www-data as a "sudoer" should be (close to) the least preferred solution.

Do you need to use "sudo" when running the scripts in terminal ?
- If yes, why ? What resource needs superuser access for it to work ?
- If no, then making www-data a sudoer doesnt make sense.

Have you checked the apache logs for useful PHP errors ?

beemurt
Posts: 16
Joined: Fri May 24, 2019 4:27 pm

Re: Cant run python script using php on web browser?

Tue May 28, 2019 2:08 pm

Thank you for the reply Topguy

I wasnt aware of the apache errors log. (I am new to linux/raspberry pi)

There are a few errors noted. I was able to view the log via:

sudo cat /var/log/apache2/error.log in the terminal

This issue was nothing to do with my python or php or even the permissions. It was simply to do with my use of the philips hue bridge and bulb.
For some reason it was seeing the website as a new user and I had to go, press the bridge button and then click my html buttons on and off for the bridge to pick it up! Very simple!

Many thanks for your help

Bee

Return to “Python”