How to Configure nginx and apache on same server | HostAdvice

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

Nginx and Apache are great and powerful web servers. However, they both have drawbacks; Apache uses up server memory while Nginx (best used for static files) require the help of php-fpm to process dynamic content.

This article takes you on how to configure Nginx and Apache to work together.

Unsure about navigating the intricacies of Nginx and Apache? Dive into our recommended Managed VPS Hosting Providers and let experts streamline the process for you.

Overview

Nginx is an excellent lightweight web server designed to serve high traffic while Apache is another popular web server serving more than half of all active websites in the world. One can combine the two web servers to significant effect, with Nginx serving as static web server front and while Apache is processing the back end. So let’s look into how to configure your Nginx to work with Apache side by side.

Configuring Apache and Nginx

Set different ports for each server. That means you can leave port 80 for  Nginx and assign Apache a different port.

Nginx

Install Nginx

Install and configure Nginx that will serve as the front end of your site.

Download from apt-get;

sudo apt-get install nginx

Once it has downloaded, configure the virtual host to run on the front end. However, a few changes are required on the configuration file.

Configure Nginx

Open up the nginx configuration file

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

For example, you could tell apache to listen on 127.0.0.1:8080 and instruct Nginx to reverse –proxy traffic to Apache while still serving static content;

server {
 listen   127.0.0.1:80;
 server_name  some.name another.dname;
 access_log  /var/log/nginx/something-access.log;
 location / {
  proxy_pass http://localhost:8080;
  proxy_redirect off;
  proxy_set_header Host $host;
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
 }
 location ~* ^.+\.(jpg|js|jpeg|png)$ {
  root /some/where/on/your/disks;
 }
; put your static hosting config here.
}

Activate the virtual host.

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

Delete the default Nginx server block.

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

Apache

Install Apache

Install the backend which is Apache

sudo apt-get install apache2

Apache starts running on port 80 as Nginx is not started. Let’s make Apache listen on a different port so that they can work together.

Configuring Apache

Open Apache ports.conf file using the below command;

sudo nano /etc/apache2/ports.conf

Look for the following line;

Listen 127.0.0.1:80

Moreover, change it to;

Listen 127.0.01:8080

Save and Exit.

Next, edit the default virtual host file in Apache. The <VirtualHost> in this file is set to serve sites only on port 80

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

Look for the following line;

<VirtualHost 127.0.0.1:80>

Moreover, change it to;

<VirtualHost 127.0.0.1:8080>

Save the file and reload Apache.

sudo service apache2 reload

Verify that Apache is now listening on 8080.

sudo netstat -tlpn

The output is shown below, with apache2 listening on :::8080.

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address     Foreign Address      State    PID/Program name
tcp        0      0 0.0.0.0:22        0.0.0.0:*            LISTEN   1086/sshd
tcp6       0      0 :::8080           :::*                 LISTEN   4678/apache2

Nginx is configured and running as the front web server on port while Apache is configured to run on the backend on port 8080. Nginx redirects proxy traffic as it still serves static content.

Conclusion

The most important thing we take from this simple configuration is that Apache and Nginx can and do work together. A problem may arise when they both listen to the same ports. By giving them different ports to listen to, your system functionality is assured.

Ready to get started with a Virtual Private Server? Visit our VPS Hosting page to find the Best VPS Hosting Providers that’s right for you


Check out the top 3 Linux hosting services

Webdock
AU$1.57 /mo
Starting price
Visit Webdock
Rating based on expert review
  • User Friendly
    3.8
  • Support
    4.5
  • Features
    4.5
  • Reliability
    4.3
  • Pricing
    4.3
Kamatera
AU$5.94 /mo
Starting price
Visit Kamatera
Rating based on expert review
  • User Friendly
    3.5
  • Support
    3.0
  • Features
    3.9
  • Reliability
    4.0
  • Pricing
    4.3
Ultahost
AU$3.71 /mo
Starting price
Visit Ultahost
Rating based on expert review
  • User Friendly
    4.3
  • Support
    4.8
  • Features
    4.5
  • Reliability
    4.0
  • Pricing
    4.8

How to Configure Nginx as Reverse Proxy for Apache on an Ubuntu 16.04 VPS or Dedicated Server

This guide is compiled to help you learn how to install Nginx as a reverse proxy
less than a minute
Eliran Ouzan
Eliran Ouzan
Web Designer & Hosting Expert

How to Host Multiple Websites on an Ubuntu 18.04 VPS or Dedicated Server

This article will show you how to host two or more websites on a single Virtual
less than a minute
Idan Cohen
Idan Cohen
Marketing Expert

How to Install Apache Cassandra on an Ubuntu 18.04 VPS or Dedicated Server

This tutorial will help you install and configure Apache Cassandra (an opensourc
less than a minute
Max Ostryzhko
Max Ostryzhko
Senior Web Developer, HostAdvice CTO

How To Install Apache, MySQL & PHP on an Ubuntu 18.04 VPS or Dedicated Server

This article shows users how to setup PHP, Apache web server and MySQL database
less than a minute
Idan Cohen
Idan Cohen
Marketing Expert
HostAdvice.com provides professional web hosting reviews fully independent of any other entity. Our reviews are unbiased, honest, and apply the same evaluation standards to all those reviewed. While monetary compensation is received from a few of the companies listed on this site, compensation of services and products have no influence on the direction or conclusions of our reviews. Nor does the compensation influence our rankings for certain host companies. This compensation covers account purchasing costs, testing costs and royalties paid to reviewers.
Click to go to the top of the page
Go To Top