Turn Your Raspberry Pi 4 Into a Web Server and Build Your Own Site
Ready to harness the power of a Raspberry Pi 4 as a lightweight, cost‑effective web server? This guide walks you through installing Apache, PHP, and creating custom HTML/CSS pages—all while keeping your setup secure and maintainable.
What You’ll Need
- Raspberry Pi 4 (any RAM variant works)
- MicroSD card (16 GB+ recommended)
- Power supply (5 V, 3 A)
- Optional: Dual‑fan heatsink or aluminium case to keep temperatures down
Step 1: Prepare Your Pi
Boot up your Pi with Raspberry Pi OS (Lite or Desktop). Open a terminal or connect via SSH to run the following commands.
sudo apt update && sudo apt upgrade -y
This ensures all packages are current before we install the web stack.
Installing Apache
Apache is a proven, open‑source web server that serves static and dynamic content. Install it with:
sudo apt install apache2 -y
Once installed, Apache automatically starts and places a default page in /var/www/html.
Installing PHP
PHP enables server‑side scripting and database integration. Install the core package and the Apache module:
sudo apt install php libapache2-mod-php -y
Restart Apache to load PHP:
sudo systemctl restart apache2
Step 2: Verify the Server Is Running
Open a web browser on the Pi and navigate to https://localhost/ or https://raspberrypi.local/. You should see the Apache welcome page. To test from another device, find the Pi’s IP address:
hostname -I
Then visit https://192.168.1.102 (replace with your actual IP) on any computer connected to the same network.
Step 3: Customize the Default Page
By default, the index.html file is owned by the root user, preventing edits. Change ownership to your own user (replace pi with your username):
sudo chown pi:pi /var/www/html/index.html
You can now edit index.html directly or use a text editor like nano:
nano /var/www/html/index.html
Refresh your browser to see updates.
Step 4: Enable Remote Editing (Optional but Handy)
For frequent changes, consider using a secure file transfer client. WinSCP (for Windows) or FileZilla (cross‑platform) let you edit files over SFTP:
- Protocol: SFTP
- Host name:
192.168.1.102(your Pi’s IP) - Port:
22 - Username:
pi - Password:
raspberry(or your custom password)
After connecting, navigate to /var/www/html to modify index.html or add new pages.
Step 5: Secure Your Server (Best Practice)
• Change the default pi password: passwd.
• Enable the built‑in firewall: sudo apt install ufw -y, then sudo ufw allow OpenSSH and sudo ufw enable.
• Consider installing fail2ban to protect against brute‑force attempts.
Next Steps: Building Your Site
With Apache and PHP in place, you can now develop static pages with HTML/CSS or dynamic content using PHP and MySQL (install with sudo apt install mariadb-server php-mysql -y). Host your own portfolio, blog, or IoT dashboard right from your kitchen table.
Resources
Manufacturing process
- Efficient Web Server Solutions for Resource-Constrained Microcontrollers
- Raspberry Pi Unveils RP2040 MCU and $4 Pico Board
- Real‑Time Temperature & Humidity Monitoring via Web on Raspberry Pi 4
- Home Temperature & Humidity Monitor with Raspberry Pi & Web Dashboard – Real‑Time Remote Tracking
- Build a Professional Raspberry Pi Universal Remote with LIRC
- Converting RF to DC with a Raspberry Pi: Building and Troubleshooting an Envelope Detector
- Cycle Chaser: Transform Your Bike into a Nighttime Light Show with Raspberry Pi
- Wireless Node.js‑Controlled Robotic Car – Real‑Time IoT Project
- Build a Raspberry Pi NAS Server: Step‑by‑Step Guide to Secure, Wireless File Sharing
- Web Server vs. Application Server: Key Differences & How to Choose the Right One