How to Control an LED with Raspberry Pi Webserver using Apache

Controlling an LED with Raspberry Pi Webserver using Apache

In this tutorial, we will install Apache web server in Raspberry Pi to control the LED from a webpage that can be accessed from anywhere over the internet. This is a basic tutorial with minimum features and it can be further modified to use this method in IoT-based home automation, remote control automation, robotics, etc.

Here we control an LED connected to Raspberry Pi by using Apache web server. For this, we create an HTML/php web page which has two buttons - one for turning on the LED and the second for turning off the LED.


Components Required

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


An SSH client (Putty) is used to connect the Raspberry pi using a Laptop or computer. For this, the raspberry pi needs to be connected to a network via LAN or Wi-Fi. If you have a separate monitor for your raspberry pi, then it's better to connect raspberry pi with the monitor and you don’t have to use any SSH client.


Controlling LED using Raspberry Pi Webserver

Step 1: Connections

Raspberry Pi LED Connection Circuit

The connections in this project are quite simple - the positive pin of LED is connected to GPIO 27 pin and the negative pin to a 270 ohm resistor, the other side of which is connected to GND pin.


Step 2: Installing WiringPi Library

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.

1. First we will update our Pi with the latest versions of Raspbian using the command:

sudo apt-get update


2. Now we will install git by using this command:

sudo apt-get install git-core


3. Now obtain WiringPi using git by this command:

git clone git://


4. Then install WiringPi library using:

cd wiringP./build


Step 3: Installing a Web Server

Apache is a very popular webserver, designed to create web servers that have the ability to host one or more HTTP-based websites. Apache Web Server can be enhanced by manipulating the code base or adding multiple extensions/add-ons. In our project, we are using an HTTP server and its PHP extension.

To Install Apache web server, we will use the following commands:

First, update the available packages:

sudo apt-get update


Now, install the apache2 package by using this command in the terminal:

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.

To find the Pi's IP address, type ifconfig at the command line.

