banner

Blogs

How to configure Nginx as a Reverse Proxy for Apache on Ubuntu 16.04 ?

  • September 23rd, 2016
  • By Amal S
  • Blog
  • apache_nginx_tw-min

    Apache and Nginx are the two open source web servers which account over 50% of the total traffic on the internet. Those solutions handle a diverse workload and are compatible with other software, which is associated with the server, in order to produce complete web stack.

    Both Apache and Nginx excel in its own way and have their own qualities but the latter is considered to be the fastest web server in the world. Big technology companies such as WordPress, Comodo, Netflix, Github, Cloudflare had already switched to Nginx due to its high performance and resource requirements. NGINX set of features and some server roles are:

    • Reverse proxy server for the HTTP, HTTPS, IMAP, POP3 and SMTP protocol.
    • Front-end proxy for Apache and other web servers, combining the flexibility of Apache along with the support of good static content performance of NGINX.
    • Load balancer and an HTTP cache.
    • As Nginx only passes true HTTP requests, it protects your server from common attacks such as DDOS (Distributed Denial of Service).
    • Compatible with GZIP compression
    • High output

    This blog guides on how to set up Nginx which is known for stability, simple configuration, rich attribute set and effective resource utilization as a reverse proxy for Apache on Ubuntu 16.04.

    Required Pre-requisites

    In order to configure Nginx as a reverse proxy over Apache on Ubuntu, there are Pre-requisites which is needed to be made available

    • Install a new Ubuntu version
    • A standard user account with sudo privileges is required for the configuration.
    • Install Nginx HTTP services on the server.
    • Skill for working on a Linux platform.

    Apache and PHP-FPM Installation

    Install the PHP FastCGI Apache module named as libapache2-mod-fastcgi. Check whether the repository is updated and thus make sure you have the latest version.

    sudo apt-get update

    Install the necessary packages.

    sudo apt-get install apache2 libapache2-mod-fastcgi php-fpm

    Apache and PHP-FPM Configuration

    In order to configure Apache and PHP-FPM, the Apache port number will be changed to 8080 and configure it to work with PHP-FPM using mod_fastcgi module. For this edit the Apache configuration file and change the Apache number.

    sudo nano /etc/apache2/ports.conf

    Find the following line:

    Listen 80

    Change it to:

    Listen 8080

    Save and exit ports.conf.

    Edit the default virtual host file of Apache. Only on the port 80, the <VirtualHost> directive in this file is set to serve sites, so we have to change that. Open the default virtual host file.

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

    The first line should be:

    <VirtualHost *:80>

    Change it to:

    <VirtualHost *:8080 >

    Save the file and reload Apache.

    sudo systemctl reload apache2

    Verify that Apache is now listening on 8080.

    sudo netstat -tlpn

    Apache to Use mod_fastcgi Configuration

    Using mod_php, Apache serves PHP pages but it requires additional configuration to work with PHP-FPM.

    sudo a2dismod php7.0

    The next step is to add a configuration block for mod_fastcgi which depends on mod_action. mod_action is disabled by default, so we first need to enable it.

    sudo a2enmod actions

    These configuration directives pass requests for .php files to the PHP-FPM UNIX socket.

    sudo nano /etc/apache2/mods-enabled/fastcgi.conf

    Following lines needed to be added within the <IfModule mod_fastcgi.c> . . . </IfModule> block, below the existing items in that block:

    AddType application/x-httpd-fastphp .php

    Action application/x-httpd-fastphp /php-fcgi

    Alias /php-fcgi /usr/lib/cgi-bin/php-fcgi

    FastCgiExternalServer /usr/lib/cgi-bin/php-fcgi -socket /run/php/php7.0-fpm.sock -pass-header Authorization

    <Directory /usr/lib/cgi-bin>

    Require all granted

    </Directory>

    Save the changes you made to fastcgi.conf and does a configuration test.

    sudo apachectl –t

    PHP Functionality Verification

    Check whether the PHP works by creating a phpinfo () file and accessing it from your web browser.

    echo “<?php phpinfo(); ?>” | sudo tee /var/www/html/info.php

    Virtual Hosts Creation for Apache

    First, create the root directories:

    sudo mkdir -v /var/www/{foobar.net,test.io}

    Then create an index file for each site.

    echo “<h1 style=’color: green;’>Foo Bar</h1>” | sudo tee /var/www/foobar.net/index.html

    echo “<h1 style=’color: red;’>Test IO</h1>” | sudo tee /var/www/test.io/index.html

    Then create a phpinfo() file for each site so we can test PHP is configured properly.

    echo “<?php phpinfo(); ?>” | sudo tee /var/www/foobar.net/info.php

    echo “<?php phpinfo(); ?>” | sudo tee /var/www/test.io/info.php

    Now create the virtual host file for the foobar.net domain.

    sudo nano /etc/apache2/sites-available/foobar.net.conf

    Place the following directive in this new file:

    <VirtualHost *:8080>

    ServerName foobar.net

    ServerAlias www.foobar.net

    DocumentRoot /var/www/foobar.net

    <Directory /var/www/foobar.net>

    AllowOverride All

    </Directory>

    </VirtualHost>

    Now that both Apache virtual hosts are set up, enable the sites using the a2ensite command. Through this, a symbolic link is created to the virtual host file in the sites-enabled directory.

    sudo a2ensite foobar.net

    sudo a2ensite test.io

    Check Apache for configuration errors again.

    sudo apachectl -t

    Reload Apache if Syntax OK is displayed.

    sudo system reload apache2

    Nginx – Installation and Configuration

    Here, we will install Nginx and configure the domains sample.org and example.com as Nginx’s virtual hosts. Install Nginx using,

    sudo apt-get install Nginx

    Remove the default virtual host’s symlink

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

    The same procedures which we have used for Apache will be used for creating virtual hosts for Nginx. Develop the root directories for both the websites.

    sudo mkdir -v /usr/share/nginx/{example.com,sample.org}

    we’ll again create index and phpinfo() files for testing after setup is complete.

    echo ”

    Example.com

    “|sudo tee /usr/share/nginx/example.com/index.html

    echo ”

    Sample.org

    ” | sudo tee /usr/share/nginx/sample.org/index.html

    echo “” | sudo tee /usr/share/nginx/example.com/info.php

    echo “” | sudo tee /usr/share/nginx/sample.org/info.php

    The next step is to create a virtual host file for the domain example.com.

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

    Paste the following into the file for example.com:

    server {
    listen 80 default_server;

    root /usr/share/nginx/example.com;
    index index.php index.html index.htm;

    server_name example.com www.example.com;
    location / {
    try_files $uri $uri/ /index.php;
    }

    location ~ \.php$ {
    fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    include snippets/fastcgi-php.conf;
    }
    }

    Save and close the file. Now create a virtual host file for Nginx’s second domain, sample.org.

    sudo nano /etc/nginx/sites-available/sample.org

    The server block for sample.org should look like this:

    server {
    root /usr/share/nginx/sample.org;
    index index.php index.html index.htm;

    server_name sample.org www.sample.org;
    location / {
    try_files $uri $uri/ /index.php;
    }

    location ~ \.php$ {
    fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    include snippets/fastcgi-php.conf;
    }
    }

    Save and close the file. Then enable both the sites by creating symbolic links to the sites-enableddirectory.

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

    sudo ln -s /etc/nginx/sites-available/sample.org /etc/nginx/sites-enabled/sample.org

    Do an Nginx configuration test:

    sudo nginx -t

    Then reload Nginx if OK is displayed.

    sudo systemctl reload nginx

    Configuration of Nginx for Apache’s Virtual Hosts

    Create and an additional Nginx virtual host with multiple domain names in the server_name directives.

    Let’s create an additional Nginx virtual host with multiple domain names in the server_name directives. To apache, the requests for such domain names will be proxied.

    Create a new Nginx virtual host file:

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

    Add the code block below. It includes the names of both Apache virtual host domains and proxies to their requests to Apache.

    server {
    listen 80;
    server_name foobar.net www.foobar.net test.io www.test.io;

    location / {
    proxy_pass http://your_server_ip:8080;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    }
    }
    Enable this new virtual host by creating a symbolic link and after that save the file.

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

    Do a configuration test:

    sudo nginx -t

    Reload Nginx if OK is displayed.

    sudo systemctl reload nginx

    Conclusion

    Nginx is considered to be the most popular HTTP server and reverse proxy. It is used for the common protocols like SMTP, HTTPS, POP3, HTTP, and IMMP and used as the load balancer for HTTP cache. This method of a reverse proxy is a set up which utilizes Nginx as both the frontend and backend and handles the request coming from the browser by passing it to the backend of Apache. It is more suitable for static websites rather than dynamic websites. Nginx has rich features such as high performance and low memory/RAM usage. Even though the Nginx is a reverse proxy for Apache, Nginx’s proxy service is transparent and connections to Apache’s domains appear to be served directly from Apache itself.

    Recent Post

    Looking for a reliable 24/7 support provider?

    Contact US Today

    Contact Us