Using Apache with NGINX as a reverse proxy involves configuring NGINX to forward incoming requests to Apache, which then serves the content. This setup can provide benefits like load balancing, SSL termination, and improved performance. Here's a general outline of the steps to set up Apache with NGINX as a reverse proxy:

  1. Install and Configure Apache:

    sudo apt-get install libapache2-mod-php
    sudo a2enmod php
    
  2. Install and Configure NGINX:

  3. Configure Apache for the Application:

  4. Configure NGINX as a Reverse Proxy:

    server {
        listen 80;
        server_name yourdomain.com;
    
        location / {
            proxy_pass http://apache_ip_address:apache_port;
            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;
        }
    }
    
    

    Replace yourdomain.com with your actual domain, and apache_ip_address and apache_port with the IP address and port where your Apache server is listening.

    Another example of nginx server block for reverse proxy

    # /etc/nginx/conf.d/mydomain.conf
    server {
        listen 80;
        server_name mydomain.com;
    
        location / {
            proxy_pass <http://127.0.0.1:8080>;  # Apache's default port
            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 ~ \\.php$ {
            proxy_pass <http://127.0.0.1:8080>;  # Apache's default port
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;  # Use your PHP-FPM socket
        }
    }
    

    In this example, the first location block proxies non-PHP requests to Apache, while the second location block proxies requests for PHP files. Make sure to adjust the PHP-FPM socket path to match your PHP-FPM setup.

  5. Test and Apply Changes:

  6. Secure Your Setup:

  7. Monitor and Maintain:

Setting up NGINX and Apache on the same Ubuntu PC involves installing both web servers and then configuring them to work together. Here's a step-by-step guide:

  1. Install Apache and NGINX:

    Open a terminal and run the following commands to install Apache and NGINX:

    sudo apt update
    sudo apt install apache2 nginx
    
    
  2. Configure Apache:

    After installing Apache, it should automatically start as a service. You can test it by opening a web browser and navigating to http://localhost.

  3. Stop Apache Service:

    Before proceeding to configure NGINX, stop the Apache service:

    sudo systemctl stop apache2
    
    
  4. Configure NGINX:

    Create an NGINX configuration file to proxy requests to Apache. Create a new .conf file in /etc/nginx/conf.d/. For example:

    sudo nano /etc/nginx/conf.d/apache.conf
    
    

    Add the following configuration:

    server {
        listen 80;
        server_name yourdomain.com;  # Replace with your domain or IP
    
        location / {
            proxy_pass <http://127.0.0.1:8080>;  # Apache's default port
            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 the file and exit the text editor.

  5. Test NGINX Configuration:

    Run a configuration test to ensure there are no syntax errors:

    sudo nginx -t
    
    
  6. Start NGINX:

    If the test is successful, start NGINX:

    sudo systemctl start nginx
    
    
  7. Enable Apache:

    While NGINX is acting as a reverse proxy, you can run Apache on a different port. For example, let's change Apache's default port to 8080. Open the Apache ports configuration file:

    sudo nano /etc/apache2/ports.conf
    
    

    Change the Listen directive to use port 8080:

    Listen 8080
    
    

    Save the file and exit the text editor.

  8. Restart Apache:

    Restart Apache to apply the configuration changes:

    sudo systemctl restart apache2
    
    
  9. Access the Setup:

    Now, you should be able to access your application through NGINX by visiting http://yourdomain.com, where yourdomain.com is your server's domain or IP address.

If you've set up Apache with virtual hosts (vhosts) and you want to access these vhosted websites through NGINX as a reverse proxy on the same PC, you need to configure NGINX to proxy requests to the appropriate Apache vhost based on the domain or hostname. Here's how you can do that: