π Raspberry Pi β Setup Web Server (Apache/Nginx) (2025 Guide to Hosting Locally)
π§² Introduction β Turn Your Raspberry Pi into a Web Server
Want to host your own website, control a smart dashboard, or serve APIs from your Raspberry Pi? With Apache or Nginx, you can set up a powerful and lightweight web server right on your Pi. Itβs perfect for local development, IoT dashboards, camera feeds, and even small blogs.
π― In this guide, youβll learn:
- The difference between Apache and Nginx on Raspberry Pi
- How to install, configure, and serve HTML pages
- How to use PHP or Python with your server
- Real-world applications like dashboards and web control panels
βοΈ Apache vs Nginx on Raspberry Pi
π Feature | π§± Apache | π Nginx |
---|---|---|
Performance | Process-based, heavier on RAM | Event-driven, lightweight |
PHP Integration | Built-in via mod_php | Uses PHP-FPM |
Use Case | Traditional websites, CMS | APIs, static files, dashboards |
Setup Ease | Easier for beginners | Fast & lean for advanced users |
β Use Apache if you’re building with PHP/WordPress. Choose Nginx for speed and modern web apps.
π§ Install Apache Web Server on Raspberry Pi
β Step 1: Update & Install Apache
sudo apt update
sudo apt install apache2 -y
β Step 2: Test Web Server
Open a browser and go to:
http://<your_pi_ip_address>
You should see:
Apache2 Debian Default Page
β
HTML files go in /var/www/html/
.
π§ͺ Serve Your Own HTML Page
β Replace Default Page:
sudo nano /var/www/html/index.html
Insert:
<html>
<head><title>My Pi Server</title></head>
<body><h1>Hello from Raspberry Pi!</h1></body>
</html>
Save and refresh browser. Youβll see your custom page!
π Add PHP Support (Optional)
β Install PHP:
sudo apt install php libapache2-mod-php -y
β Test PHP:
sudo nano /var/www/html/info.php
Insert:
<?php phpinfo(); ?>
Visit:
http://<pi_ip_address>/info.php
β This displays PHP config. Delete the file after testing:
sudo rm /var/www/html/info.php
π Install Nginx Web Server on Raspberry Pi
β Step 1: Install Nginx
sudo apt install nginx -y
β Step 2: Start Server
sudo systemctl start nginx
sudo systemctl enable nginx
Visit:
http://<pi_ip_address>
Youβll see the Welcome to Nginx page.
π© Configure Nginx Server Block
β Serve a Custom Page:
sudo nano /var/www/html/index.html
Edit the HTML like before.
β Restart Nginx:
sudo systemctl restart nginx
β
Nginx serves pages from /var/www/html/
by default.
π§ͺ Serve Dynamic Pages with PHP-FPM (for Nginx)
β Install PHP-FPM:
sudo apt install php-fpm -y
β Configure Nginx:
Edit:
sudo nano /etc/nginx/sites-available/default
Find index index.html
and change to:
index index.php index.html index.htm;
Uncomment and update:
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
}
β Restart Services:
sudo systemctl restart php7.4-fpm
sudo systemctl restart nginx
β
Now you can run .php
files from /var/www/html
.
π¦ Useful Web Server Tools for Raspberry Pi
π§ Tool | π Purpose |
---|---|
ufw | Configure basic firewall rules |
certbot | Add HTTPS with Letβs Encrypt |
sqlite3 | Lightweight local database |
Node.js | Serve JavaScript-based web apps |
Flask/Django | Use Python frameworks for web apps |
π‘ Real-World Raspberry Pi Web Server Projects
π‘ Project Name | π Description |
---|---|
Local Weather Dashboard | Serve live sensor data with PHP/Python |
Home Automation Panel | Control GPIO and devices via HTML buttons |
Static Personal Website | Host portfolio or resume |
Streaming MJPEG Camera | Stream Raspberry Pi camera feed |
IoT REST API | Serve JSON data for sensors |
π Summary β Recap & Next Steps
You can easily set up a full-featured local or internet-accessible web server using Apache or Nginx on your Raspberry Pi. Perfect for hosting dashboards, APIs, or websites right from your own device.
π Key takeaways:
- Apache is ideal for traditional PHP sites; Nginx is faster for modern apps
- Both servers store content in
/var/www/html/
- PHP, Node.js, Flask, and Django can all run on top of these servers
- Easily expose GPIO or sensor data via HTML or API
βοΈ Real-world relevance: Use your Raspberry Pi as a local server, smart home control hub, IoT dashboard, or educational web host.
β FAQs β Raspberry Pi Web Server Setup
β Which is better: Apache or Nginx for Raspberry Pi?
β Nginx is faster and uses less memory, great for modern apps. Apache is easier with built-in PHP support.
β How do I find my Piβs IP address?
β Run:
hostname -I
Then visit http://<ip_address>
from your browser.
β Can I run both Apache and Nginx?
β Technically yes, but they must listen on different ports (e.g., Apache on 8080, Nginx on 80).
β Can I access the Pi server from outside my network?
β Yes, use port forwarding on your router or install ngrok for tunneling.
β How can I secure my Raspberry Pi server?
β
Use HTTPS via Letβs Encrypt (certbot
), change default passwords, and enable UFW firewall rules.
Share Now :