How To Set Up Nginx as a Reverse Proxy for Apache on Debian 11

Set Up Nginx as a Reverse Proxy for Apache on Debian 11

In this tutorial, we will show you how to set up Nginx as a reverse proxy for Apache on Debian 11. For those of you who didn’t know, Nginx and Apache both are free, open-source, and most popular web servers around the world. Apache is known for its power while Nginx is known for its speed. Both have some pros and cons. Nginx will be used as a frontend web server that will handle client requests and send them to Apache, which will be our backend web server, and return the requested response. This procedure allows obtaining various benefits from performance to safety.

This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo‘ to the commands to get root privileges. I will show you through the step-by-step installation of the Nginx as a reverse proxy for Apache on a Debian 11 (Bullseye).

Set Up Nginx as a Reverse Proxy for Apache on Debian 11 Bullseye

Step 1. Before we install any software, it’s important to make sure your system is up to date by running the following apt commands in the terminal:

sudo apt update
sudo apt upgrade

Step 2. Installing Apache Web Server on Debian 11.

Now we install Apache Web Server on the Debian system by running the following command below:

sudo apt install apache2 apache2-utils

Confirm Apache build and version:

apache2 -v

Before starting the configurations, make sure that Apache services are running on your system. Run the following command to check apache services status:

sudo systemctl status apache2
sudo systemctl start apache2
sudo systemctl enable apache2

Next, edit the Apache port configuration file:

nano /etc/apache2/ports.conf

Find and change the following lines to have apache running on port 8080, accessible only from the localhost:

NameVirtualHost 127.0.0.1:8080
Listen 127.0.0.1:8080

Save and close the file then edit the Apache default configuration file:

nano /etc/apache2/sites-enabled/000-default.conf

Change the default port from 80 to 8000 as shown below:

<VirtualHost *:8000>

Save and close the file when you have finished. Then, restart the Apache web service to apply all the configuration changes:

sudo systemctl restart apache2

Once successfully installed, you should be able to view the test Apache web page through your web browser:

http://your-server-ip-address:8000

Set Up Nginx as a Reverse Proxy for Apache on Debian 11 Bullseye

Step 3. Installing Nginx on Debian 11.

Now we run the following command below to install Nginx to your Debian system:

sudo apt install nginx

After the installation is complete, start Nginx and add it to automatically start on your system start-up using:

sudo systemctl start nginx
sudo systemctl enable nginx

Verify the installation:

nginx -v

Next, configure Nginx as a reverse proxy to pass the incoming requests to the Apache server with the following command:

nano /etc/nginx/sites-enabled/default

Paste the following configuration in your file then save and exit:

server {

listen 80;
index index.php index.html index.htm;

server_name your-server-ip;
                
location / {
proxy_pass http://localhost:8000;
proxy_set_header Host $http_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 verify the Nginx for any syntax error with the following command below:

nginx -t
sudo systemctl restart nginx

Step 4. Test Out the Reverse Proxy Server.

Nginx and Apache are installed and configured properly. Now, you can test the functionality of the Nginx reverse proxy. Now open your web browser and type the URL http://your-server-ip-address. You should see the Apache webserver default page on the following screen:

Set Up Nginx as a Reverse Proxy for Apache on Debian 11

Congratulations! You have successfully set up Nginx reverse proxy. Thanks for using this tutorial to configure Nginx as a reverse proxy for Apache on Debian 11 Bullseye. For additional help or useful information, we recommend you check the official Nginx website.

VPS Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “VPS Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!