I have a simple Python (sockets.py) module which operates some Energenie sockets. See below. Note the print statements are simply there as a basic 'breakpoint' function so I can see how far the module runs.

When run from terminal, or from the Thonny editor it executes just fine.

When called from a simple PHP script it simply stops at line 'import energenie as e' and Apache reports an error (also below).

The PHP script (also below) runs without errors and appears to call the sockets.py module just fine, and 'import time' also fine.

The owner/group of both the PHP script and the Python module are root:root. The files are located in var/www/html folder.

sockets.py is as follows:

print('Hello, world! This proves that the call to Python is correct !!')

import time

print('This proves sockets is running past import time')

import energenie as e

print('This proves sockets is running past import energenie')









print('This proves sockets is running')

index.php is as follows:

                 <title>Python test</title>
<form action="<?=$_SERVER['PHP_SELF']?>" method="post"><input name="on" type="submit" value="On"></form>
                 if (isset($_POST['on'])){
                echo shell_exec("python /var/www/html/sockets.py");

The Apache Error is as follows:

Traceback (most recent call last):
  File "/var/www/html/sockets.py", line 1, in <module>
    import energenie as e
  File "/usr/local/lib/python2.7/dist-packages/energenie/__init__.py", line 2, in <module>
    from .energenie import switch_on, switch_off
  File "/usr/local/lib/python2.7/dist-packages/energenie/energenie.py", line 16, in <module>
    GPIO.setup(BIT1, GPIO.OUT)
RuntimeError: No access to /dev/mem.  Try running as root!

I am a newbie at Raspberry Pi and Python, and would really appreciate some help please.

All the best


  • What user is the Apache script running as? Presumably that user doesn't have the needed privileges. Print get_current_user.
    – joan
    Jan 24, 2021 at 16:20
  • The apache2 script runs as uid=www-data, gid=www-data. That user does NOT have root privileges (and it's a security exposure to give www-data root privileges). Try using a GPIO library that doesn't need that like @joan 's pigpio.
    – Dougie
    Jan 25, 2021 at 8:20
  • I have confirmed that Apache is running as as user www-data. As just starting out on a Pi have no experience of libraries such as @joans's pigpio ? where would I find it please?
    – Colin
    Jan 25, 2021 at 9:28

2 Answers 2


It should be possible BUT not if you use the (depreciated) Sysfs interface which requires root privileges.


Thanks for your help, I have solved the problem

First use a GPIO library, so typical Python code is:

from gpiozero import Energenie

import time

lamp = Energenie(1)


time sleep(2)


This simply turns a socket on and off in 2 seconds.

The Python file needs to be owned group:user www-data:www-data

Second add the Apache User (www-data in my case) to the gpio group i.e. usermod -a -G gpio www-data

This works fine from Apache, in my case from a simple PHP script that call the Python file.

Thanks for your help.

All the best


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.