Raspberry Pi with a monitor showing Apache and Nginx status in the terminal, plus a USB hub

Web Server on Raspberry Pi: Apache vs. Nginx – Differences, Use Cases and Installation

To host your own website on a Raspberry Pi, you need a web server. A web server in front also makes Home Assistant or Portainer easier to reach, under a proper name instead of an IP address and port number. Either way, the question is usually Apache or Nginx (pronounced “engine-x”). Both serve web pages, but they work differently under the hood, which makes each a better fit for different projects.

This guide explains how the two differ, how much memory they actually use on a Pi and which one suits your project. Then you’ll set up the one you picked with PHP, or use Nginx as a reverse proxy for Home Assistant and Portainer. We tested every command in October 2026 in a Raspberry Pi OS (64-bit) environment on a Raspberry Pi 5, with Apache 2.4, Nginx 1.26 and PHP 8.4 (package versions per packages.debian.org).

In short

Use Apache for WordPress and other PHP apps, and Nginx as a reverse proxy and for static sites. Memory doesn’t decide it: neither used more than 60 MB in our tests.

  • Apache, because WordPress, Nextcloud and the like ship their rules in .htaccess files, which only Apache reads.
  • Nginx, because as a reverse proxy it puts several services behind their own names, and it serves files very efficiently.
  • Only one of them can have port 80. To run both, move one to a different port.

Key terms in a nutshell

Terms used in this guide

  • Web server: a program that takes requests from the browser and sends pages back.
  • Ports 80 and 443: the standard ports for HTTP and HTTPS. An address without a port number ends up there. Only one program can use a port at a time.
  • PHP: a programming language that builds pages on every request, as WordPress does.
  • PHP-FPM: a separate service that runs PHP files. The web server hands the request over and gets the result back.
  • .htaccess: a file in a website folder with rules such as clean URLs. Only Apache reads it.
  • Reverse proxy: a web server that doesn’t answer requests itself but forwards them to another service, Home Assistant for example.

Apache vs. Nginx: what’s the difference?

Apache: flexible, with .htaccess support

Apache spreads incoming connections across several processes with many threads. Raspberry Pi OS starts it with the event model, which handles lots of connections on little memory. If you install PHP as an Apache module (mod_php), Apache switches to the older prefork model, where every connection gets its own process with PHP built in. That’s easy to set up but uses more memory. Apache can also run PHP through PHP-FPM instead.

Apache’s big strength is .htaccess files. Apps like WordPress and Nextcloud drop their own rules there, for clean URLs or locked-down folders. Apache reads them on every request, no restart needed. For that to work, the site configuration has to allow it with AllowOverride (see Option A).

Nginx: lean and built for forwarding

Nginx starts one master process and one worker per CPU core. Each worker handles many connections at once instead of spawning a process per connection. Nginx serves static files such as images, HTML and CSS very efficiently, and it’s the go-to choice as a reverse proxy that passes requests on to other services.

Nginx doesn’t run PHP itself. It forwards PHP requests to PHP-FPM. It also ignores .htaccess files, so every rule lives in its own configuration. WordPress still runs fine with one extra line (see Option B), but rules that caching or security plugins write to .htaccess won’t take effect under Nginx. You’d have to port them by hand.

How much memory do Apache and Nginx need?

We measured both web servers on a Raspberry Pi 5 (4 GB) with the stock Raspberry Pi OS configuration: idle and under load with 10 and 100 concurrent requests. The figures show the memory actually used by the web server and PHP-FPM processes, without a database:

SetupIdle10 concurrent requests100 concurrent requests
Nginx, static pages only8 MB8 MB8 MB
Apache, static pages only8 MB13 MB19 MB
Nginx + PHP-FPM (PHP page)27 MB30 MB31 MB
Apache + mod_php (PHP page)22 MB40 MB54 MB
Apache + PHP-FPM (PHP page)27 MB42 MB60 MB

Without PHP, Nginx uses far less than Apache under load. With PHP, both are about even when idle, and under load Apache with mod_php uses just over 20 MB more. On a Pi 3, 4 or 5 that’s irrelevant. Going by these numbers, even a Raspberry Pi Zero 2 W with 512 MB would have room for either one. The apps behind the web server use far more memory, especially the database for WordPress or Nextcloud.

Which web server for which project?

