Originally published by Aug 28, 2020
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.
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.
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.
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.
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.
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.
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:
• 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.
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:
Your browser will load a page that looks like this.
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.
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.
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.
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:
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.
Don’t stop there – there’s so much to explore with Raspberry Pi. Take a look at some more of our Raspberry Pi guides: