What's the best way of doing stuff with the GPIO in PHP


7 posts
by AforAlex » Fri Jun 08, 2012 12:13 am
Hi,

I've reading a number of different threads, and wondering what the best (and simplest) way of accessing the GPIO stuff is in PHP.

From what I can see the biggest problem with PHP and GPIO is that in order to access the GPIO stuff you have to be running as root, which is not a great idea as far as security is concerned.

A) I've seen one thread which is running shell commands from the PHP page (viewtopic.php?f=34&t=7252) but I have tried to follow what they have done but its not worked ... or more accurately i haven't been able to follow what they have done.

B) Get the PHP page to write a flag where it can, and then have a separate programme running as root that acts on this flag (http://www.instructables.com/id/Control ... r-t/#step1)

C) I have had some success using the GPIO through Python (running as root), is it possible to run the relevant Python programme from the PHP page? I have tried this but have not had much luck, not getting any error messages, just doesn't seem to be running the Python (I think its a root issue again)

I've had a bit of a tinker and not got any of them to work, I was a complete linux newbie until I got Raspberry Pi so still learning, I don't think I have gotten my head around all the commands, so if someone is able to offer me help please don't just tell me to "Alter the php files security so that it runs under user xxxx" instead it would be brilliant if you could mention the files, commands and syntax that I would need to use.

I'm running the Debian Squeeze distribution of Linux btw

Many thanks from anyone that is able to offer a bit of help
Posts: 74
Joined: Thu May 31, 2012 7:56 pm
by dph » Fri Jun 08, 2012 6:37 pm
First off, I'm not sure why running as root is such a big deal, I assume people aren't using these for any kind of production-level purposes. If it's a huge deal, maybe you can set up private groups and allow a particular group to have access to the gpio? http://wiki.debian.org/UserPrivateGroups

Also, unless you're doing something with the web, or i guess even IF you do anything with the web, I'm not sure why you would prefer PHP. If you have luck with Python, then just use Python. I'm pretty certain the simplest way to access the gpio isn't through PHP. You set and reset those pins by simply writing to a file, it's pretty straight forward to do that via the command line. Check out this page http://www.instructables.com/id/Control ... l-Outputs/
Posts: 5
Joined: Fri Jun 08, 2012 6:26 pm
by AforAlex » Sat Jun 09, 2012 5:48 pm
Cheers, at the moment I've gone for the very simple option of getting PHP to write to a file, and then having a python script running in the back group that looks for this file and then takes action depending on whether it finds it or not.

Still not quite got my head around what the best way forward to do this type of things is ... but for now this is my proof of concept (http://aforalex.no-ip.biz/led_with_video_external.php) - I can't say how long it will stay up for, but i'll try and keep it there for a couple of days.

http://thingswatihavedonewithmyraspberr ... pot.co.uk/
Posts: 74
Joined: Thu May 31, 2012 7:56 pm
by WPsites » Sun Jun 10, 2012 12:08 pm
A nice little article here on controlling an Arduino via serial using PHP which should give you some pretty good pointers on how you may be able to do it with PHP directly.

http://wanderr.com/jay/controlling-ardu ... 010/12/28/
Posts: 17
Joined: Sun Jun 10, 2012 11:05 am
Location: Southport, Merseyside, UK
by gordon@drogon.net » Mon Jun 11, 2012 5:15 pm
AforAlex wrote:Hi,

I've reading a number of different threads, and wondering what the best (and simplest) way of accessing the GPIO stuff is in PHP.

From what I can see the biggest problem with PHP and GPIO is that in order to access the GPIO stuff you have to be running as root, which is not a great idea as far as security is concerned.

A) I've seen one thread which is running shell commands from the PHP page (viewtopic.php?f=34&t=7252) but I have tried to follow what they have done but its not worked ... or more accurately i haven't been able to follow what they have done.


PHP doesn't really have the language primitives to do low level control stuff like this, and while I've written 1000's of lines of PHP, I've never written any modules to alter the language to make it use low-level hardware, etc.
However.. You can get PHP to execute an external program, and if that program can toggle GPIO pins, or read them (and you get the output back via popen), then you can do it.
And it just so happens that I've written such a program, scorned, scoffed and laughed at by many ;-) but it's there and it works. You just need to fetch and install it.
However it's not very efficient, as it requires a fork() every time you want to use it - but at the end of the day if you're just toggling a few pins for a simple web request you'll be fine.
https://projects.drogon.net/raspberry-pi/wiringpi/
You need the 'gpio' program, but you need to compile and install the wiringPi library first.
-Gordon
--
Gordons projects: https://projects.drogon.net/
User avatar
Posts: 1508
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
by romilly » Mon Jun 11, 2012 5:54 pm
dph wrote:First off, I'm not sure why running as root is such a big deal, I assume people aren't using these for any kind of production-level purposes. If it's a huge deal, maybe you can set up private groups and allow a particular group to have access to the gpio? http://wiki.debian.org/UserPrivateGroups


There are two reasons why running as root is discouraged.

One - it's a dangerous habit to get into. As root it's all too easy to wipe your SD card. While you man not be in production, you might be a tad annoyed at losing all your programs and data.

Two - if you're running a web server that's public visible, and the web server is running as root, and it gets hacked, and it's on a network, you could be in a lot of trouble. I know that's a lot of 'ifs', but the threat is serious.

There are solutions out there that use private groups to avoid the problem. Gordon describes one solution below. You might also take a look at http://quick2wire.com/2012/05/safe-cont ... pberry-pi/ and http://quick2wire.com/2012/05/quick2wir ... -released/
Posts: 10
Joined: Fri Dec 02, 2011 7:02 am
by plugwash » Mon Jun 25, 2012 5:33 pm
AforAlex wrote:Hi,

I've reading a number of different threads, and wondering what the best (and simplest) way of accessing the GPIO stuff is in PHP.

Best and simplest are probablly not the same.

The best way IMO is to write a daemon in a suitable programming language (afaict PHP is NOT a good choice for daemons), the daemon you write understands the hardware you have hooked up and keeps track of it's state. Then the php scripts run by the web server talk to the daemon (say over a unix domain socket) to request information from it and give it instructions.

While it's possible to access the hardware directly from php code it's probablly not a good idea design wise as it gives you no way to do background servicing of your hardware and no easy way to resolve conflicts between multiple simultanious requests.
Forum Moderator
Forum Moderator
Posts: 2231
Joined: Wed Dec 28, 2011 11:45 pm