树莓派GrovePi通过PHP网页控制LED遇IOError: [Errno 13]权限拒绝求助
Hey there, let's figure out how to fix that IOError: [Errno 13] Permission denied you're facing when trying to control a GrovePi LED with PHP on your Raspberry Pi. This is a super common snag when web server processes try to access hardware resources, so let's walk through the solutions step by step:
Add the web server user to hardware access groups
PHP runs under the web server's user account (usuallywww-datafor Apache, ornginxif you're using Nginx). This user doesn't have default access to GPIO or I2C interfaces needed by GrovePi. Add them to the required groups with these commands:sudo usermod -aG gpio www-data sudo usermod -aG i2c www-dataThen restart your web server to apply the group changes:
# For Apache sudo systemctl restart apache2 # For Nginx sudo systemctl restart nginxCheck and adjust permissions for hardware devices
Verify the permissions of the I2C device (GrovePi uses I2C) with:ls -l /dev/i2c-1It should show the group as
i2c. If not, fix the ownership and permissions:sudo chown root:i2c /dev/i2c-1 sudo chmod 660 /dev/i2c-1This ensures members of the
i2cgroup (includingwww-data) can read/write to the device.Ensure GrovePi script/files are accessible to the web server
If your PHP code calls a Python script (common since GrovePi's official library is Python-based), make sure the script and its parent directories are readable and executable bywww-data:# Adjust the path to match your GrovePi script location sudo chmod -R 755 /home/pi/grovepi # Also make sure the pi user's home directory is accessible sudo chmod 755 /home/piTest the script directly as www-data
To rule out PHP-specific issues, switch to thewww-datauser and run your GrovePi LED script manually:sudo -u www-data python3 /home/pi/grovepi/led_control.pyIf this throws the same permission error, the problem is with the script/hardware permissions, not your PHP code. If it works, then check your PHP code for any mistakes in how it calls the script (like incorrect paths).
Never run PHP as root (critical security note)
It might be tempting to fix permissions by running the web server as root, but this is a huge security risk. Stick to the group-based permission fixes above to keep your system safe.
Most of the time, this error comes down to the web server user lacking access to the hardware interfaces or GrovePi files. Following these steps should get your LED control working smoothly without compromising security.
内容的提问来源于stack exchange,提问作者tomassilny

