nginx – PHP Not Executing from Webpage [but is fine from PHP-CLI] on a Raspberry Pi-ThrowExceptions

Exception or error:


  • Raspberry Pi
  • nginx web server
  • PHP5 & PHP-CLI installed

My Python script “” is really cool because it turns on/off lights in my living room through a relay connected to an Arduino Uno [then connected to the Pi via USB]. I know I could’ve used the Pi’s GPIO pins, I just didn’t. That doesn’t matter here anyway.

I wanted to be able to activate the script from a web browser through the site hosted by my Pi, so I have /var/wwww/test/lights.php containing this code:


Simple, no? Well, when I browse to that page in a browser, nothing shows up (expected) but the lights don’t change state (unexpected). However, at the command line, logged in as user Pi, I can run “php /var/wwww/test/lights.php” and it works just fine!

I imagine this is because nginx seems to use this user called www-data to do stuff, so maybe its a permissions issue? Now I’m wandering into unknown territory for me … I tried “su – www-data” to see if I could attempt the script as that user, but it asks for a password, which I never set up (and a blank password didn’t work).

Any help here is greatly appreciated.

UPDATE – Here is “ls -la /var/www/test/lights*”

-rw-r--r-- 1 www-data root  37 Feb  1 23:56 /var/www/test/lights.php
-rwxr-xr-x 1 www-data root 129 Feb  1 23:51 /var/www/test/


pi@raspberrypi ~ $ sudo su - www-data
$ pwd
$ php ./test/lights.php
python: can't open file '': [Errno 2] No such file or directory
$ python ./test/
Traceback (most recent call last):
  File "./test/", line 4, in <module>
    ser = serial.Serial('/dev/ttyACM0', 9600)
  File "/usr/local/lib/python2.7/dist-packages/serial/", line 260, in __init__
  File "/usr/local/lib/python2.7/dist-packages/serial/", line 276, in open
    raise SerialException("could not open port %s: %s" % (self._port, msg))
serial.serialutil.SerialException: could not open port /dev/ttyACM0: [Errno 13] Permission denied: '/dev/ttyACM0'

That ‘/dev/ttyACM0’ device is my Arduino connection, so it appears the user “www-data” doesn’t have access to run that Python script, since it can’t output to that file. Am I able to “chmod” that device?

THIRD UPDATE – I feel like we’re almost at the end!

Using “ls -g /dev/ttyACM0” I found that it is owned by group “dialout.” Using “grep dialout /etc/group” I found that only user “Pi” is assigned to it. Therefore, I added www-data to that group with “usermod -a -G dialout www-data”

Now, check it:

pi@raspberrypi ~ $ sudo su - www-data
$ cd test
$ php lights.php
$ python

Both the PHP and Python script work, and the lights went on and off! BUT loading the web page “lights.php” from a browser still doesn’t do anything!

I changed the PHP file to this:


exec('python', $output, $return);

print "Output:";
print $output;
print "<br />";
print "Return:";
print $return;


From what I gather, that’s the proper way to get some debug info from the exec() statement. Here’s the output when I refresh the webpage:


Does that help me at all?

How to solve:

PHP is probably configured to run in safe mode in your website configuration. When running in safe mode, some features like exec are disabled. Try disabling safe mode.

Another possibility is that the script cannot be found in the directory where your web server is running. To solve that, use the full path to your script, like exec("/usr/bin/python /home/armani/").

Another possibility is that the web server is running as a different user as yourself when you are logged (probably as nouser). And this user may not have access to some of the resources.

And there are probably many more such possible errors. Please provides more information on the error you get (try enabling debug in PHP to get a traceback).


None of the given answers helped me… in fact, my updates to my posts log what was ultimately the answer, which I found through my own trial/error and Googling.

I needed to assign user www-data into the dialout group in order to have access to /dev/ACM0, and a reboot was required (of the whole server, not just nginx for some reason).

I don’t know why everybody kept answering that this was a path issue, WHEN I KEPT SAYING THE PYTHON SCRIPT RAN FINE FROM SHELL, AND THE PHP FILE RAN FINE FROM PHP-CLI.

Anyway, that’s what solved it in the end. Thanks to everyone for trying.


What’s the chmod of your file ?
Could you try the followind command ?

chmod +x


Please check if your paths are correct, the php script will be run from inside the /var/wwww/test directory. Is the file also located in that directory? If not, specify the full path to the file


You will have to check which is the current working directory and if it is not the one you want change to it or even simpler just execute with python the script with full path

exec(‘python /var/www/test/’);

Leave a Reply

Your email address will not be published. Required fields are marked *