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 upgradeThis 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 -ysudo systemctl start apache2
sudo systemctl enable apache2http://[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 -ysudo systemctl restart apache2sudo nano /var/www/html/test.phpAdd 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 -ysudo systemctl start nginx
sudo systemctl enable nginxIf your web application requires a database, install MySQL:
sudo apt install mysql-server -ysudo mysql_secure_installationsudo mysql -u root -pCREATE DATABASE my_database;
EXIT;To deploy your own website:
sudo nano /var/www/html/index.htmlAdd 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.