🌍 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
PerformanceProcess-based, heavier on RAMEvent-driven, lightweight
PHP IntegrationBuilt-in via mod_phpUses PHP-FPM
Use CaseTraditional websites, CMSAPIs, static files, dashboards
Setup EaseEasier for beginnersFast & 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
ufwConfigure basic firewall rules
certbotAdd HTTPS with Let’s Encrypt
sqlite3Lightweight local database
Node.jsServe JavaScript-based web apps
Flask/DjangoUse Python frameworks for web apps

πŸ’‘ Real-World Raspberry Pi Web Server Projects

πŸ’‘ Project NameπŸ” Description
Local Weather DashboardServe live sensor data with PHP/Python
Home Automation PanelControl GPIO and devices via HTML buttons
Static Personal WebsiteHost portfolio or resume
Streaming MJPEG CameraStream Raspberry Pi camera feed
IoT REST APIServe 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 :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

🌍 Raspberry Pi – Setup Web Server (Apache/Nginx)

Or Copy Link

CONTENTS
Scroll to Top