ProjectPickWhy
WordPress, Joomla and other PHP appsApache (Option A).htaccess rules from the app and its plugins work out of the box
NextcloudApache (Option A)Recommended in the Nextcloud docs; Nginx with PHP-FPM is supported too
Putting Home Assistant, Portainer and other services behind their own namesNginx (reverse proxy)Lightweight and well documented as a reverse proxy
Static site, documentation, small dashboardNginx (Option B)Serves files with the least memory
Apache site already running, plus services behind a proxyBoth (Nginx in front of Apache)Nginx in front, Apache on port 8080

If you want a complete web server with a database and HTTPS, follow the full step-by-step guide Set Up a Raspberry Pi Web Server with Apache, PHP and MariaDB.

Which Raspberry Pi should you use?

Any Raspberry Pi from the Pi 3 onwards can run a web server or reverse proxy, and a Zero 2 W is enough for a small static site. The apps behind it are what need more power: for WordPress or Nextcloud with a database, go for a Raspberry Pi 5* or a Pi 4. If the Pi runs around the clock and writes to a database constantly, an SSD lasts longer and is faster than an SD card, for example an NVMe SSD* on a Pi 5 (set up an NVMe SSD on the Raspberry Pi 5) or an SSD over USB.

Links marked with * are affiliate links, which makes them advertising. If you buy through one, I earn a commission at no extra cost to you. As an Amazon Associate I earn from qualifying purchases. Learn more

Before you install: is port 80 already taken?

Apache and Nginx both want port 80. If Apache is already running, Nginx installs but doesn’t start: apt only prints Not attempting to start NGINX, port 80 is already in use., and the next time Nginx tries to start, for example after a reboot, the log shows bind() to 0.0.0.0:80 failed. If Nginx got there first, Apache fails to start with could not bind to address. So check first whether port 80 is free:

sudo ss -ltnp | grep ':80 '

No output means the port is free. If you see apache2 or nginx, a web server is already running. To switch, stop the old one and keep it from starting at boot:

sudo systemctl disable --now apache2

For Nginx the command is sudo systemctl disable --now nginx. sudo apt purge apache2 or sudo apt purge nginx removes the old server completely. If you want to keep both, read Running both on one Pi first.

Option A: Apache with PHP

Install Apache from the Raspberry Pi OS repositories:

sudo apt update
sudo apt install apache2 -y

Apache starts right away and on every boot. Open http://raspberrypi.local in a browser on another computer. If you gave your Pi a different hostname, use that instead of raspberrypi, or use the IP address from hostname -I. You’ll see the “Apache2 Debian Default Page”.

Next, add PHP. Without a version number in the package names, Raspberry Pi OS installs the current release, PHP 8.4 at the moment, and later versions arrive with regular updates:

sudo apt install php libapache2-mod-php php-mysql php-curl php-gd php-mbstring php-xml php-zip -y
sudo systemctl restart apache2

libapache2-mod-php plugs PHP into Apache; the other packages add extensions for databases, downloads, images, multibyte text, XML and ZIP archives. A test file shows whether PHP works:

echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php

Open http://raspberrypi.local/info.php. The PHP version is at the top, and “Server API” says “Apache 2 Handler”. The page reveals a lot about your system, so delete it after the test:

sudo rm /var/www/html/info.php

Allow .htaccess

To let WordPress and other apps use their .htaccess rules, enable the URL rewriting module and allow .htaccess in the website folder. Without this step, Apache answers those addresses with “404 Not Found”:

sudo a2enmod rewrite
sudo nano /etc/apache2/sites-available/000-default.conf

Add this block just before the closing </VirtualHost> line, save with Ctrl+O and Enter, and exit nano with Ctrl+X:

<Directory /var/www/html>
    AllowOverride All
</Directory>

Test the configuration and restart Apache:

sudo apache2ctl configtest
sudo systemctl restart apache2

configtest should say “Syntax OK”. The “AH00558: Could not reliably determine the server’s fully qualified domain name” notice is harmless; Troubleshooting shows how to silence it. For the next steps, including a database, multiple websites and HTTPS, see Raspberry Pi web server with Apache, PHP and MariaDB.

Option B: Nginx with PHP-FPM

Install Nginx:

sudo apt update
sudo apt install nginx -y

