Atlantic.Net Blog

How to Setup Nginx as a Reverse Proxy for Apache on Ubuntu 18.04 VPS

Hitesh Jethva
by Atlantic.Net (9posts) under VPS Hosting
0 Comments

Apache and Nginx are free, open-source and the most widely used web servers in the world. Apache is the number one server, while Nginx comes in the second place. Apache is known for its power and the backend compatibility while Nginx is known for its speed. Nginx provides a rich set of features including load balancing, security, and acceleration. Using Nginx as a reverse proxy allows you to use these features for any web application.

Configuring a reverse proxy helps you to hide the identity of your backend servers and protect your servers from attacks. A reverse proxy also performs load balancing and distributes client request across backend servers. This can very helpful when some servers become overloaded due to a sudden spike in client requests.

Using Nginx as a reverse proxy for Apache will allow both servers to work together and allow you to take advantage of the benefits of both servers. You can easily monitor what traffic goes in and out through the reverse proxy.

In this tutorial, we will learn how to install and configure Nginx as a reverse proxy for Apache on Ubuntu 18.04 VPS. We will configure Apache to run on port 8080, configure Nginx to run on port 80, and forward client requests coming on port 80 to the Apache webserver.

Prerequisites

  • A fresh Ubuntu 18.04 server VPS on Atlantic.net Cloud.
  • A valid domain name is pointed to your VPS IP address. In this tutorial, we will use example.com

Create Atlantic.Net Cloud Server

First, Login to your Atlantic.Net Cloud Server. Create a new server, choosing Ubuntu 18.04 as the operating system, with at least 2GB RAM. Connect to your Cloud Server via SSH and log in using the credentials highlighted at the top of the page.

Once you are logged into your Ubuntu 18.04 server, run the following command to update your base system with the latest available packages.

apt-get update -y

Install and Configure Apache

First, you will need to install Apache webserver on your VPS. You can install Apache by running the following command:

apt-get install apache2 -y

Once installed, start the Apache web service and enable it to start after system reboot with the following command:

systemctl start apache2
systemctl enable apache2

By default, Apache listens on port 80. You will need to configure Apache to listen on port 8080. You can do it by editing ports.conf file:

nano /etc/apache2/ports.conf

Find the following line:

Listen 80

Replace it with the following:

Listen 127.0.0.1:8080

Save and close the file. Then, open the Apache default virtual host file with the following command:

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

Find the following line:

<VirtualHost *:80>

Replace it with the following:

<VirtualHost 127.0.0.1:8080>

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

systemctl restart apache2

You can now verify that Apache web server is listening on port 8080 with the following command:

netstat -ant | grep 8080

You should see the following output:

tcp 0 127.0.0.1:8080 0.0.0.0:* LISTEN

Install and Configure Nginx

Next, you will need to install Nginx and configure it as a reverse proxy to forward requests coming on port 80 to the Apache webserver that is listening on port 8080.

You can install the Nginx web server by running the following command:

apt-get install nginx -y

Once installed, start Nginx service and enable it to start after system reboot:

systemctl start nginx
systemctl enable nginx

Next, set up the Nginx reverse proxy by creating a new virtual host file:

nano /etc/nginx/sites-available/example.com

Add the following lines:

server {
   listen 80;

   root /var/www/html/;
   index index.html index.htm;

   server_name example;

   location \ {

   proxy_set_header X-Real-IP $remote_addr;
   proxy_set_header X-Forwarded-For $remote_addr;
   proxy_set_header Host $host;
   proxy_pass http://127.0.0.1:8080;
}

}

Save and close the file. Then, enable the virtual host configuration file with the following command:

ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/

Next, verify Nginx for any syntax error with the following command:

nginx -t

If everything is correct, you should get the following output:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Finally, restart Nginx service to apply these configuration changes:

systemctl restart nginx

Test the Nginx Reverse Proxy

Nginx and Apache are installed and configured properly. Now, you can test the functionality of the Nginx reverse proxy.

To do so, open your web browser and type the URL http://example.com. You will be redirected to the Apache default page as shown in the following screen:

Conclusion

Congratulations! You have successfully installed and configured Nginx as a reverse proxy for Apache webserver. You can also configure Nginx as a reverse proxy for other applications like Tomcat, Wildfly, Node.Js and Glassfish.

If you’re ready to get started with a virtual private server for hosting your websites, visit Atlantic.Net’s VPS Hosting page to find a hosting package that’s right for you.

 

Get started with 12 months of free cloud VPS hosting

Free Tier includes:
G2.1GB Cloud VPS Server Free to Use for One Year
50 GB of Block Storage Free to Use for One Year


New York, NY

100 Delawanna Ave, Suite 1

Clifton, NJ 07014

United States

San Francisco, CA

2820 Northwestern Pkwy,

Santa Clara, CA 95051

United States

Dallas, TX

2323 Bryan Street,

Dallas, Texas 75201

United States

Ashburn, VA

1807 Michael Faraday Ct,

Reston, VA 20190

United States

Orlando, FL

440 W Kennedy Blvd, Suite 3

Orlando, FL 32810

United States

Toronto, Canada

20 Pullman Ct, Scarborough,

Ontario M1X 1E4

Canada

London, UK

14 Liverpool Road, Slough,

Berkshire SL1 4QZ

United Kingdom

Resources

We use cookies for advertising, social media and analytics purposes. Read about how we use cookies in our updated Privacy Policy. If you continue to use this site, you consent to our use of cookies and our Privacy Policy.