I am using Linux on the Pi, in-which I installed Apache, Python, and mod_python for integrating python with Apache.

After running server it can run simple hello world program But after writing program for accessing GPIO pin it shows below error:

I type this url into the browser

This is my code in test4.py file:

import RPi.GPIO as GPIO

def LED(req):
        print "Led on"

Below is error in the browser:


ProcessId:      2704
Interpreter:    ''

ServerName:     ''
DocumentRoot:   '/var/www'

URI:            '/test4.py/LED'
Location:       None
Directory:      '/var/www/'
Filename:       '/var/www/test4.py'
PathInfo:       '/LED'

Phase:          'PythonHandler'
Handler:        'mod_python.publisher'

Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/mod_python/importer.py", line 1537, in HandlerDispatch
    default=default_handler, arg=req, silent=hlist.silent)
  File "/usr/lib/python2.7/dist-packages/mod_python/importer.py", line 1229, in _process_target
    result = _execute_target(config, req, object, arg)
  File "/usr/lib/python2.7/dist-packages/mod_python/importer.py", line 1128, in _execute_target
    result = object(arg)
  File "/usr/lib/python2.7/dist-packages/mod_python/publisher.py", line 204, in handler
    module = page_cache[req]
  File "/usr/lib/python2.7/dist-packages/mod_python/importer.py", line 1059, in __getitem__
    return import_module(req.filename)
  File "/usr/lib/python2.7/dist-packages/mod_python/importer.py", line 296, in import_module
    log, import_path)
  File "/usr/lib/python2.7/dist-packages/mod_python/importer.py", line 680, in import_module
    execfile(file, module.__dict__)
  File "/var/www/test4.py", line 6, in <module>

RuntimeError: No access to /dev/mem.  Try running as root!

Accessed:       Tue Dec  8 20:21:29 2015
Generation:     0

_mp_0dc35a1ecc84e788f45a0f43ed4d3629 {
  FileName:     '/var/www/test4.py'
  Instance:     1 [IMPORT]
  Generation:   0 [ERROR]
  Modified:     Tue Dec  8 19:21:44 2015
  • 3
    The user running your webserver (and test4.py file - likely www-data does not have root access). You need root access to access the GPIO pins. You can work around this by creating a new rule in the /etc/sudoers file that allows (www-data to run the GPIO command without a password). Note you need to edit this file with visudo to prevent locking yourself out of the system because of a bad edit. You can verify this is the problem by dunning test4.py in the terminal with sudo: sudo python test4.py if the LED turns on you know this is the problem Dec 8, 2015 at 20:54
  • 2
    You can also change the way you call the GPIO code. If you create a new file with the GPIO code in it (led.py) with the above code in it. Then change test4.py to call this file with sudo - sudo python led.py. You may need to use the full path to the python binary and your script. This ,ay also need a sudoer rule such as this www-data ALL=(ALL) NOPASSWD: ALL again to avoid locking yourself out of the system only edit this file with visudo Dec 8, 2015 at 21:05
  • @SteveRobillard - that probably counts as 2 answers :) However if any sort of security is needed, it would be a great idea to specify which scripts to allow running as root with sudo via specifying them to NOPASSWD instead of allowing everything.
    – Wilf
    Dec 9, 2015 at 0:59
  • @Wilf Good point, though that would have made my comments even denser and if and when I hear back from the OP that this works.I will address the security issue. Dec 9, 2015 at 1:03
  • 1
    @SteveRobillard: Is there a reason you posted that as a comment and not an answer?
    – Jacobm001
    Dec 9, 2015 at 3:56

1 Answer 1


You don't so much need root access as permission to access the gpio device. Raspbian has a user group 'gpio' to enable this. By default the 'pi' user is in the gpio group and can access GPIOs. If you add the apache user (www-data normally) to the gpio group then the web server will be able to access the GPIOs without requiring root access. You can do that with usermod -a -G gpio www-data

You may need to restart the apache service to pickup the new group ID once you make this change (or reboot the machine). Note there is also an 'spi' group and a 'i2c' group.

This is actually achieved by the python code trying to use /dev/gpiomem first (see the code at line 72) which has read-write access to the gpio group. If that fails it tries /dev/mem which requires kmem group access or root for read-write.

  • Thanks, this solved the problem for me and saved having to do anything silly with root. May 19, 2016 at 22:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.