Thursday, April 25, 2024
How ToIoT HardwaresRaspberry PiTutorials/DIY

Control LED with Raspberry Pi using Apache Webserver

In this tutorial we are going to Control LED with Raspberry Pi using Apache Webserver. For this we create an html/php web page which has two buttons – one for turning on the LED and second for turning off the LED.


  1. Raspberry pi board (With Raspbian OS)
  2. LED
  3. 250 ohm resistor
  4. Jumper Wires

Control LED with Raspberry Pi using Apache Webserver

Wire Connection

Connect Positive pin of LED is connected to GPIO 27 pin and the negative to a 270 ohm resistor, the other side of which is connected to GND pin.

Installing WiringPi Library in RPi

WiringPi is a PIN based GPIO access library written in C for the BCM2835, BCM2836 and BCM2837 SoC devices used in all Raspberry Pi versions. It’s released under the GNU LGPLv3 license and is usable from C, C++ and RTB (BASIC) as well as many other languages with suitable wrappers.

sudo apt-get update
sudo apt-get install git-core
git clone git://
cd wiringP./build

Installing Apache Web Server

Apache Web Server is an open-source web server creation, deployment and management software. Initially developed by a group of software programmers, it is now maintained by the Apache Software Foundation.  To Install Apache web server we will use following commands:

sudo apt-get update
sudo apt-get install apache2 -y

To test the web server whether it is working or not, go to your browser and type the Pi’s IP address in the tab. You can see a default web page of apache. To find the Pi’s IP address, type ifconfig at the command line.

Now we will see how to Change the default web page with your own HTML page. This default web page is just an HTML file on the filesystem. It is located at var/www/html/index.html. Navigate to this directory in a terminal window and have a look at what’s inside:

cd  var/www/html
ls -al
This will show you:
total 12
drwxr-xr-x  2 root root 4096 Jan  8 01:29 .
drwxr-xr-x 12 root root 4096 Jan  8 01:28 ..
-rw-r--r--  1 root root  177 Jan  8 01:29 index.html

This shows that by default there is one file in /var/www/html/ called  index.html and it is owned by the root user . To edit the file, you need to change its ownership to your own username. Change the owner of the file using:

sudo chown pi: index.html

Now edit this file and then refresh the browser to see the web page change.

PHP Installation in Raspberry pi

Now we are going to install PHP extension in Raspberry pi because we want use PHP code along with HTML. Using PHP code we can create shell commands to control the LED from PHP script. To allow Apache server to edit PHP files, we will install the latest version of PHP and the PHP module for Apache. Use the following command in terminal to install these:

sudo apt-get install php libapache2-mod-php -y

Now remove the default index.html file:

sudo rm index.html

And create the your own index.php file:

sudo nano index.php

Now enter the below code in index.php to test the PHP installation.

<?php phpinfo(); ?>

Save it by pressing CTRL + X and the ‘y’ and enter.  Now refresh the webpage in your browser you will see a long page with lots of information about PHP. This shows that the PHP extension is installed properly. If you have any problem to the pages or if the pages do not appear try reinstalling apache server and its PHP extension.

Code for controlling GPIO pin using this Raspberry Pi Webserver

Now delete the previous code in index.php (<?php phpinfo(); ?>) file and insert below PHP code to control GPIO pins inside body of HTML code.

Below is the complete code for creating two buttons to turn on and off the LED connected to Raspberry Pi.

<meta name="viewport" content="width=device-width" />
<title>Control LED with Raspberry Pi using Apache Webserver </title>
       <center><h1>Control LED using Raspberry Pi Webserver - IoTbyHVM.OOO</h1>
<p>Visit <a href=""> IoTbyHVM.OOO</a> For more IoT Tutorials </p>
         <form method="get" action="index.php">
            <input type="submit" style = "font-size: 14 pt" value="OFF" name="off">
            <input type="submit" style = "font-size: 14 pt" value="ON" name="on">
    shell_exec("gpio -g mode 27 out");
                        echo "LED is off";
                        shell_exec("gpio -g write 27 0");
            else if(isset($_GET['on']))
                        echo "LED is on";
                        shell_exec("gpio -g write 27 1");

control led apache

In above code there is a PHP script which checks which button is pressed by using below code and then turns on and off the LED accordingly.

    shell_exec("gpio -g mode 27 out");
                        echo "LED is off";
                        shell_exec("gpio -g write 27 0");
            else if(isset($_GET['on']))
                        echo "LED is on";
                        shell_exec("gpio -g write 27 1");

Here we have used shell_exec() command in php code, this command is used to run the shell command from the PHP script. Learn more about shell_exec here. If you run the command inside shell_exec directly form the terminal of Raspberry pi, you can directly make GPIO pin 27 low or high. Below are two commands to test the LED directly from terminal.

gpio -g write 27 0
gpio -g write 27 1

After completing this, run the code in your browser by typing the IP address of raspberry pi in the browser. You will see 2 buttons – ON, OFF to control your LED by clicking these buttons.

I hope you like this post ”Control LED with Raspberry Pi using Nodejs”.  Do you have any questions? Leave a comment down below!

Thanks for reading. If you like this post probably you might like my next ones, so please support me by subscribing my blog.

Explore Some more Raspberry Pi Tutorials :

Apache web server and PHP installation on Windows PC:

Harshvardhan Mishra

Hi, I'm Harshvardhan Mishra. Tech enthusiast and IT professional with a B.Tech in IT, PG Diploma in IoT from CDAC, and 6 years of industry experience. Founder of HVM Smart Solutions, blending technology for real-world solutions. As a passionate technical author, I simplify complex concepts for diverse audiences. Let's connect and explore the tech world together! If you want to help support me on my journey, consider sharing my articles, or Buy me a Coffee! Thank you for reading my blog! Happy learning! Linkedin

2 thoughts on “Control LED with Raspberry Pi using Apache Webserver

Leave a Reply

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