keepcalculator

06 Numbers and Powers

Nginx and Apache Together on One Server

Card No. 06·73653 · Updated · Standard formula, shown below

Running Nginx and Apache on the same server lets you use Nginx as a fast reverse proxy or static file server while Apache handles dynamic content like PHP or Python. You enter the Apache port and the domain, and you get a working setup where Nginx listens on port 80 and forwards requests to Apache on a different port (e.g., 8080). This guide walks you through the configuration step by step.

What You’ll Need

  • A Linux server (Ubuntu/Debian or CentOS/RHEL) with sudo access.
  • Both Nginx and Apache installed. If not, install them with your package manager (e.g., apt install nginx apache2 on Debian/Ubuntu).
  • Apache must be configured to listen on a non-standard port (like 8080) so it doesn’t conflict with Nginx on port 80.
  • A domain name or IP address to point to your server (optional but recommended).

Step-by-Step

  1. Check current services. Run sudo systemctl status nginx apache2 (or httpd on RHEL) to see if both are running. Stop one if it’s using port 80 to avoid conflicts.
  2. Configure Apache to listen on a different port. Edit Apache’s ports configuration file (usually /etc/apache2/ports.conf on Debian/Ubuntu, or /etc/httpd/conf/httpd.conf on RHEL). Change or add Listen 8080 and remove or comment out Listen 80.
  3. Set Apache virtual hosts. For each site, edit its virtual host file (e.g., /etc/apache2/sites-available/example.com.conf) and change the <VirtualHost *:80> to <VirtualHost *:8080>. Update the ServerName and document root as needed.
  4. Restart Apache. Run sudo systemctl restart apache2 (or httpd). Verify it’s listening on 8080 with sudo netstat -tulpn | grep 8080 or sudo ss -tulpn | grep 8080.
  5. Configure Nginx as a reverse proxy. Create a new Nginx server block file, e.g., /etc/nginx/sites-available/example.com (on Debian/Ubuntu) or /etc/nginx/conf.d/example.com.conf (on RHEL). Add a server block that listens on port 80 and proxies requests to Apache on 127.0.0.1:8080.
  6. Example Nginx configuration. Inside the server block, set server_name example.com www.example.com; and then add a location / block with proxy_pass http://127.0.0.1:8080;. Include common proxy headers: proxy_set_header Host $host;, proxy_set_header X-Real-IP $remote_addr;, proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;.
  7. Enable the Nginx site. On Debian/Ubuntu, create a symlink: sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/. On RHEL, the file in conf.d is already active.
  8. Test and reload Nginx. Run sudo nginx -t to check for syntax errors, then sudo systemctl reload nginx (or sudo systemctl restart nginx).
  9. Test the setup. Open your browser and go to http://example.com. You should see your Apache-served site. To verify which server handled the request, you can check the response headers with curl -I example.com – you’ll see Server: nginx in the response, but the content is from Apache.
  10. Optional: Serve static files directly from Nginx. For performance, you can make Nginx handle static assets (images, CSS, JS) directly by adding a location ~* \.(jpg|jpeg|png|css|js)$ { root /var/www/example.com; } block. This reduces load on Apache.

Common Mistakes

  • Both services trying to use port 80. If Apache is still listening on 80, Nginx will fail to start. Always change Apache to 8080 first.
  • Forgetting to update virtual host ports. If you only change the global Listen but not the <VirtualHost> lines, Apache may still bind to the wrong port or fail to start.
  • Missing proxy headers. Without X-Forwarded-For, Apache sees all requests as coming from 127.0.0.1, which can break logging and IP-based restrictions.
  • Not testing Nginx configuration. Skipping nginx -t can lead to a broken server if there’s a syntax error.
  • Using the same document root for both. If Nginx serves static files, it needs its own root path, not the same as Apache’s, or you’ll get duplicate content.

Tips

  • Use a dedicated user for each service. Keep the default users (www-data on Debian, apache on RHEL) to avoid permission issues.
  • Set timeouts. Add proxy_read_timeout 300s; and proxy_send_timeout 300s; in Nginx if your Apache scripts take longer than the default 60 seconds.
  • Enable gzip compression in Nginx. It can compress responses before sending to clients, reducing bandwidth.
  • Use a firewall. Block external access to port 8080 with ufw deny 8080 or firewall-cmd --remove-port=8080/tcp so only Nginx can reach Apache.
  • Monitor logs. Check both /var/log/nginx/access.log and /var/log/apache2/access.log to see which server is handling what.

Troubleshooting

  • Nginx fails to start with “address already in use”. Run sudo lsof -i :80 to find the process. Stop Apache if it’s still on 80, or change Nginx to a different port temporarily.
  • Apache shows a default page instead of your site. Check that the virtual host file is enabled (sudo a2ensite example.com on Debian) and that the document root exists and has correct permissions.
  • 502 Bad Gateway from Nginx. This means Nginx can’t reach Apache. Verify Apache is running (sudo systemctl status apache2) and listening on 127.0.0.1:8080. Test with curl -I http://127.0.0.1:8080.
  • Requests are slow. Check if Apache is overloaded. Increase MaxRequestWorkers in Apache’s mpm config, or add caching in Nginx.
  • PHP files download instead of executing. This usually means Apache isn’t processing PHP. Install and enable PHP module: sudo apt install libapache2-mod-php and restart Apache.

Frequently Asked Questions

Can I run both on the same port?

No, both services can’t bind to the same port simultaneously. You must use different ports (e.g., 80 for Nginx, 8080 for Apache).

Why use Nginx in front of Apache?

Nginx handles static files and concurrent connections more efficiently, reducing the load on Apache. It also allows you to add caching, load balancing, and SSL termination.

How do I handle SSL certificates?

You can terminate SSL at Nginx by configuring a listen 443 ssl; block and setting the certificate paths. Then proxy to Apache over HTTP on 8080.

Will my .htaccess files still work?

Yes, because Apache still processes them. Nginx passes the request to Apache, which reads the .htaccess files in the document root.

Can I use Nginx for some sites and Apache for others?

Yes, you can configure Nginx to proxy only specific server names or paths, while serving others directly or forwarding to different backends.

How do I change the Apache port on CentOS/RHEL?

Edit /etc/httpd/conf/httpd.conf and change the Listen directive to 8080. Also update any <VirtualHost> lines to use *:8080.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →