Apache and Nginx are very popular and powerful web servers used to host websites on the internet. Each web server has its own pros and cons.
When you are planning to host multiple websites on a single server which has varied requirements, you should use Apache as a web server and use Nginx as a reverse proxy server in front of the Apache webserver to handle a large number of requests.
This post will show you how to use Nginx as a reverse proxy in front of the Apache web server on Fedora.
Step 1 – Install Apache and PHP
First, install Apache and PHP packages with the following command.
dnf install httpd php php-cli -y
Next, you will need to configure Apache to run as a backend web server on a non-standard port. You can do it by editing the Apache main configuration file.
nano /etc/httpd/conf/httpd.conf
Change the listen port from 80 to 8080 as shown below.
Listen 8080
Save and close the file, then restart the Apache service to implement the changes.
systemctl restart httpd
Next, verify the Apache listening port using the following command.
ss -antpl | grep httpd
You will see the following output.
LISTEN 0 511 *:8080 *:* users:(("httpd",pid=11652,fd=4),("httpd",pid=11651,fd=4),("httpd",pid=11650,fd=4),("httpd",pid=11648,fd=4))
Now, create a sample PHP file to test the Apache server.
nano /var/www/html/info.php
Add the following code.
<?php phpinfo(); ?>
Save and close the file, then open your web browser and access the PHP page using the URL http://your-server-ip:8080/info.php. You will see the following screen.
Step 2 – Configure Nginx as a Reverse Proxy
Now, you will need to configure Nginx as a reverse proxy for the Apache backend server.
First, install the Nginx server with the following command.
dnf install nginx -y
Next, start and enable the Nginx service with the following command.
systemctl start nginx systemctl enable nginx
Next, create an Nginx virtual host configuration file.
nano /etc/nginx/conf.d/proxy.conf
Add the following lines.
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, then restart the Nginx service to apply the changes.
systemctl restart nginx
Step 3 – Verify Nginx Server
At this point, Nginx is installed and configured as a reverse proxy for the Apache server. You can now access your PHP page via Nginx using the URL http://test.example.com.
Conclusion
In this post, we showed you how to install Apache and configured it as a backend server. Then, we explained how to use Nginx as a reverse proxy to forward all requests to the Apache web server. Now you can implement high-performance websites on a single server. You can now try to use Nginx as a reverse proxy on dedicated server hosting from Atlantic.Net!