Setup Nginx as web server and as reverse proxy for Apache with Virtualmin support

We know that Nginx is more faster than Apache and most of us prefer to replace Apache with Nginx as their web server. Nginx is known to serve faster static content and run with less RAM. As of this writing, Virtualmin supports Apache as its web server. To take advantage of Nginx, we will install it as reverse proxy for Apache and continue using Virtualmin to manage your domains. This guide also applies to Nginx+PHP FPM setup just skip the “Configure Apache” section and skip the “Configure Virtualmin” section if you are not using Virtualmin. Nginx configurations for virtual host are tailored for Drupal site and the following are the features:

The following procedures are tested on Linode server running Centos 7 64-bit Linux distribution.

Install Nginx

If you need to install Nginx with PageSpeed module please follow the steps here instead then jump to configure Nginx section.

  1. In able to install the latest Nginx server we will need to register Nginx repository:
    Have the following codes as its content:
    Note: if just in case the nginx does not install try to hard code the $releasever with value of 7
  2. Install Nginx using yum:
  3. Make Nginx auto-start upon reboot:

Configure Nginx

  1. We will not need the native Nginx configurations provided because we will create new configurations. Lets backup the original Nginx configurations first:
  2. Create the folders following the directory structure shown below:Nginx folder structure

    In the next steps, we will populate these folders with Nginx configurations. We will start populating each folder from bottom folder (utils) to top folder (apps).

  3. Create the main Nginx file /etc/nginx/nginx.conf and copy the following scripts to this file:

    Create the file /etc/nginx/drupal/php_handler.conf and add the following to it:

    We have two options here: microcache_auth.conf which uses the Nginx cache for anonymous users only and microcache.conf which uses the Nginx cache for both anonymous and authenticated users. The microcache_auth.conf is enabled by default. Select between the two according to your requirements.

    Create the file /etc/nginx/drupal/named_location.conf and add the following to it:

    Create the file /etc/nginx/drupal/drupal_upload_progress.conf and add the following to it:

    Create the file /etc/nginx/drupal/drupal_install.conf and add the following to it:

    Create the file /etc/nginx/drupal/drupal_cron_update.conf and add the following to it:

    Create the file /etc/nginx/drupal/core.conf and add the following to it:

    Create the file /etc/nginx/drupal/common_server_context.conf and add the following to it:

    Create the file /etc/nginx/drupal/boost.conf and add the following to it:

    Populate the /etc/nginx/apps/pagespeed folder under this guide.

    To enable Google PageSpeed in your Nginx configuration (make sure first that your Nginx is compiled with PageSpeed module), uncomment the line seen below at /etc/nginx/nginx.conf:

    … also uncomment the line seen below at /etc/nginx/apps/drupal/common_server_context.conf:
    … and uncomment the line seen below at /etc/nginx/site-available/template.conf:
  4. To enable this Nginx reverse proxy for Apache setup, execute the following:
    On the other hand, if PHP FPM backend is your setup, execute the following:
  5. Set permission:
  6. Restart Nginx:

Setup Nginx requirements

  1. In this tutorial the Apache will use port 8080 and lets open this port to become accessible:
  2. Create the Nginx cache path folder:
  3. Create Nginx logrotate script:
    Content:

Configure Apache

  1. Since Nginx is reverse proxy to Apache, the IP address that Apache will get is the IP of the server and we need to correct that. Apache 2.4 and above do have mod_remoteip and we will use that module. Open mod_remoteip’s configuration file:
    Add the following codes:
    Note: change 188.8.8.8 to your server’s IP address.
  2. Change the port of Apache:
    Look for:
    … and change to:
  3. Restart Apache:

Configure Virtualmin

  1. Set the virtual server template to listen to 8080. Login to Virtualmin, go to “System Settings” -> “Server Templates” -> “Default Settings” and select from the dropdown “Apache Website”. Change the “Port number for virtual hosts” from 80 to 8080. Restart webmin:
  2. Lets build the script that will automate the creation of website virtual host Nginx configuration file each time Virtualmin created a new server.Create the file /usr/local/bin/virtualmin.sh and add the following to it:
    Make the script executable:
  3. Let Virtualmin know about the virtualmin.sh. Login to Virtualmin, go to “System Settings” -> “Virtualmin Configuration” and select from dropdown “Actions upon server and user creation”. Populate the “Command to run after making changes to a server” field with:
Fonte: https://www.webfoobar.com/node/35
Facebook Comments
Rate this post