Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9245341
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:14:46+00:00 2026-06-18T09:14:46+00:00

Setup: Raspberry Pi nginx web server PHP5 & PHP-CLI installed My Python script lights.py

  • 0

Setup:

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

My Python script “lights.py” 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:

<?php
 exec('python lights.py');
?>

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/lights.py

SECOND UPDATE – Check it:

pi@raspberrypi ~ $ sudo su - www-data
$ pwd
/var/www
$ php ./test/lights.php
python: can't open file 'lights.py': [Errno 2] No such file or directory
$ python ./test/lights.py
Traceback (most recent call last):
  File "./test/lights.py", line 4, in <module>
    ser = serial.Serial('/dev/ttyACM0', 9600)
  File "/usr/local/lib/python2.7/dist-packages/serial/serialutil.py", line 260, in __init__
    self.open()
  File "/usr/local/lib/python2.7/dist-packages/serial/serialposix.py", 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 lights.py
$

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!

FOURTH UPDATE
I changed the PHP file to this:

<?php

exec('python lights.py', $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:

Output:Array
Return:1

Does that help me at all?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T09:14:47+00:00Added an answer on June 18, 2026 at 9:14 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Setup: firebirdsql 2.1.3 win32, php 5.3 (installed with xampp) When I try to update
Setup: Python script A inserts data to a database every 15 minutes Python script
SETUP: Using Google Apps Script's UI (doGet) with tabPanel option. At the bottom of
Setup iMac 10.7.4, Eclipse Indigo, Pydev, Python 2.7 I’m trying to install the Tweepy
Setup OSX 10.7.4, Eclipse, Pydev, Python 2.7, Tweepy-1.9 Hey, I was originally trying to
I am just starting to learn python and using it with my raspberry pi
Setup TFS 2010 on a pretty oldish server (actually an oldish desktop machine running
Setup: Windows Server 2003 / SQL Server 2005. ASP.NET 2.0. IIS 6. The site
Setup: ASP.net 3.5, Linq-to-Sql. Separate Web and DB servers (each 8-core, 8GB RAM). 4
Setup: Multiple web servers with synchronized forms authentication. Multiple asp.net Applications running on these

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.