PHP on Raspberry Pi

Lead Image © Thatchakon Hin-ngoen,

Second Language

Getting started with PHP on Raspberry Pi is easy. We show how to build a simple PHP app to control an LED.

Python and Scratch are by far the most popular options for programming Raspberry Pi. With a bit of work, however, you can teach Raspberry Pi to understand other languages, including PHP. You might want to use PHP on Raspberry Pi for several reasons. If you've already built a PHP-based app, and you want to integrate it with Raspberry Pi, for example, it makes sense to stick to PHP. Perhaps you are more familiar with writing code in PHP than Python, or maybe you just want to give another programming language a try.

Before you can start working with PHP on Raspberry Pi, you need to install PHP itself on the machine. In most situations, PHP is used in tandem with a web server like Apache. Installing both PHP and Apache on Raspberry Pi is a matter of running:

sudo apt-get install apache2 php5

Instead of Apache, you can install a more lightweight server like lighttpd using the

sudo apt-get install lighttpd php5


Building Bridges

Probably the easiest way to use PHP with Raspberry Pi is through the shell_exec() function. This function lets you execute shell commands, so it can act as a sort of bridge between PHP and the Raspberry Pi. In the most simple case, shell_exec() can call Python scripts that perform certain tasks and control GPIO pins.

Another approach is to deploy the Wiring Pi library [1] for working with GPIO pins and then use the library with PHP via the shell_exec() function. For this solution to work, you need to install Wiring Pi on Raspberry Pi first. The library is not available as a binary package, so you need to compile and install it from the source. Fortunately, this process is relatively straightforward. Start with installing the Git software using:

sudo apt-get install git-core

Then, clone the Wiring Pi Git repository by running

git clone git://

Switch to the resulting wiringPi directory and use the ./build command to compile and install Wiring Pi:

cd wiringPi

To make sure Wiring Pi is installed and works properly, run the gpio -v command; it should return the current version of Wiring Pi along with the basic Raspberry Pi info. Next, execute the gpio readall command to view a detailed GPIO layout diagram.

To put Wiring Pi to practical use, I'll build a super-simple PHP app for controlling an LED. Connect an LED with a resistor to GPIO pin 17 and GND as shown in Figure 1. Open the terminal on your Raspberry Pi (or connect to it via SSH) and switch to the /var/www directory. Then, use the sudo nano gpio.php command to create the gpio.php file for editing. Place the code in Listing 1 (adapted from the Raspberry Pi Tutorials website [2]) in the file.

Listing 1

Simple PHP App to Control an LED

01 <html>
02 <head>
04 <title>LED Control</title>
05 </head>
06         <body>
07         LED Control:
08         <form method="get" action="gpio.php">
09                 <input type="submit" value="ON" name="on">
10                 <input type="submit" value="OFF" name="off">
11         </form>
12         <?php
13         $setmode17 = shell_exec("/usr/local/bin/gpio -g mode 17 out");
14         if(isset($_GET['on'])){
15                 $gpio_on = shell_exec("/usr/local/bin/gpio -g write 17 1");
16                 echo "LED is on";
17         }
18         else if(isset($_GET['off'])){
19                 $gpio_off = shell_exec("/usr/local/bin/gpio -g write 17 0");
20                 echo "LED is off";
21         }
22         ?>
23         </body>
24 </html>
Figure 1: LED wiring diagram.

The key element of the app is an HTML form containing ON and OFF buttons. When you press one of the buttons, its value is passed as a part of the URL (gpio.php?on=ON and gpio.php?off=OFF). GPIO pin 17 is controlled by PHP code that uses the shell_exec() function. The statement in line 13 sets the mode of the pin to out; the code block in lines 14-21 reads the current value from the URL and uses the shell_exec() function to turn the pin on and off.

Now, point the browser to (replace with the actual IP address of the Raspberry Pi) and use the buttons to turn the LED connected to the Raspberry Pi on and off (Figure  2).

Figure 2: Behold the simple PHP app in all its bare-bones glory.

If the buttons don't work, most likely the web server doesn't have appropriate rights to execute shell commands. To fix this, run the sudo visudo command and add the following line to the sudoers file:


Also, make sure that the /var/www directory belongs to the www-data user and group (use sudo chown -R www-data:www-data /var/www to set the correct owner).

