Setup Raspberry Pi as Web Server Using Apache


Did you know that the Raspberry Pi Foundation’s official website is hosted on a cluster of Raspberry Pi boards?  It was able to serve millions of visitors even on the Foundation’s busiest day,  the launch of the Raspberry Pi 4.  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. While you won’t get similar results from your home internet,  the 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 development environment where you have full access rights. You won’t have to worry about bandwidth charges or acceptable use policies while you’re 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.  One of the very best reasons to install a web server on a Raspberry Pi is just to start learning about web servers.

The two most popular web servers are Apache and Nginx. These are both open source projects you can install and use for free.  In this tutorial,  we will be using Apache.  Apache HTTP Server is a free and open-source web server that delivers web content through the internet. It is commonly referred to as Apache and is the most popular HTTP client on the web.  Apache is just one component that is needed in a web application stack to deliver web content. One of the most common web application stacks involves LAMP, or Linux, Apache, MySQL, and PHP.  A large portion of web applications run on some form of the LAMP stack because it is easy to build and also free to use. For the most part, web applications tend to generally have similar architecture and structure even though they serve many different functions and purposes. 

Apache is considered open source software, which means the original source code is freely available for viewing and collaboration. Being open source has made Apache very popular with developers who have built and configured their own modules to apply specific functionality and improve on its core features. Apache has been around since 1995 and is responsible as a core technology that helped spur the initial growth of the internet.  One of the pros of Apache is its ability to handle large amounts of traffic with minimal configuration. It scales with ease and with its modular functionality at its core, you can configure Apache to do what you want, how you want it. You can also remove unwanted modules to make Apache more lightweight and efficient.  Some of the most popular modules that can be added are SSL, Server Side Programming Support (PHP), and Load Balancing configs to handle large amounts of traffic. Apache can also be deployed on Linux, MacOS, and Windows. 

Apache functions as a way to communicate over networks from client to server using the TCP/IP protocol. Apache can be used for a wide variety of protocols, but the most common is HTTP(S). HTTP(S) or Hyper-Text Transfer Protocol (S stands for Secure) is one of the main protocols on the web, and the one protocol Apache is most known for.  HTTP(S) is used to define how messages are formatted and transmitted across the web, with instructions for browsers and servers on how to respond to various requests and commands. Hypertext Transfer Protocol Secure is usually through port 443 with the unsecured protocol being through port 80.

The Apache server is configured via config files in which modules are used to control its behavior. By default, Apache listens to the IP addresses configured in its config files that are being requested. This is where one of Apache’s many strengths come into play.  With the Listen directive, Apache can accept and route specific traffic to certain ports and domains based on specific address-port combination requests. By default, Listen runs on port 80 but Apache can be bound to different ports for different domains, allowing for many different websites and domains to be hosted and a single server. You can have domain1.com listening on port 80, domain2.com on port 8080 and domain3.com on port 443 using HTTPS all on Apache.  Once a message reaches its destination or recipient, it sends a notice, or ACK message, basically giving acknowledgment to the original sender that their data has successfully arrived. If there’s an error in receiving data, or some packets were lost in transit, the destination host or client sends a Not Acknowledged, or NAK message, to inform the sender that the data needs to be retransmitted.

While Flexibility is one of the biggest advantages of the software,  it is a double-edged sword.  Customizing various aspects of Apache gives you greater control over everything, but it can also open up security vulnerabilities. These modifications can create an open door for hackers to exploit.  More experienced web developers can avoid this, but the fact remains, it can be dangerous.  There are a lot of modules that add specific features to your web server. Many are quite useful.  However, some of the features may be unnecessary or serve no purpose. It really depends on the website. In those cases, it can be hard to recognize which modules are necessary and which are not without being knowledgeable about the software.

It’s expected that when a website gets a lot of traffic, it starts to slow down, and the web server software is typically to blame.  In the case of Apache, every time a request is made, the software generates a new process to handle that new connection. This isn’t an issue for smaller websites, but when a lot of traffic occurs at the same time, you could generate hundreds or thousands of processes in seconds.  That’s when websites start to take a very long time to load. The good news is that there are workarounds, but they require reconfiguring Apache to handle it.  If you are looking for a better alternative for a high-traffic site, Nginx gets better performance in this case.

The Raspberry Pi,  just like any computer,  needs an operating system that is stored somewhere.  In the Raspberry Pi,  the storage is a user replaceable microSD card.  There are several readily available operating systems available for the Raspberry Pi.  They are available for download on the internet as an img file.  Once downloaded,  we will need to copy the image to the microSD card. We have another article that shows how to do this at <a href=”https://ames-pc.com/geek/blogs/blog.html?issue=1949″ target=”_blank”>https://ames-pc.com/geek/blogs/blog.html?issue=1949</a>.  Once the OS is transferred to the microSD card,  all we need to do is insert the microSD card into the Pi and turn it on.  Once that is done,  we can install the protocols to turn the Raspberry Pi to a web server.

You will need internet connectivity to install the Apache packages.  Go to the terminal and update your operating system by typing <br>sudo apt update && sudo apt upgrade -y</b>.  When the updates are complete, install Apache by typing <b>sudo apt install apache2 -y</b>.  You can now test if Apache is installed properly by browsing to your Raspberry Pi. If you’re using the Pi’s web browser, type <b>http://localhost.com/</b> into the address bar. From another computer on your local network, you will need to use the IP address. If you don’t know this already, you can get it by typing <b>hostname -I</b>.  If apache is set up correctly,  you will see the Apache2 Default page.  If simple,  static pages are all you want to serve,  you are done.

Instead of just displaying the same thing every time, you may 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 like 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 or a database.  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 to Apache requires just one command, <b>sudo apt install php libapache2-mod-php -y</b>.  After running the command,  you can test whether this works by replacing the index page with one that’s rendered in PHP. Create a new file called index.php by typing <b>sudo nano /var/www/html/index.php</b>.  Add the following line to the file and save it.

<br>

<br>

<pre>

  <code>

    <?php phpinfo(); ?>

  </code>

</pre>

<br>

<br>

Now,  when you go to <b>http://localhost/index.php</b>,  you will see a php rendered page.

what is a web server

https://en.wikipedia.org/wiki/Web_server

what is https

https://en.wikipedia.org/wiki/HTTPS