Let me begin by saying that nearly every guide on how to set up a web server on a Raspberry Pi isn’t written for the intended consumer market of the Raspberry Pi — noobs. The Raspberry Pi was made to help noobs (like me) understand the basic concepts behind computer science.
The assumption of most tutorial writers is that the reader is comfortable using the Linux command line, that they understand how web servers work and how HTTP works. But that’s just not the case for most new users, and certainly not for me. So I’m going to write a guide on installing a web server on the Raspberry Pi 3 B+ (which should work on other models of the Raspberry Pi).
First of all, what is Apache and what’s a web server?
A good explanation can be found on the Apache site, but basically, a web server is a piece of software (or the hardware it resides on) that delivers (serves) a web page to anyone who might request it.
Apache is more precisely an HTTP server. HTTP is an abbreviation for HyperText Transfer Protocol. Hypertext is a special way of writing a document that produces a webpage when opened by a web browser (such as Chrome or Firefox). So HTTP is a method (protocol) of transferring hypertext (web pages) from a web server to a client (web browser).
Let’s install our server!
Apache is free to use and is simple to install. The easiest way to do it is from the Raspberry Pi terminal. The terminal is the text interface of our Raspberry Pi. To open it we can press Ctrl + Alt + t or we can click on the icon in the upper left corner of our desktop.
Once in the terminal we need to type in sudo apt install apache2
on a single line. But before we go any further, I want us to understand what we’re typing.
sudo
is a command we will see a lot; it’s short for “super user do.” Many of the commands we will be using in the terminal are reserved for the “root” user, that is, the person with access to every aspect of the computer. That’s us, but we’re most likely signed in as “pi” or another user name. Fortunately, the Raspberry Pi automatically assigns us to a special list of users called “sudoers” who have sudo privileges. sudo
lets us run commands that are typically only available to the root user. If we tried to run the command apt install apache2
without appending sudo
to the beginning, we would get a response from the terminal saying “Permission denied.”
The reason we need to use sudo
is because the apt
command can make changes to your computer that are reserved for the root user.
apt
command, it helps to understand the operating system (OS) of the Raspberry Pi a little better. Raspberry Pi typically runs on the Raspbian OS. Raspbian’s design was based on the Debain OS (a type of Linux OS) and optimized to run on the Raspberry Pi. When we installed Raspbian on our Raspberry Pi, a number of packages were also installed. Packages are compressed software files. Some of these packages are uncompressed and installed with the OS, others aren’t essential to using the Pi and aren’t installed but are included anyway because they become useful as we learn more about our Pi. apt
, short for Advanced Packaging Tool, is a program that lets us interact with those packages. One of the most common tasks of the apt
program is to install software. In the case of sudo apt install apache2
, install
is part of the apt
program and tells it to install the package apache2.
Installation complete
Now we have Apache installed on our Raspberry Pi, we need to get it running. Type sudo service apache2 start
. service
is a program that runs scripts (a specialized type of program) that control Raspbian services. Think of services as programs that are working in the background without you having to directly interact with them to make them work. In this case, apache2
is our script that controls the service Apache (our HTTP server which will automatically run when we start up the Pi). Finally, the service
command requires at least two pieces of information (arguments) to run: the script to run (apache2
), and what to do with the service (e.g. start
, stop
, restart
). Because we can start and stop important services (programs) with this command, it requires the use of sudo
.
That’s it! Although our website can only be accessed in our Local Area Network (anything connected to our router) we have a web page. To see it on our Pi open our web browser and type in “localhost”. The page we see will be the default webpage made by Apache for Debian systems.
To see it on another device on our network we need to find out the IP address of our Pi. IP is short for Internet Protocol, the method by which computers communicate with each other over the internet. Our IP address is a set of four numbers, each from 0 – 255 in the form of ###.###.###.### and is how our computer is identified either on our local network or on the internet.
To find our IP address, open the terminal and type hostname -I
. In a very broad sense (and in this case specifically), a host is any computer connected to a network, but it can have different meanings depending on the context. The hostname
command lets us interact with different information about our computer as it relates to how it’s seen on our network. The -I
argument of the command tells hostname
to display all IP address for the host computer. This IP address is not permanent (don’t worry, it won’t change as long as our Pi is powered on) and it is only available on our local network.
Now that we have our IP address, we can see our new webpage from any computer on our local network. Just put the IP address into the web browser’s address bar and we’re all set.
Let’s go over one more thing. What if we want to change our boring Apache2 default page? To do that, we need to navigate to the directory where the page is and replace it with one of our own. In the Raspberry Pi terminal enter the following command: cd /var/www/html
. cd
is short for “change directory.” Directories in Linux work the same as they do in Windows or Macintosh, you can even navigate there from the Raspbian desktop with the file explorer. Inside the /html
directory is a file called index.html
. This is the default html file of our web page and if we replace it, we should name the new file index.html
as well.
3 thoughts on “Raspberry Pi Apache2 Web Server on Raspbian, Part 1”
Comments are closed.