logo-mobile

ROHM

ROHM
Menu
  • Arduino –
  • Raspberry Pi –
  • Trending –
  • Others –
  • About –
  • Contact –

Raspberry Pi

How to Use a Raspberry Pi as a Web Server

DevicePlus Editorial Team
Published by DevicePlus Editorial Team at March 1, 2022
Categories
  • Raspberry Pi
Tags
Pi server

Originally published by Aug 28, 2020

Table Of Contents

  1. So Why Not Do The Same?
  2. Free Alternatives to a Raspberry Pi Web Server
  3. When to Use Raspberry Pi as a Web Server
  4. Choosing a Web Server
  5. Installing Apache on Raspberry Pi
  6. Congratulations, You Have a Web Server
  7. Build Dynamic Websites with Raspberry Pi
  8. Installing PHP
  9. Where Should You Go From Here?
  10. Related Articles

Were you aware that the Raspberry Pi Foundation’s official website is hosted on a cluster of Raspberry Pi boards?
This configuration successfully served tens of millions of visitors on the Foundation’s busiest day – the launch of the Raspberry Pi 4.

So Why Not Do The Same?

Understand that this Pi cluster is located in a data center and relies on enterprise-grade switches, load balancers and a file server to write their own custom SD card images to the Pis.
You won’t get comparable results from your lounge room with a wireless router and home internet (even if your router and internet are quite good).
You also have to manage security. If you make your Pi available to the public internet and an attacker compromises it, they might gain access to your local network. That’s not great.

raspberry pi

If you’ve got your heart set on using a Raspberry Pi to host a website on the public internet, you’re probably better off paying someone who offers Raspberry Pi hosting to handle it for you. That’s especially true if you have anything commercial riding on it.

Free Alternatives to a Raspberry Pi Web Server

Be that as it may, it’s not that difficult to host a web server in the cloud for $0.00 a month. That’s because the big cloud giants are happy to just give you a small VPS on their free tier.
You won’t launch a hot new tech start-up with this level of infrastructure, but it’s plenty for a personal blog, or even for a local business or freelancing website. All you need to pull this off are the same Linux skills as it takes to do the same thing on a Raspberry Pi.

When to Use Raspberry Pi as a Web Server

A Raspberry Pi makes excellent sense as an inexpensive and power-efficient way to host websites and web applications over a local network.
It’s a great testbed or development environment where you have full access right up to the metal. You won’t have to worry about bandwidth charges or acceptable use policies while you’re robustly testing it either.
It’s also an excellent way to host something like a wiki on the local intranet for a small or medium business. Just be sure to automate backups.
You may be using your Pi to prototype an IoT device that features some kind of web interface or dashboard. Installing a web server is the only real way to do this.
Finally, one of the very best reasons to install a web server on a Raspberry Pi is just to start learning about web servers.

Choosing a Web Server

By far, the two most popular web servers are Apache and Nginx (pronounced Engine-X). These are both open source projects you can install and use freely.
Apache, first released in 1995, is nearly as old as the web itself and is still the most popular web server on the internet.
It has its own modules to serve dynamic content such as PHP, and its ubiquity means that it’s incredibly well documented. It’s a robust all-rounder that makes it easy to give individual directories their own configuration. These things make it a great place to get started.

Pi server

Nginx was written to address Apache’s limitations with handling many concurrent users. It excels at serving static content but requires an external processor for dynamic content.
In this guide, we’ll install Apache. Nginx’s advantages are real but aren’t that important to the typical use cases of a Raspberry Pi. But if you are using your Pi to prototype something that might scale, think about learning Nginx as well.
It’s, in fact, not all that unusual to see both used together, with Nginx on the front end, serving static resources and Apache in the back, rendering dynamic content.
Other web servers of interest include:
• Node.js is installed on your Raspberry Pi already. It’s not a traditional web server, but a runtime environment for Javascript. It can handle many connections at once, which makes it very handy for large web applications, though you would normally use something like Apache or Nginx as a front end.
• Also already on your Raspberry Pi is Python’s http.server module. It’s good for testing and development, but it’s especially convenient for those things. If you’re writing a web application in Python, you can set up a simple web server in a few lines of code.
• Lighttpd makes very efficient use of system resources, which should interest anyone developing embedded systems and IoT devices.

Installing Apache on Raspberry Pi

This guide is written for Raspberry Pi OS Buster, but if you’re using an earlier version of Raspbian, this won’t be very different; Apache is very mature software.
You will need internet connectivity to install these packages.
Get on to the terminal and update your operating system by typing:
sudo apt update && sudo apt upgrade -y

When the updates are complete, install Apache with this command:
sudo apt install apache2 -y

You can now test that Apache has installed properly by browsing to your Raspberry Pi. If you’re using the Pi’s web browser, type http://localhost.com/ into the address bar.
From elsewhere on your local network, you will need to use the IP address. If you don’t know this already, you can get it with this command:
hostname -I

Your browser will load a page that looks like this.
Apache2 Debian Default Page

Congratulations, You Have a Web Server

That was easy, wasn’t it?
This simple setup is all you need to serve static content: pages that appear exactly the same in the browser each time they’re loaded, until you edit the files yourself.
So let’s edit one of these files. Type:
sudo nano /var/www/html/index.html

Scroll down the file until you find the line that says “Apache2 Debian Default Page” and change it to whatever new heading you feel like. Save the file and then reload the page in your browser.
Change page