Nginx starts right away and on every boot. http://raspberrypi.local now shows “Welcome to nginx!”. If you don’t need PHP, say for a plain HTML site, just put your files in /var/www/html and you’re done.

Install PHP-FPM

For PHP, install PHP-FPM with the same extensions as for Apache, again without a version number:

sudo apt install php-fpm php-mysql php-curl php-gd php-mbstring php-xml php-zip -y

Older guides use php8.2-fpm here. Those packages no longer exist in current Raspberry Pi OS, and apt stops with “Unable to locate package”. Using the unversioned package name also keeps you safe at the next PHP upgrade. PHP-FPM then runs as its own service, waiting for requests from Nginx.

Set up your own PHP site

Create a folder for your site with a small PHP test page and open a new Nginx configuration file:

sudo mkdir -p /var/www/myproject
echo "<?php echo 'PHP ' . PHP_VERSION . ' is running'; ?>" | sudo tee /var/www/myproject/index.php
sudo nano /etc/nginx/sites-available/myproject

Paste in this configuration:

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name _;
    root /var/www/myproject;
    index index.php index.html;

    location / {
        try_files $uri $uri/ =404;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php-fpm.sock;
    }

    location ~ /\.ht {
        deny all;
    }
}

The key lines: default_server makes this the site that answers requests by IP address. root points to your folder. The location ~ \.php$ block sends every PHP file to PHP-FPM, and /run/php/php-fpm.sock works whatever PHP version is installed. The last block denies access to any .htaccess files lying around.

Now disable the bundled default site, enable yours, test the configuration and reload:

sudo rm /etc/nginx/sites-enabled/default
sudo ln -s /etc/nginx/sites-available/myproject /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx

nginx -t has to report “syntax is ok” and “test is successful”, otherwise Nginx won’t load the new configuration. Then open http://raspberrypi.local; the test page says “PHP 8.4.x is running”.

⚠️ Don’t leave the default site enabled

If you keep the default site enabled and only give your own site a name, requests by IP address still go to the default site. It knows nothing about PHP and serves your PHP files as downloads, complete with source code and possibly your database passwords. That’s exactly how the configurations in many older guides behave.

WordPress and clean URLs on Nginx

WordPress and many other PHP apps route addresses like /2026/10/hello-world/ through their index.php. Apache does that via .htaccess; on Nginx, open your configuration /etc/nginx/sites-available/myproject and replace the try_files line in the location / block with this one, as the WordPress documentation recommends:

try_files $uri $uri/ /index.php?$args;

Then run sudo nginx -t and sudo systemctl reload nginx again. Without this line, Nginx answers those addresses with “404 Not Found”.

Nginx as a reverse proxy for Home Assistant and Portainer

As a reverse proxy, Nginx accepts every request on port 80 and passes it to the right service based on the name in the address. That way you reach Home Assistant, Portainer and your website by name instead of IP addresses and port numbers. All you need is Nginx installed (the first command in Option B); the proxy doesn’t need PHP. If Apache is already running on the Pi, read Running both on one Pi first.

Diagram: the browser sends requests to Nginx on the Raspberry Pi, which forwards them by name to a website, Home Assistant or Portainer
Nginx accepts every request on port 80 or 443 and hands it to the right service based on the name.

Point the names at your Pi

For Nginx to match a request, the name in the browser has to resolve to your Pi’s IP address. Names like homeassistant.local won’t work, because they belong to the device running Home Assistant and bypass the Pi. You have two options:

  • Your own domain: create subdomains like ha.your-domain.com with your domain provider, point them at your internet connection via dynamic DNS and forward ports 80 and 443 on your router to the Pi. Only do this right before you set up HTTPS (see below), otherwise Home Assistant and Portainer are exposed to the internet unencrypted.
  • Home network only: add the names to your computer’s hosts file, for example 192.168.1.50 ha.your-domain.com portainer.your-domain.com with your Pi’s IP address. On Windows the file is C:\Windows\System32\drivers\etc\hosts and needs admin rights to save; on macOS and Linux it’s /etc/hosts. To cover every device on your network at once, add a local DNS record in Pi-hole or AdGuard Home.

Example 1: Home Assistant

Create a configuration file for Home Assistant:

