Running both Nginx and Apache2 on the same Ubuntu server frequently triggers the dreaded bind() to 0.0.0.0:80 failed (98: Address already in use) systemd startup failure because only one web server daemon can bind to TCP ports 80 and 443 on a single IP address at a time.
Whether you are migrating from Apache2 to an event-driven Nginx stack for higher concurrency, or removing Nginx to re-activate an Apache2 virtual host that relies on complex .htaccess rewrite rules, executing the transition cleanly prevents downtime and preserves your TLS certificates. Alongside our Ubuntu 24.04 Server Security Hardening Guide, this tutorial covers both migration paths and unifying PHP-FPM Unix sockets.
mpm_event)php8.3-fpm (/run/php/php8.3-fpm.sock)Key Takeaways
- Stop & Mask Before Switching Ports: Always run
sudo systemctl disable --nowon the outgoing web server before starting the replacement daemon on ports80and443. - Use Shared
php8.3-fpmUnix Sockets: Configure both Nginx (fastcgi_pass) and Apache (proxy_fcgi) to communicate with/run/php/php8.3-fpm.sockrather than legacymod_phpso switching web servers requires zero PHP reconfiguration. - Never Purge
/etc/letsencrypt/: Uninstalling Nginx or Apache viaapt purgenever deletes your Certbot SSL certificates in/etc/letsencrypt/live/. Existing certificates remain ready for instant reuse.
Architectural Comparison: Nginx vs Apache2 (mpm_event) on Ubuntu 24.04
Deciding between Nginx and Apache2 on modern Ubuntu servers comes down to static asset concurrency, memory footprint on small VPS instances, and whether your application depends on per-directory .htaccess overrides.
| Architectural Metric | Nginx (Event-Driven) | Apache 2.4 (mpm_event + PHP-FPM) | Apache 2.4 (Legacy mpm_prefork) |
|---|---|---|---|
| Worker Concurrency Model | Asynchronous Non-Blocking Event Loop | Hybrid Multi-Threaded Event Workers | 1 Process Per Connection (Heavy RAM) |
| Idle RAM Usage (1,000 Conns) | ~28 MB | ~85 MB | 650 MB+ (Prone to OOM Crashes) |
.htaccess Per-Directory Support | No (Centralized in server {} block) | Yes (Native .htaccess Parsing) | Yes |
| Reverse Proxy & Load Balancing | Industry Standard (Sub-Millisecond) | Supported via mod_proxy | Slow Under High Concurrency |
Method 1: How to Switch from Apache2 to Nginx on Ubuntu 24.04
To migrate an active Ubuntu server from Apache2 to Nginx, stop and mask the apache2 systemd service to release TCP ports 80 and 443, install Nginx with php8.3-fpm, and point your Nginx server {} block to your existing document root and Let’s Encrypt certificate paths.
# 1. Stop and disable Apache2 so it releases ports 80 and 443
sudo systemctl disable --now apache2
# 2. Verify ports 80 and 443 are free
sudo ss -tulpn | grep -E ':80|:443'
# 3. Install Nginx and PHP 8.3 FPM
sudo apt update && sudo apt install nginx php8.3-fpm -y
sudo systemctl enable --now nginx php8.3-fpm
After enabling Nginx, apply our Nginx Gzip & Brotli Compression Configuration and point ssl_certificate to your existing certificates from our Let’s Encrypt Wildcard SSL Setup Guide.
Method 2: How to Completely Remove Nginx & Re-Activate Apache2
If a legacy web application requires complex .htaccess rules and you want to completely remove Nginx and re-activate Apache2, stop the Nginx daemon, purge the Nginx packages, and enable Apache’s modern mpm_event and proxy_fcgi modules.
# 1. Stop Nginx and purge Nginx packages cleanly
sudo systemctl disable --now nginx
sudo apt purge nginx nginx-common nginx-core -y
sudo apt autoremove -y
# 2. Enable Apache2 with modern event MPM and PHP-FPM socket proxy
sudo a2dismod mpm_prefork 2>/dev/null || true
sudo a2enmod mpm_event proxy_fcgi setenvif rewrite ssl headers
sudo a2enconf php8.3-fpm
# 3. Unmask, enable, and start Apache2
sudo systemctl unmask apache2
sudo systemctl enable --now apache2
Frequently Asked Questions (FAQ)
How do I fix bind() to 0.0.0.0:80 failed (98: Address already in use) when starting Nginx or Apache?
Run sudo ss -tulpn | grep ':80' to identify which process is holding port 80. Almost always, the other web server (apache2 or nginx) is still running in the background; stop it with sudo systemctl disable --now apache2 and start your target server.
Can I run Nginx and Apache at the same time on the same Ubuntu server?
Yes, by configuring Nginx as a front-end reverse proxy listening on public ports 80 and 443, and changing Apache’s /etc/apache2/ports.conf to listen strictly on 127.0.0.1:8080 so Nginx serves static files directly and proxies dynamic requests to Apache.
