How to Configure Nginx and Apache Together on the same Ubuntu VPS or Dedicated Server

Nginx-and-Apache

How to Configure Nginx and Apache Together on the same Ubuntu VPS or Dedicated Server

Posted on |
Configure Nginx and Apache

Apache and Nginx are powerful web servers as per Dataservermarket. However, Apache and Nginx have various drawbacks. Apache uses server memory, and Nginx processes the dynamic content that requires PHP-fpm. Dataservermarket will provide a comprehensive guide on configuring Apache and Nginx. Before diving into the blog, Dataservermarket will help you guide the Apache and Nginx configuration.

The article takes you on configuring Apache and Nginx to work together by Dataservermarket.

What are Nginx and Apache?

Nginx and Apache are the powerful web servers often used in PHP. Nginx and Apache can run on an identical virtual machine with multiple hosting websites. The general solution for running Nginx and Apache on a single server is to use various IP addresses or different port numbers.

IPv4 and IPv6 addresses configure servers running on Nginx and Apache. Having a different port number for Nginx and Apache, such as 81 or 8080, is another solution for Nginx and Apache; however, sharing URLs is impossible. In the given tutorial, you will configure Nginx and Apache.

The code changes are required to keep Nginx and Apache as a reverse proxy when the SSL sites of Nginx and Apache are configured. To avoid this, you need to install Nginx and Apache module called mod_rpaf that rewrites environment variables so that Nginx and Apache directly handle requests from web clients. Let’s start the configuration of Nginx and Apache. Before starting with the structure of Nginx and Apache, let’s first understand the pre-requisite of configuring Nginx and Apache.

Pre-requisite for configuring Nginx and Apache:

  • A Ubuntu VPS for configuring Nginx and Apache
  • Initial Ubuntu 18.04 server set up for Nginx and Apache configuration
  • Sudo non-root user and firewall for Nginx and Apache
  • Qualified domain names for IP address to configure Nginx and Apache

So, these were the pre-requisites for configuring Nginx and Apache. Let’s dive into the configuration of Nginx and Apache.

Overview of configuring Apache and Nginx by Dataservermarket:

Dataservermarket recommends configuring Apache and Nginx or Nginx and Apache together. Nginx is designed to serve high traffic that is an excellent web server. Apache is another popular web server handling half of the world’s active websites. One can combine the power of Apache and Nginx. Apache with Nginx or Ngnix and Apache will make a good combination as per Dataservermarket. Nginx serves the purpose of a static web server, while Apache handles the back-end process. So, let’s understand the complete process of configuring Apache with Nginx described by Dataservermarket.

Configuring Apache with Nginx by Dataservermarket:

The first step recommended by Dataservermarket is to set different ports for Apache with Nginx. It means you can leave port 80 for Apache with Nginx and assign a different port to Apache as per Dataservermarket.

Install Nginx:

Dataservermarket recommends configuring and installing Apache Nginx, serving as the website’s front-end.

Download from the apt-get by issuing the command:

sudo apt-get install nginx

Configure the virtual host of Apache Nginx on the front-end once it is downloaded.

sudo nano /etc/nginx/sites-available/example

Activate the virtual host of Apache Nginx by issuing the command:

sudo ln -s /etc/nginx/sites-available/example /etc/nginx/sites-enabled/example

Delete the Apache Nginx default server block:

sudo rm /etc/nginx/sites-enabled/default

Install Apache:

Dataservermarket recommends configuring and installing Nginx and Apache, serving as the website’s back-end.

Install the Apache Nginx or Nginx and Apache with the following command:

sudo apt-get install apache2

Nginx is not started, and thus, Apache starts running on port 80. Dataservermarket suggests Apache listen on different ports so that Apache with Nginx works together.

For configuring Apache Nginx or Nginx and Apache, open Apache ports.conf file:

sudo nano /etc/apache2/ports.conf

Look for the following line in Apache Nginx:

Listen 127.0.0.1:80

And change Apache Nginx to:

Listen 127.0.01:8080

Save Apache Nginx and exit.

The next step recommended by Dataservermarket is editing the default virtual host file in Apache Nginx. The Apache with Nginx virtual host file is set to serve only on port 80.

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

Look for the following line in Apache with Nginx:

<VirtualHost 127.0.0.1:80>

Change Apache with Nginx into:

<VirtualHost 127.0.0.1:8080>

Save and reload Apache with Nginx file:

sudo service apache2 reload

Verify that Apache with Nginx is now listening on 8080:

sudo netstat -tlpn

Apache with Nginx is configured, and both are performing as front-end and back-end servers. Apache with Nginx servers as a proxy server still delivers static content. Dataservermarket provided a brief guide on configuring Apache with Nginx. We hope you understood the above steps of configuring Apache with Nginx.

Wrapping up:

The essential thing we learn from the simple configuration is that Nginx and Apache can be together. Nginx and Apache are the best combinations of servers. However, a minor problem arises when Nginx and Apache listen to the same ports. By giving Nginx and Apache different ports, the system efficiency is assured. We hope that you understood the configuration of Nginx and Apache together on the Ubuntu VPS or dedicated server.

Leave a Reply

Your email address will not be published. Required fields are marked *