Static websites are lightning-fast and simple to maintain and offer a lot to explore. For a simple blog, business website, or information website, this might be all you need. Just deploy your website to the directory /var/www/html/, and Apache will serve it.

Build Dynamic Websites with Raspberry Pi

Instead of just displaying the same thing every time, you might want to serve dynamic content – pages the server generates by executing code.
This might be so that users can edit or add to pages or to add new pages of their own – perhaps for a wiki, a forum, or a comments section on a blog.
It might also be so that your device can display data on a dashboard, whether that’s collected from an online service, a database, or from something connected to the GPIO pins.
There are many scripting languages you can use to build dynamic websites, such as Java, Perl, PHP, Python or Ruby. You can even use compiled binaries written in Go, C++ or C.
PHP is a good starting point since the most popular open-source CMS platforms like WordPress, Joomla! and Drupal are built on it. Together, these platforms easily form a majority of all websites.

Installing PHP

Adding PHP functionality to Apache requires just one command:
sudo apt install php libapache2-mod-php -y

Test whether this works by replacing the index page with one that’s rendered in PHP. Delete index.html and open a new file in nano called index.php.
sudo rm /var/www/html/index.html
sudo nano /var/www/html/index.php

Now type this line and then save the file:

<?php phpinfo(); ?>

Now reload the page in the browser. You should see your PHP function render like this:
PHP function render

Where Should You Go From Here?

Well, it’s up to you.
The basic building blocks of the web are HTML and CSS, so it’s not a bad idea to get started with one of them.
You can avoid a lot of the work of HTML and CSS by using a front-end framework like Bootstrap, where much of it is already written for you.
If you want to avoid code completely, a static site builder like Hugo will generate the whole website.
Once you’ve got those mastered, you might want to try dynamic websites.
You might also want a database store and retrieve data. If you add MariaDB (or MySQL) to the web server we’ve already built, you will have what’s known as a LAMP stack (Linux, Apache, MySQL, PHP).
A LAMP stack is the basis for many website platforms, including the most popular one: WordPress. It’s also a good way to host a wiki, a forum, and far more besides.
Maybe you’d rather not use PHP but Python. I mean, if you’re prototyping some kind of IoT device or home automation system on a Raspberry Pi, there’s a good chance you’re using it already.
It’s a bit more work to get Apache to run Python, because you need to configure the Common Gateway Interface, or CGI. If you get stuck, just use Python’s built-in http.server module for development and testing while you figure it out.
Python also offers popular web frameworks, including Django and Flask. Flask is the simpler and more lightweight of the two.
We’re still barely scratching the surface. Enjoy, and see where this takes you.

Related Articles

Don’t stop there – there’s so much to explore with Raspberry Pi. Take a look at some more of our Raspberry Pi guides:

  1. The History and Uses of Raspberry Pi
  2. How to Use a Raspberry Pi as a VPN Server
  3. How to Use a Raspberry Pi as a DNS Server
  4. How to Make a VNC Server on Raspberry Pi
  5. How Does Raspberry Pi Compare to Computers of the Past?
DevicePlus Editorial Team
DevicePlus Editorial Team

Check us out on Social Media

  • Facebook
  • Twitter

Recommended Posts

  • How to Use a Raspberry Pi as a DNS ServerHow to Use a Raspberry Pi as a DNS Server
  • How to Use a Raspberry Pi as a VPN ServerHow to Use a Raspberry Pi as a VPN Server
  • Installing Wordpress on Raspberry PiInstalling Wordpress on Raspberry Pi
  • How to integrate an RFID module with Raspberry PiHow to integrate an RFID module with Raspberry Pi
  • Setting Up Raspberry Pi as a Home Media ServerSetting Up Raspberry Pi as a Home Media Server
  • Raspberry Pi – Basic CompletionRaspberry Pi – Basic Completion
Receive update on new postsPrivacy Policy

Recommended Tutorials

  • How to integrate an RFID module with Raspberry Pi How to integrate an RFID module with Raspberry Pi
  • How to Use the NRF24l01+ Module with Arduino How to Use the NRF24l01+ Module with Arduino
  • How to Run Arduino Sketches on Raspberry Pi How to Run Arduino Sketches on Raspberry Pi
  • Setting Up Raspberry Pi as a Home Media Server Setting Up Raspberry Pi as a Home Media Server

Recommended Trends

  • SewBot Is Revolutionizing the Clothing Manufacturing Industry SewBot Is Revolutionizing the Clothing Manufacturing Industry
  • All About The Sumo Robot Competition And Technology All About The Sumo Robot Competition And Technology
  • 5 Interesting Tips to Calculating the Forward Kinematics of a Robot 5 Interesting Tips to Calculating the Forward Kinematics of a Robot
  • Go Inside the Drones That Are Changing Food Delivery Go Inside the Drones That Are Changing Food Delivery
Menu
  • Arduino –
    Arduino Beginner’s Guide
  • Raspberry Pi –
    Raspberry Pi Beginner's Guide
  • Trending –
    Updates on New Technologies
  • Others –
    Interviews / Events / Others

Check us out on Social Media

  • Facebook
  • Twitter
  • About
  • Company
  • Privacy Policy
  • Terms of Service
  • Contact
  • Japanese
  • 简体中文
  • 繁體中文
Don’t Forget to Follow Us!
© Copyright 2016-2023. Device Plus - Powered by ROHM
© 2023 Device Plus. All Rights Reserved. Muffin group

istanbul escort istanbul escort istanbul escort