By default, Apache puts a test HTML file in the web folder. This default web page is served when you browse to (whatever the Pi's IP address is) from another computer on the network.

Browse to the default web page either on the Pi or from another computer on the network and you will see the following:

Apache2 Webserver

This means the Apache web server is working.


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.


You can now try editing this file and then refresh the browser to see the web page change.


Install PHP in Raspberry Pi  

Now if we want to use PHP code along with HTML, then we have to further install the PHP extension in Raspberry pi. Using PHP code, we can create shell commands to control the LED from the PHP script.

To allow the 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 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 with the pages or if the pages do not appear, try reinstalling the apache server and its PHP extension.


Step 5: Start Coding 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>Raspberry Pi WiFi Controlled LED</title>
       <center><h1>Control LED using Raspberry Pi Webserver</h1>      
         <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("/usr/local/bin/gpio -g mode 27 out");
                        echo "LED is off";
                        shell_exec("/usr/local/bin/gpio -g write 27 0");
            else if(isset($_GET['on']))
                        echo "LED is on";
                        shell_exec("/usr/local/bin/gpio -g write 27 1");

Raspberry Pi Webserver

In the 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("/usr/local/bin/gpio -g mode 27 out");
                        echo "LED is off";
                        shell_exec("/usr/local/bin/gpio -g write 27 0");
            else if(isset($_GET['on']))
                        echo "LED is on";
                        shell_exec("/usr/local/bin/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.

/usr/local/bin/gpio -g write 27 0
/usr/local/bin/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 liked this article. You can also check out our other Internet of Things projects.


Hello, Neat post. There is a problem with your site in web explorer, may
test this? IE still is the marketplace leader and a large part of other folks will omit
your excellent writing because of this problem.

Hi, i read your blog occasionally and i own a similar one and i was just curious
if you get a lot of spam responses? If so how do you prevent it,
any plugin or anything you can suggest? I get so
much lately it's driving me mad so any assistance is very
much appreciated.

Howdy would you mind sharing which blog platform you're working with?

I'm going to start my own blog soon but I'm having a difficult time selecting
between BlogEngine/Wordpress/B2evolution and Drupal. The reason I ask
is because your design seems different then most blogs and I'm looking for something completely unique.
P.S My apologies for being off-topic but I had
to ask!

Its like you read my mind! You appear to know a lot about this, like you
wrote the book in it or something. I think that you could do with some pics to drive the message home a bit, but other than that, this is magnificent blog.
An excellent read. I will certainly be back.

Its like you learn my mind! You seem to know a lot
about this, such as you wrote the e book in it or something.
I believe that you can do with some percent to power the message house a little bit, but other
than that, this is great blog. A fantastic read.

I'll certainly be back.

you're in point of fact a just right webmaster.
The web site loading pace is amazing. It sort of
feels that you are doing any unique trick. In addition, The contents are
masterwork. you've performed a wonderful task in this topic!

I don't know if it's just me or if perhaps everyone else encountering problems with your blog.

It appears as though some of the text in your content are running off the screen. Can someone
else please provide feedback and let me know if this
is happening to them as well? This could be a issue with my web browser because I've had this happen previously.
Thank you

This design is incredible! You obviously know how to keep a reader amused.
Between your wit and your videos, I was almost moved to start my own blog (well, almost...HaHa!) Great job.
I really enjoyed what you had to say, and more than that,
how you presented it. Too cool!

Pool Cover Pumps on Amazon - Low Priced Pool Cover Pumps,amazonsurmountwayautomaticswimmingpoolcoverpump_2021 Type of pool cover pump: Pool cover pumps, come in 2 main types, automatic or manual. Automatic pool cover pumps are electric. Why Use a Pool Cover Pump? - In The Swim,Cover Pumps are an essential pool closing accessory if you own an inground pool or an above ground pool. Cover Pumps sit atop your winter pool cover and pump water off of its surface after it reaches a certain level.

I have been browsing online greater than three hours nowadays, yet I never
found any attention-grabbing article like yours. It's lovely worth enough
for me. In my view, if all site owners and bloggers made just right content
as you probably did, the net will probably be a lot more helpful
than ever before.

Hi there! This is kind of off topic but I need some advice from an established blog.
Is it very difficult to set up your own blog? I'm not very techincal but I can figure
things out pretty quick. I'm thinking about creating
my own but I'm not sure where to begin. Do you have any points or suggestions?
Thank you

Somebody necessarily lend a hand to make seriously posts I'd state.

That is the first time I frequented your website page and
to this point? I surprised with the analysis you made
to create this actual publish amazing. Wonderful activity!

Hi! I know this is kinda off topic but I was wondering if
you knew where I could locate a captcha plugin for my comment form?
I'm using the same blog platform as yours and I'm having difficulty finding one?

Thanks a lot!

Wonderful goods from you, man. I have have in mind your stuff prior to and you are just too fantastic.
I really like what you've got right here, certainly like what you're saying and the way in which in which
you assert it. You're making it entertaining and you still care for to stay it wise.
I cant wait to read much more from you. That is really a
tremendous website.

Good day I am so grateful I found your blog, I really found
you by error, while I was searching on Google for something
else, Anyhow I am here now and would just like to say thanks for a incredible post
and a all round thrilling blog (I also love the theme/design),
I don’t have time to browse it all at the minute but I have saved it and also added your RSS feeds, so when I have time I will be back to read a lot more, Please
do keep up the superb jo.

I think that everything typed was actually very logical.
But, think on this, suppose you typed a catchier post title?
I ain't saying your content is not solid, but what if you
added a post title that makes people want more?
I mean Raspberry Pi Webserver Tutorial - How to Control
LED with Raspberry Pi Webserver using Apache is kinda boring.
You should look at Yahoo's home page and note how they create article headlines
to grab viewers interested. You might add a video or a related pic or two to get people excited about everything've written. In my opinion, it could make your website a little livelier.

Hello! This is my first visit to your blog!

We are a group of volunteers and starting a new initiative in a community in the same niche.
Your blog provided us valuable information to work on. You have done a outstanding job!

Very nice post. I just stumbled upon your weblog and wished to
say that I've truly enjoyed browsing your blog posts.
After all I will be subscribing to your rss feed and I hope you write again very soon!

Woah! I'm really enjoying the template/theme of this site.

It's simple, yet effective. A lot of times it's challenging to get that
"perfect balance" between superb usability and visual appeal.
I must say you've done a superb job with this.
Also, the blog loads very quick for me on Internet explorer.
Exceptional Blog!

Great post. I used to be checking constantly this weblog and I'm inspired!

Very helpful info specifically the remaining part :) I care for such info a
lot. I used to be seeking this certain information for a very long time.
Thank you and best of luck.

Hey there would you mind stating which blog platform you're working with? I'm looking to start my own blog soon but I'm having a hard time selecting between BlogEngine/Wordpress/B2evolution and Drupal. The reason I ask is because your layout seems different then most blogs and I'm looking for something unique. P.S Sorry for getting off-topic but I had to ask!|

hey i gain lot of knowladge thourgh your article if you are also want gain your knowladge about satta king g then check official website

I truly wanted to post a small remark in order to appreciate you for these marvelous recommendations you are sharing at this site. My time consuming internet search has at the end of the day been paid with pleasant content to exchange with my family. I 'd suppose that most of us readers actually are undeniably lucky to live in a perfect site with many perfect professionals with very helpful basics. I feel extremely grateful to have seen the web site and look forward to many more fabulous moments reading here. Thank you once more for everything.

I want to show some thanks to you just for bailing me out of such a issue. Right after surfing throughout the world-wide-web and getting solutions which were not pleasant, I was thinking my entire life was well over. Existing devoid of the solutions to the issues you've solved by way of your entire article content is a serious case, as well as those which may have adversely affected my career if I hadn't encountered your site. Your actual mastery and kindness in dealing with all the details was vital. I am not sure what I would have done if I had not come upon such a stuff like this. I'm able to at this moment look forward to my future. Thank you very much for your specialized and amazing help. I won't hesitate to endorse your web blog to any person who should get guide about this topic.

I precisely wanted to thank you very much once again. I am not sure the things I would've carried out without those information discussed by you regarding such theme. It had been a troublesome issue for me, but encountering this well-written way you resolved that forced me to jump over contentment. I'm just grateful for your support and thus hope that you find out what an amazing job you were undertaking instructing most people with the aid of your web site. Most probably you've never come across any of us.

I and my buddies happened to be checking out the best tips found on your website while at once I had a horrible feeling I never thanked the blog owner for those tips. All the people became as a result happy to study all of them and now have clearly been loving those things. Thank you for simply being indeed thoughtful as well as for deciding upon some magnificent issues most people are really needing to be aware of. My sincere apologies for not expressing appreciation to earlier.

I must express thanks to the writer for bailing me out of this type of issue. Just after exploring throughout the world wide web and finding ways that were not productive, I was thinking my life was done. Living without the approaches to the problems you've solved as a result of your good website is a crucial case, as well as the ones that might have negatively damaged my career if I hadn't noticed your web page. That ability and kindness in taking care of every part was important. I don't know what I would have done if I had not come across such a thing like this. It's possible to at this time look ahead to my future. Thanks very much for your high quality and effective guide. I will not hesitate to propose your blog to anyone who ought to have recommendations about this topic.

I in addition to my guys ended up viewing the nice recommendations located on your web site and so suddenly came up with a horrible suspicion I never expressed respect to the blog owner for those tips. All of the boys were totally glad to learn them and now have certainly been making the most of those things. We appreciate you really being well accommodating and for picking out such awesome resources millions of individuals are really wanting to learn about. Our sincere regret for not expressing appreciation to you earlier.

I not to mention my pals came looking at the good points located on your website while before long I got a horrible suspicion I had not thanked the blog owner for those tips. Most of the guys appeared to be as a consequence very interested to read all of them and have in effect absolutely been enjoying these things. Appreciate your being indeed accommodating and also for considering such ideal information most people are really desirous to understand about. My sincere apologies for not expressing gratitude to you sooner.

Add new comment

The content of this field is kept private and will not be shown publicly.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.