The Raspberry Pi is an excellent choice for hosting a small web server. It’s low cost, power-efficient, and highly versatile. In this tutorial, we’ll guide you through setting up a web server on your Raspberry Pi using Apache or Nginx.
Before installing any software, update your system:
sudo apt update && sudo apt upgrade
This ensures you’re using the latest packages and improves system stability.
Apache is one of the most popular web server software options:
sudo apt install apache2 -y
sudo systemctl start apache2
sudo systemctl enable apache2
http://[Raspberry_Pi_IP]
You should see the default Apache welcome page.
To enable dynamic content on your web server, install PHP:
sudo apt install php libapache2-mod-php -y
sudo systemctl restart apache2
sudo nano /var/www/html/test.php
Add the following content:
<?php
phpinfo();
?>
Ctrl+O
, Enter
, Ctrl+X
).http://[Raspberry_Pi_IP]/test.php
in your browser. You should see the PHP info page.If you prefer Nginx, follow these steps:
sudo apt install nginx -y
sudo systemctl start nginx
sudo systemctl enable nginx
If your web application requires a database, install MySQL:
sudo apt install mysql-server -y
sudo mysql_secure_installation
sudo mysql -u root -p
CREATE DATABASE my_database;
EXIT;
To deploy your own website:
sudo nano /var/www/html/index.html
Add your HTML content, save, and close the file.
http://[Raspberry_Pi_IP]
in your browser.If you want to access your web server from outside your local network:
http://[Public_IP]
.Explore more advanced features like SSL encryption using Let’s Encrypt, deploying a CMS (e.g., WordPress), or hosting a Python or Node.js application. A Raspberry Pi web server is a fantastic way to learn web hosting and server management.