You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

树莓派GrovePi通过PHP网页控制LED遇IOError: [Errno 13]权限拒绝求助

Fixing Permission Denied Error for GrovePi LED Control via PHP on Raspberry Pi

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 (usually www-data for Apache, or nginx if 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-data
    

    Then restart your web server to apply the group changes:

    # For Apache
    sudo systemctl restart apache2
    # For Nginx
    sudo systemctl restart nginx
    
  • Check and adjust permissions for hardware devices
    Verify the permissions of the I2C device (GrovePi uses I2C) with:

    ls -l /dev/i2c-1
    

    It 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-1
    

    This ensures members of the i2c group (including www-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 by www-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/pi
    
  • Test the script directly as www-data
    To rule out PHP-specific issues, switch to the www-data user and run your GrovePi LED script manually:

    sudo -u www-data python3 /home/pi/grovepi/led_control.py
    

    If 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 09:42:03