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 apache2on 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
- Check current services. Run
sudo systemctl status nginx apache2(orhttpdon RHEL) to see if both are running. Stop one if it’s using port 80 to avoid conflicts. - Configure Apache to listen on a different port. Edit Apache’s ports configuration file (usually
/etc/apache2/ports.confon Debian/Ubuntu, or/etc/httpd/conf/httpd.confon RHEL). Change or addListen 8080and remove or comment outListen 80. - 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 theServerNameand document root as needed. - Restart Apache. Run
sudo systemctl restart apache2(orhttpd). Verify it’s listening on 8080 withsudo netstat -tulpn | grep 8080orsudo ss -tulpn | grep 8080. - 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 aserverblock that listens on port 80 and proxies requests to Apache on 127.0.0.1:8080. - Example Nginx configuration. Inside the server block, set
server_name example.com www.example.com;and then add alocation /block withproxy_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;. - 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 inconf.dis already active. - Test and reload Nginx. Run
sudo nginx -tto check for syntax errors, thensudo systemctl reload nginx(orsudo systemctl restart nginx). - 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 withcurl -I example.com– you’ll seeServer: nginxin the response, but the content is from Apache. - 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
Listenbut 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 -tcan 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-dataon Debian,apacheon RHEL) to avoid permission issues. - Set timeouts. Add
proxy_read_timeout 300s;andproxy_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 8080orfirewall-cmd --remove-port=8080/tcpso only Nginx can reach Apache. - Monitor logs. Check both
/var/log/nginx/access.logand/var/log/apache2/access.logto see which server is handling what.
Troubleshooting
- Nginx fails to start with “address already in use”. Run
sudo lsof -i :80to 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.comon 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 withcurl -I http://127.0.0.1:8080. - Requests are slow. Check if Apache is overloaded. Increase
MaxRequestWorkersin 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-phpand 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
- How to Choose an Amplifier for Magico A3 Speakers
- How to Ban a Player from Your FiveM Server
- Pokemon Go Friend Codes: How to Add & Use Them33.33 XP/day
- Google Algorithm Change: Original Reporting Boost
- Create Folder for Selected Item on Mac: Full Guide
- Data Center Location Questions for AI Workloads