sudo nano /etc/nginx/sites-available/homeassistant
server {
    listen 80;
    listen [::]:80;
    server_name ha.your-domain.com;

    location / {
        proxy_pass http://192.168.1.20:8123;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }
}

In the proxy_pass line, replace the IP address 192.168.1.20 and the port 8123 with those of your Home Assistant device. The port depends on how it’s installed: since 2026.8, new Home Assistant OS installations listen on port 80, older ones keep 8123, and Home Assistant in a Docker container always uses 8123 (details in Home Assistant without :8123). If Home Assistant runs as a container on the same Pi, the target is http://127.0.0.1:8123. The last two proxy_set_header lines pass WebSocket connections through, which Home Assistant uses to update its dashboard live. Without them, Nginx rejects WebSocket connections with “502 Bad Gateway”.

Enable the configuration, test it and reload Nginx:

sudo ln -s /etc/nginx/sites-available/homeassistant /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx

Important: Home Assistant blocks requests that arrive through a proxy by default. In Home Assistant, go to Settings → System → Network, expand Reverse proxy in the HTTP server section, turn on Trust X-Forwarded-For and add your Pi’s IP address under Trusted proxies. Click Save and restart; once Home Assistant is back up, it asks whether to keep the new settings. Click Confirm within 5 minutes, or Home Assistant rolls the change back automatically. Versions before 2026.8 don’t have this page yet; there, use_x_forwarded_for and trusted_proxies go into the http: section of configuration.yaml (HA docs).

Home Assistant is now at http://ha.your-domain.com.

Example 2: Portainer

Out of the box, Portainer only serves its interface over HTTPS on port 9443, with a self-signed certificate. If Nginx forwards there while you reach Nginx itself over plain HTTP, Portainer marks its login cookie as HTTPS-only. The browser drops it and the login fails, which matches Portainer’s own notes. So for the proxy, also publish the HTTP port 9000, but only to the Pi itself (127.0.0.1). That means recreating the container; your settings stay in the portainer_data volume:

docker stop portainer
docker rm portainer
docker run -d -p 8000:8000 -p 9443:9443 -p 127.0.0.1:9000:9000 --name portainer --restart=always \
  -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/portainer-ce:lts

The command is the one from Install Portainer on Raspberry Pi, plus -p 127.0.0.1:9000:9000. Next, the Nginx configuration:

sudo nano /etc/nginx/sites-available/portainer
server {
    listen 80;
    listen [::]:80;
    server_name portainer.your-domain.com;

    location / {
        proxy_pass http://127.0.0.1:9000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }
}

Portainer needs the WebSocket lines for its container console. Enable the configuration and reload Nginx:

sudo ln -s /etc/nginx/sites-available/portainer /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx

Portainer is now at http://portainer.your-domain.com. Everything else still goes to the default site, which is your site from Option B or the Nginx welcome page.

HTTPS for the reverse proxy

With your own domain pointing at your connection and ports 80 and 443 forwarded, Certbot fetches free Let’s Encrypt certificates and adds them to the Nginx configuration for you:

sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d ha.your-domain.com

Add another -d for each extra name. Certbot also sets up a timer that renews the certificates automatically. How to make your Pi reachable from the internet, and what to lock down first, is covered step by step in Raspberry Pi web server with Apache, PHP and MariaDB. If you can’t forward ports, for example because your connection has no public IPv4 address, use a Cloudflare Tunnel, which handles HTTPS for you. We checked the plugin and the renewal timer; issuing a certificate needs a real domain, so that part follows the documentation.

Running both on one Pi: Nginx in front of Apache

If an Apache site is already running on your Pi and you want Nginx as a reverse proxy as well, move Apache to port 8080. Nginx takes over port 80, serves static files itself and passes PHP requests to Apache. Switch Apache first:

sudo sed -i 's/^Listen 80$/Listen 8080/' /etc/apache2/ports.conf
sudo sed -i 's/<VirtualHost \*:80>/<VirtualHost *:8080>/' /etc/apache2/sites-available/000-default.conf
sudo systemctl restart apache2
sudo ss -ltnp | grep ':8080 '

The first line changes the port Apache listens on, the second the port of the default site. The last command should show apache2 on port 8080. Then install Nginx with sudo apt install nginx -y. You don’t need PHP-FPM here, since PHP keeps running through Apache. Open the default Nginx configuration and replace everything in it with the configuration below:

sudo nano /etc/nginx/sites-available/default
server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name _;
    root /var/www/html;
    index index.php index.html;

    location / {
        try_files $uri $uri/ @apache;
    }

    location ~ \.php$ {
        proxy_pass http://127.0.0.1:8080;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }

    location @apache {
        proxy_pass http://127.0.0.1:8080;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

If you went through Option B earlier, your own site is the default server there. nginx -t rejects two default servers on port 80, so take your Option B site out and enable the default configuration again:

sudo rm /etc/nginx/sites-enabled/myproject
sudo ln -s /etc/nginx/sites-available/default /etc/nginx/sites-enabled/

After sudo nginx -t and sudo systemctl restart nginx, Nginx serves existing files such as images and HTML from /var/www/html itself. PHP files and any address without a matching file go to Apache with the original address, so WordPress’s .htaccess rules keep working. To test, open your site at http://raspberrypi.local as usual; it should look exactly as before. Add the proxy configurations for Home Assistant and Portainer alongside, as shown above.

Troubleshooting

ProblemCauseFix
Nginx won’t start, the log shows “bind() to 0.0.0.0:80 failed”Port 80 is taken, usually by Apachesudo ss -ltnp | grep ':80 ' shows the service. Stop it or move Apache to port 8080
The browser downloads a .php file instead of showing itNginx default site still enabled, or no PHP blockRemove /etc/nginx/sites-enabled/default, use your own site with default_server and a PHP block (Option B)
“502 Bad Gateway”PHP-FPM isn’t running, wrong socket path, or the proxy target is downCheck systemctl status php8.4-fpm (version number from php -v), use the socket /run/php/php-fpm.sock, test the target IP and port
Home Assistant shows “400: Bad Request” or blocks requests behind the proxyProxy not marked as trusted, or the settings weren’t confirmed after the restartTurn on “Trust X-Forwarded-For”, add the Pi’s IP under “Trusted proxies” and click “Confirm” after the restart
Portainer login keeps bouncing back to the login pageThe proxy forwards over HTTP to port 9443, so the login cookie is HTTPS-onlyForward to http://127.0.0.1:9000 or run Nginx with HTTPS
Apache says “AH00558: Could not reliably determine the server’s fully qualified domain name”No global server name set; harmlessecho "ServerName localhost" | sudo tee /etc/apache2/conf-available/servername.conf, sudo a2enconf servername, sudo systemctl reload apache2
“403 Forbidden”Folder without an index file, or wrong permissionsCreate index.php or index.html, check permissions (chmod and chown)
WordPress clean URLs return “404 Not Found” on NginxNginx ignores .htaccesstry_files $uri $uri/ /index.php?$args; in the location / block

Conclusion

On a Raspberry Pi, your project decides, not memory: Apache when a PHP app brings its own .htaccess rules, Nginx as a reverse proxy and for static sites. Only one of them can have port 80, so check before you install. Install PHP without a version number and your setup survives the next PHP upgrade. Once PHP is involved, switch off the Nginx default site. If you need both, put Nginx in front of Apache.

Frequently asked questions

Can I run Apache and Nginx on the same Raspberry Pi?

Yes, but not both on port 80. If you install Nginx while Apache is running, Nginx won’t start. Move Apache to port 8080 and let Nginx take requests on port 80 and pass PHP pages to Apache.

Which PHP version does Raspberry Pi OS install?

Current Raspberry Pi OS ships PHP 8.4. Install the packages without a version number, so php-fpm rather than php8.4-fpm. Later versions then arrive with regular updates, and you won’t need the php8.2 commands from older guides.

Does WordPress work with Nginx?

Yes. With PHP-FPM and the line try_files $uri $uri/ /index.php?$args; clean URLs work too. Rules that plugins write to an .htaccess file don’t take effect under Nginx, though, and have to be added to the Nginx configuration by hand.

How much memory does a web server need on a Raspberry Pi?

Very little. In our tests on a Raspberry Pi 5, Apache and Nginx with PHP used at most 60 MB even with 100 concurrent requests, and Nginx without PHP only 8 MB. The apps behind them, such as the database, use far more.

How do I make my web server reachable from the internet?

If you have a public IPv4 address, forward ports 80 and 443 to the Pi on your router and set up a dynamic DNS name, then add HTTPS with Certbot. Without a public IPv4 address, use a Cloudflare Tunnel, which works without port forwarding.

Similar Posts