Apache and Nginx together on Ubuntu

Is it possible to run  Nginx and Apache at the same time on the same machine?

The answer is YES.

This post provides instructions on how to configure Apache and Nginx  to work together on the same machine running Ubuntu.

Overview

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.

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

Apache and Nginx can definitely run simultaneously. The default config will not allow them to start at the same time because they will both try to listen on the same port and the same IP.However, you can easily either change the ports or ports and IPs, or IPs. There are various ways to make them run either one behind the other(usually Apache behind Nginx because Nginx will be the first entry point in the chain since it’s faster for static resources, and Apache will then only be triggered for some advanced dynamic rendering/processing) or just side by side.

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.

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

Those text in blue is where you should edit according to your server info.

 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

and  change it to

Listen 127.0.0.1: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>

then, 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.

References