Permission error when calling python script from php via browser[SOLVED]

Are there any advice to make php-script run python-script with “exec” function without being a “root” user?

I’m a user of Grove Pi+ on Raspberry Pi3 /w Raspbian OS.

When I call a simple python-script that blinks LED once, from a PHP-script via browser, it gives me a permission error such as below.

Traceback (most recent call last): File "grove_blink.py", line 6, in from grovepi import * File "/var/www/html/grovepi.py", line 64, in bus = smbus.SMBus(1) IOError: [Errno 13] Permission denied

To isolate the problem I ran this PHP-script via command line, and it worked fine.
> pi@raspberrypi:/var/www/html $ php grove_blink.php

Also tried a direct call from PHP command line, and worked fine.
> pi@raspberrypi:/var/www/html $ php -r "exec('python ./grove_blink.py');"

Naturally, if I run the python-script, that the php-script above calls, worked fine too.
> pi@raspberrypi:/var/www/html $ python grove_blink.py

So, I assumed running this php-script via browser causes the problem.

I did some research and I understood that I needed to be a “root” or “pi” user to run, since the apache web server is a “www-data” user. It made sence.

So I called this python script with “sudo”, then I get an error such as below:

We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:

 #1) Respect the privacy of others.
 #2) Think before you type.
 #3) With great power comes great responsibility.

sudo: no tty present and no askpass program specified

It seems that I have to give the password but that’s what I don’t want, nor giving a “ALL=NOPASSWD” setting to sudoers.

Also I checked the “/dev/i2c*” permission and the owner was “root” with “i2c” group.

So the next step I took was adding user “www-data” to “i2c” group and “gpio” group.
But it didn’t work.

Are there any advice to make php-script run python-script with “exec” function without being a “root” nor “pi” user?

Self solved: After adding user “www-data” to "i2c2 and “gpio” groups, simply I had to reboot raspbian to take effect

Too many hours of research and try and error, but simple it was. Sorry for the fiddle-faddle.

Now I can use my old-php-libraries for making a web interface!
I’m really enjoying Raspberry Pi because of Grove Pi+! much obliged!

Like all IT support in the world: "have you tried rebooting? " :slight_smile:

Nice to see a mesh-up of PHP and Python nicely cooperating. What’s your project about?

Cleo

> Like all IT support in the world: “have you tried rebooting? ” ?

Precisely. =)

It’s still not a project level, though I’m tying to make a toy for my niece.

As a start I made a simple php+jQuery based web page with buttons, and when I push them it runs examples of GrovePi+’s manual. Simple.
Now I’m trying to make an simple Android app for Kindle fire that shows a web page.

My goal is to make my niece toys move, such as windmill or lighting up rooms in her small toy world. =)