How To install and configure Apache alongside Nginx?

By admin, 1 January, 2022

To install and configure Apache alongside Nginx, you can set up one of them as the primary web server listening on port 80, while the other listens on a different port (e.g., Apache on port 8080). Here's a basic guide to achieve this on a Linux system:

Install Apache and Nginx:

Install Apache:

sudo apt-get update
sudo apt-get install apache2

Install Nginx:

sudo apt-get install nginx

Configure Apache:

By default, Apache listens on port 80. You can keep it as is or change it to another port.

Edit Apache's configuration file:

sudo nano /etc/apache2/ports.conf

Change the port Apache listens on if needed. For example

Listen 8080

Save and close the file.

Configure Nginx:

Edit Nginx's default server block configuration:

sudo nano /etc/nginx/sites-available/default

Modify the server block to proxy requests to Apache. You'll add a new location block inside the existing server block. For example:

server {
listen 80;

location / {
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;

Save and close the file.

Restart Both Servers:

Restart Apache:

sudo service apache2 restart

Restart Nginx:

sudo service nginx restart

Now, Nginx will act as a reverse proxy, forwarding requests to Apache. You can access your website through Nginx on port 80, and Nginx will handle requests and pass them to Apache running on port 8080.

Remember to adjust firewall settings if necessary to allow traffic on the ports you've configured. Additionally, this is a basic setup; depending on your requirements, you may need to configure additional settings like SSL, security configurations, or load balancing.

Term Reference