The world’s most popular web server software on your favorite single board computer
Learn how to install and configure the Apache web server on Raspberry Pi, making it the perfect platform for hosting websites or web applications. …
Updated August 18, 2023
Learn how to install and configure the Apache web server on Raspberry Pi, making it the perfect platform for hosting websites or web applications.
Introduction
Apache is the most popular and powerful open-source web server software in the world. It’s responsible for serving billions of web pages every day and powers many high-traffic websites. In this article, we will show you how to install Apache on your Raspberry Pi, a small, low-cost single board computer that has become popular with hobbyists, developers, and educators for its versatility and ease of use.
Prerequisites
Before you begin, make sure that your Raspberry Pi is set up and running Raspbian, the official operating system for Raspberry Pi devices. If you haven’t done so already, follow this guide to set up your Raspberry Pi: https://www.raspberrypi.org/documentation/installation/installing-images/
Step 1: Update Your System
The first step is to update the package list and install any available updates for your Raspbian system. Open a terminal window by clicking on Menu -> Accessories -> Terminal, or pressing Ctrl+Alt+T
. Then, run the following commands:
sudo apt-get update
sudo apt-get upgrade
This will ensure that you have the latest software installed and ready to install Apache.
Step 2: Install Apache
Now we can proceed with installing Apache on your Raspberry Pi. Run the following command in the terminal:
sudo apt-get install apache2
This will download and install Apache, as well as any necessary dependencies. Once the installation is complete, you can verify that it’s running by visiting http://<your_raspberrypi_ip_address>
in your web browser. If everything is working correctly, you should see a default Apache webpage displaying “It works!”.
Step 3: Configure Apache
Apache uses a file called /etc/apache2/sites-available/000-default.conf
to configure the default website settings. Open this file with your favorite text editor (e.g., nano
, vim
, or gedit
) by running:
sudo nano /etc/apache2/sites-available/000-default.conf
Find the line that says <Directory /var/www/html>
and change it to:
<Directory /home/pi/public_html>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
This will tell Apache to serve files from your home directory’s public_html
folder instead of the default /var/www/html
location. Create this folder by running:
mkdir /home/pi/public_html
Now, you need to assign ownership and permissions for this folder so that Apache can read its contents:
sudo chown -R www-data:www-data /home/pi/public_html
sudo chmod -R 755 /home/pi/public_html
Save the file (Ctrl+X
in nano
, then Y
and Enter
) and exit the text editor.
Step 4: Restart Apache
To apply the changes you made to the configuration file, restart the Apache service by running:
sudo systemctl restart apache2
Now, you should be able to visit http://<your_raspberrypi_ip_address>
in your web browser and see an empty directory listing of your public_html
folder.
Step 5: Create a Test Page
Create a simple HTML file called index.html
in your public_html
folder with the following content:
<!DOCTYPE html>
<html>
<head>
<title>My Raspberry Pi Website</title>
</head>
<body>
<h1>Welcome to my Raspberry Pi website!</h1>
<p>This is a test page.</p>
</body>
</html>
Save the file in your public_html
folder and refresh your browser. You should see your new webpage displayed instead of the directory listing.
Conclusion
Congratulations! You have successfully installed Apache on your Raspberry Pi and configured it to serve your own webpages or web applications. With a little more work, you can set up virtual hosts, SSL certificates, and many other features that make Apache a powerful tool for running websites of all sizes. If you need further assistance or have any questions, please feel free to ask in the comments section below.