How to configure Nginx as a reverse proxy for web server and Apache on CentOS 8

How to configure Nginx as a reverse proxy for web server and Apache on CentOS 8

As you know, both Apache and Nginx are free, open source and powerful web servers all over the world. Apache is known for its powerful features, and Nginx is known for its speed. Both have some disadvantages and advantages. Nginx uses PHP-FPM to load PHP files, while Apache loads PHP itself. Apache is used for the dynamic part of the website, while Nginx provides static files such as graphics, CSS and js files.

It is best to use them on a single server at the same time and benefit from them. In this tutorial, we will show you how to use Nginx as a web server and a reverse proxy for Apache web server on CentOS 8.

prerequisites

  • Server running CentOS 8.
  • The domain name pointing to the server IP.
  • The root password is configured for the server.

Install Apache and PHP

First, you will need to install Apache web server and PHP in the server. You can use the following command to install:

dnf install httpd php php-cli -y

After the installation is complete, use the following command to start the Apache service and enable it to start when the system reboots:

systemctl start httpdsystemctl enable httpd

Configure Apache web server

By default, the Apache web server listens on port 80. Therefore, you will need to configure the Apache web server to listen on port 8080. You can do this by editing the main Apache configuration file:

nano /etc/httpd/conf/httpd.conf

Change the post from 80 to 8080 as follows:

Listen 8080

Save and close the file, then restart the Apache service to apply the changes:

systemctl restart httpd

Next, use the following command to check the Apache listening port:

ss -antpl | grep httpd

You should get the following output:

LISTEN    0         128                      *:8080                   *:*        users:(("httpd",pid=1693,fd=4),("httpd",pid=1692,fd=4),("httpd",pid=1691,fd=4),("httpd",pid=1688,fd=4))

Next, create an info.php file in the Apache Web root directory:

nano /var/www/html/info.php

Add the following line:

<?php phpinfo(); ?>

Save and close the file when you are done.

Next, open your web browser and visit the URL http://your server-ip:8080/info.php. You should see the following page:

Install Nginx web server

Next, you will need to install the Nginx package on the server. You can use the following command to install:

dnf install nginx -y

Once Nginx is installed, start the Nginx service and use the following command to enable it to start when the system restarts:

systemctl start nginxsystemctl enable nginx

Configure Nginx as a reverse proxy for Apache

Now, you need to configure Nginx as a reverse proxy to forward all requests from port 80 to the Apache web server that is listening on port 8080.

First, create a new Nginx virtual host configuration file using the following command:

nano /etc/nginx/conf.d/proxy.conf

Add the following line:

server {
    listen 80;
    server_name test.example.com;

    location ~ .php$ {
        proxy_pass http://your-server-ip:8080;
        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 when you are done. Then, restart the Nginx web server to apply the changes:

systemctl restart nginx

Now, open your web browser and visit the URL http://test.example.com/info.php. You should see the following page:

PHP information via reverse proxy

As you can see, PHP is provided by the Nginx web server.

in conclusion

In the above guide, you learned how to configure Nginx as a web server and reverse proxy for Apache. I hope this will help you improve the performance of your website.

Sidebar