Running a python program from a PHP webpage produces RuntimeError

I have a LAMP server set up on my Jetson Nano that can run webpages. I am trying to create a webpage in PHP that can run a python program that turns on an LED at a given duty cycle for a certain number of seconds. Here is the python code being run:

#!/usr/bin/env python

import Jetson.GPIO as GPIO
import sys
from time import sleep

print("hello")

if(len(sys.argv) > 2):
	try:
		dc = int(sys.argv[1])
		delay = int(sys.argv[2])
		GPIO.cleanup()

		GPIO.setmode(GPIO.BOARD)
		my_pwm2=GPIO.PWM(33,60)
		my_pwm2.start(dc)
		sleep(delay)
		GPIO.cleanup()
	except Exception as e:
		print(e)

Here is the code for the PHP web page:

<html>
<head>
<title>PWM Test</title>

<?php
$result = exec("python3 programs/pwm_cmd.py 30 4 2>&1");

echo $result;
?>
</head>
<body>

</body>
</html>

The python code runs fine from the shell. However, when I run the php code, the webpage displays the following message:

RuntimeError: The current user does not have permissions set to access the library functionalites. Please configure permissions or use the root user to run this

Here is a screenshot:

I have determined that the problem must lie within the Jetson GPIO library, because when I disable all code relating to GPIO functions, the page displays the word ‘hello’ as I intended.

Any help into this matter would be greatly appreciated

hello tomas,

may I know what’s the actual use-case,
it needs root permission for running GPIO controls.

Hi Jerry,

Sorry for the late reply. How do I determine the use-case?

hello tomas,

just wondering why you need to control LED by webpage.
according to the error messages, it needs root permission for running GPIO controls.
you may try to grant root permission for your webpage implementation.
thanks

Hello Jerry,

I’m trying to create a product where the user can run programs without having to log directly into the Jetson, hence why I want to try and control the GPIO through a webpage.

I tried adding www-data (the user running the server) to the ‘gpio’ group, but that didn’t work.

I also tried creating a binary wrapper as outlined here: apache - Execute root commands via PHP - Stack Overflow

Now when I run the compiled c code through php, I get no output. I can run the code through the command line just fine however.

hello tomas,

since there’s default pin configuration, please check developer guide, Configuring the 40-Pin Expansion Header.
please have a try with Jetson-IO tool to simplify the configuration of the I/Os exposed by the 40‑pin expansion header.
after that, you might alter the pin from user-space.
thanks

I already did the configuration to use the pin as PWM. Is there something else specifically that I need to do?

hello tomas,

are you able to execute the command, $ python3 programs/pwm_cmd.py 30 4; via a shell without root permissions?
if not, you should also expect that’s not works by PHP either.

BTW,
since it needs root permission for running GPIO controls.
it’s not a good idea to allow anyone to control GPIO pins from a webpage.
thanks