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.


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.


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 and instruct Nginx to reverse –proxy traffic to Apache while still serving static content;

server {
 server_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


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;


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;


Moreover, change it to;


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*            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.


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.

  • If you’re ready to get started with a virtual private server for hosting your websites, 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


Was this article helpful?