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

Nginx and Apache are innovative open source web servers used with PHP. Currently, Apache is the number one server of most websites since it’s release a decade ago while Nginx comes in the second place as the most preferred server for active sites. Each server holds its share of popularity for obvious reasons: Apache’s power and Nginx’s speed are widely known. However, both servers have some shortcomings. Apache is considered hard on server’s memory while Nginx (which is great for static files) requires the help of PHP-FPM or such like modules for dynamic content.

 

In this article, we will help you combine the two servers to leverage their strengths and eliminate the shortcomings. We’ll use Nginx as the static processor and Apache as the processor for dynamic, backend content.

Prerequisites

To get started with the steps in this article, you need to have the following:

  • A dedicated server running Ubuntu 16.04
  • A standard user account with sudo privileges on your server
  • A static IP address for this server

Getting started

The first thing when Installing Nginx is ensuring your Server is up to date. T update your server run the commands below:

$ sudo apt-get update
$ sudo apt-get upgrade

Step 1: Installing Nginx

Start by installing nginx. To do so, run the command below:

$ sudo apt-get install nginx

Once it’s installed, proceed to set up and configure Nginx as a reverse proxy so that it can run on the front end.

Step 2: Configuring Nginx

Once Nginx is installed you can initiate the configuration process by creating a new virtual host file using the command:

$ sudo nano /etc/nginx/sites-available/webproxy

Add the configuration below to your file:

server {
        listen   80; 

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

        server_name example.com; 

        location / {
        try_files $uri $uri/ /index.php;
        }

        location ~ \.php$ {
        
        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;

         }

         location ~ /\.ht {
                deny all;
        }
}

In this configuration, these changes were applied:

  • The root was adjusted to specify the root directory.
  • The try_files attempt to attend to any page requested by a visitor. If Nginx cannot fulfill this request, it’s passed to your proxy.
  • The proxy_pass allows nginx to define the specific address of your proxied server.
  • The location block limits access to the .htaccess file.

Run the command below to verify the Nginx configuration syntax:

$ sudo nginx -t

If everything is okay 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

Next, run the command below to activate your virtual host:

$ sudo ln -s /etc/nginx/sites-available/proxyhost /etc/nginx/sites-enabled/webproxy

Then erase the default Nginx service server block, by running the command:

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

Start your Nginx service using the command below:

$ sudo /etc/init.d/nginx start

If you want to configure Nginx to start at boot time, run the command below:

$ sudo update-rc.d nginx defaults

Step 3: Installing Apache

With Nginx activated, you can now install Apache to handle proxied requests.

To install Apache run the command below:

$ sudo apt-get install apache2

Until now, Nginx is still turned off, so Apache will be operating on port 80.

Step 4: Configuring Apache

We directed Nginx to direct the proxy to port 8080 which is where Apache listens to receive requests. Now we should direct Apache to utilize port 8080 and leave port 80 for Nginx. Now go ahead and open the Apache ports to set them on the correct port on Apache.

Run the command below to open your Apache Ports file:

$ sudo nano /etc/apache2/ports.conf

Search and edit the lines below to ensure Apache is running on the preferred port 8080 from the localhost:

NameVirtualHost 127.0.0.1:8080
Listen 127.0.0.1:8080

Save the file and exit. Now restart your Apache service:

$ sudo /etc/init.d/apache2 restart

You can configure Apache to start at boot time using the command below:

$ sudo update-rc.d apache2 defaults

Next, edit the default virtualhost file:

$ Sudo /etc/apache2/sites-enabled/000-default.conf

Ensure the configuration is like the one below:

<VirtualHost 127.0.0.1:8080>
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Save the content and exit, then restart both Nginx and Apache services using the command below:

$ sudo /etc/init.d/apache2 restart
$ sudo /etc/init.d/nginx restart

Step 5: Installing PHP5

Before testing anything, make sure you install PHP with the command below:

$ sudo apt-get install php5

Save and exit then restart both the Nginx and Apache services to apply these changes:

$ sudo service apache2 restart
$ sudo service nginx restart

Step 6: Testing the Nginx Reverse Proxy

On configuring everything properly, you can test the functionality of Nginx reverse proxy. To do this, run the curl command as shown below:

$ curl -I localhost

You should see the text below:

HTTP/1.1 200 OK
Server: nginx/1.6.2 (ubuntu)
Date: Wed, 28 July 2016 08:34:02 IST
Content-Type: text/html
Content-Length: 11321
Last-Modified: Tue, 31 June 2016 10:25:08 IST
Connection: keep-alive
Vary: Accept-Encoding
ETag: "564f1a7a-2c39"
Expires: Wed, 28 July 2016 08:34:31 IST
Cache-Control: no-cache
Accept-Ranges: bytes

Winding Up

Now everything is set up, but we are going to test whether the system information is being directed to apache by running a quick PHP script.

Create your php.info file using the command below:

$ sudo nano /var/www/info.php

Include these lines into your file:

<?
phpinfo( );
?>

Now save your file and exit. Next, open your browser and type the address below ( replace the “Domain or IP address” with your correct domain name.

http://DOMAIN_OR_IP_ADDRESS/info.php

This page will pop up:

Conclusion

By now you should have NGINX as a reverse proxy for Apache on Ubuntu 16.04.  Successful configuration of nginx and Apache can be a great boost to your server.

If you are looking to improve your servers' performance, we recommend that you also look into switching to a new, more advanced hosting service. On HostAdvice you will find the best web hosting services, alongside user reviews and plan prices.

 

Check out these top 3 Best web hosting services

Was this article helpful?