Even this very simple PHP app can be put to several uses with a minimum of tweaking. For example, I use a simple transistor switch connected to Raspberry Pi to control my film SLR camera [3]. The original solution used a simple Python script to control the switch. To run the script, I had to do this via an SSH connection, which wasn't very practical in many situations. So, I modified the example PHP script for use with the transistor switch (Listing  2).

Listing 2

PHP App to Control a Transistor Switch

01 <html>
02 <head>
04 <title>Trigger</title>
05 </head>
06         <body>
07         Trigger switch:
08         <form method="get" action="switch.php">
09                 <input type="submit" value="Trigger" name="switch">
10         </form>
11         <?php
13         if(isset($_GET['switch'])){
15                 sleep (0.5);
17                 echo "Done!";
18         }
19         ?>
20         </body>
21 </html>

All I had to do was remove one of the buttons and modify commands in the if condition to turn on the switch, wait for 0.5 seconds (so the camera registers the signal), and then turn off the switch. Now, I can use the PHP app to trigger my camera from any computer and mobile device.

Taking the gpio-php Route

Although using Wiring Pi via shell_exec() calls offers an easy way to control GPIO pins in PHP scripts, it's not the only option at your disposal. The php-gpio project [4], for example, provides a dedicated PHP library for accessing GPIO pin on Raspberry Pi. To get started with php-gpio, use the following commands to install the library and the accompanying files into the /home/pi directory:

php composer.phar create-project --stability='dev'ronanguilloux/php-gpio

php-gpio uses a handful of simple API calls to set GPIO pins and change their states from a PHP script. To enable this functionality, however, the script must contain the following statements that load the library and set up a GPIO pin:

require 'vendor/autoload.php';
use PhpGpio\Gpio;
$gpio = new GPIO();

In this example, the last statement sets GPIO pin 17 for output. Controlling the pins requires two more commands:

$gpio->output(17, 1)

$gpio->output(17, 0)

The first sets the pin's state to 1 (i.e., turns it on), and the second changes the state to 0 (i.e., turns the pin off). Finally, the $gpio->unexportAll() command resets all pins.

Using these commands, you can quickly whip up a simple PHP script that blinks the LED connected to GPIO pin 17 (Listing 3).

Listing 3

Simple PHP Script to Blink LED

01 <?php
02 require 'vendor/autoload.php';
03 use PhpGpio\Gpio;
04 $gpio = new GPIO();
05 $gpio->setup(17, "out");
06 while (true){
07         $gpio->output(17, 1);
08         sleep(1);
09         $gpio->output(17, 0);
10         sleep(1);
11 }

To run the script, execute the sudo php blinking_led_script.php command. Although the php-gpio library makes it easy to control GPIO pins from PHP scripts, keep in mind that the scripts themselves must be run from the command line. In other words, you cannot use the described commands directly in PHP pages served by a web server.

The solution is simple: Use the shell_exec() function to call the script from a PHP app. For example, I run a simple PHP-based photo gallery on my Raspberry Pi, and I have added the shell_exec('sudo php path/to/php-gpio/blink_led.php') statement that calls the blink_led.php script. This way, when someone visits my gallery, the LED blinks for a couple of seconds.

The only problem is that the entire PHP app pauses while the called script runs. In the case of the blinking LED script, that might not be an issue, but it could be a serious problem if you call a script that takes longer to complete. Fortunately, an easy fix is available. You can run the script in the background and discard output by redirecting the script:

shell_exec('sudo php path/to/php-gpio/\
  blink_led.php > /dev/null 2> /dev/null &');

Again, because all PHP scripts on Raspberry Pi must be run with root privileges, you need to add the www-data user to the sudoers file.

The php-gpio library can be put to other, more advanced uses, too. The php-gpio-web GitHub repository, for example, contains a simple web app [5] that demonstrates how to create a web interface to control an LED, and the temperature-pi project [6] shows how to read and log data from a temperature sensor.

In this article, I briefly outlined the possible approaches to using PHP on Raspberry Pi. And the example scripts offer a few pointers on controlling GPIO via PHP scripts. So if you want to use PHP with Raspberry Pi, you now know where to start.

The Author


Buy this article as PDF

Express-Checkout as PDF

Pages: 4

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content