Blog Archive

Tuesday, 25 November 2014

How to configure Drupal7, varnish, authcache2 and ESI in Nginx server?

This is step by step procedure for, how to configure the authcache, varnih and ESI for the drupal7 project in NGINX erver.

To configuring the Nginx Server use the procedure from document "http://www.geoffstratton.com/2014/03/nginx-php-fpm-apc-ssl-drupal/"

Well, make sure your nginx server is up and running.

1.Configuring the Authcache module.
 
   Download the module from https://www.drupal.org/project/authcache
   Enable only the authcache module
   Go to admin/reports/status, then you can see the Authcache area is red and saying that there is no back end has been configured.
 
2.Configure Varnish on the back-end[nginx server].

    Using the Following commands,
 
    $ curl http://repo.varnish-cache.org/debian/GPG-key.txt | sudo apt-key add -
    $ echo "deb http://repo.varnish-cache.org/ubuntu/ precise varnish-3.0" | sudo tee -a /etc/apt/sources.list
    $ sudo apt-get update
    $ sudo apt-get install varnish
 
    Configure Varnish:
 
    $ sudo gedit /etc/default/varnish
 
    Make sure “start=yes” is specified, and change DAEMON_OPTS to make Varnish listen on port 80:
 
    It should like,
        DAEMON_OPTS="-a :80 \
             -T localhost:6082 \
             -f /etc/varnish/default.vcl \
             -S /etc/varnish/secret \
             -s malloc,256m"
           
     Then edit the /etc/varnish/default.vcl.
   
     $ sudo gedit /etc/varnish/default.vcl
   
     Select everything on the file and delete. Copy everything form the Mydefault.vcl file[click to download], then paste into the /etc/varnish/default.vcl and save.
     After saving you can check the syntax using following command.
   
     $ sudo varnishd -c -f /etc/varnish/default.vcl
   
     Making Nginx to listen on 8080 instead of 80, For that you need to edit '/etc/nginx/sites-available/default' file using following command.
   
     $ sudo gedit /etc/nginx/sites-available/default
   
     While editing, you need to add the line "listen 127.0.0.1:8080 default_server;" and comment the lines "listen 80 default_server;" and "listen [::]:80 default_server ipv6only=on;"
   
     It should be look like,
   
       server {
        listen 127.0.0.1:8080 default_server; # <==================== you need to add this line  and it will make the nginx to listen for traffic     
#listen 80 default_server;
        #listen [::]:80 default_server ipv6only=on;
        [.............]
      }
   
      You can check the nginx syntax using following command.
   
      $ sudo nginx -t
   
      Well everything is fine then we need to stop the nginx and start the varnish, then after we have the start the nginx using following commands
   
      $ service nginx stop
      $ service varnish restart 
      $ service nginx start

3. Enabling Authcache_varnish module
 
    From the Step 1 we find  out there is no backend has been configured for the Authcache in path "admin/reports/status".
    So now we are going to set up the Authcache_varnish as the backend for the Authcache.
    For that got to "admin/modules" and enable authcache_varnish module.
    Also reffer the README.txt uner "/sites/all/modules/authcache/modules/authcache_varnish".
      In the README.txt file explaining about the configuration about the .vcl using the example.vcl, but here we already taken care about that
      in our step 2 by editing the data in the file "/etc/nginx/sites-available/default"
   
    Go to "admin/config/development/performance" and set "Expiration of cached pages" to an appropriate value (> 0). Eg. 6 hours.
    Next we need to go advance to the step 4 and 5, install, enable and configure the Varnish and Cache Expiration modules.
 
    After doing the step 4 and 5 we need to set up the reverse proxy server.
       Authcache Varnish verifies whether a request is coming in through a trusted proxy server.
       Therefore it is necessary to properly configure reverse proxy support in settings.php. E.g:
         $conf['reverse_proxy'] = TRUE;
         $conf['reverse_proxy_addresses'] = array('a.b.c.d');// Replace 'a.b.c.d' with the IP address of the revers proxy server.
       For testing purposes it is possible to disable the reverse proxy check by placing the following line into the settings.php file:
         $conf['authcache_varnish_validate_reverse_proxy_address'] = FALSE;
      Beside the reverse proxy address Authcache Varnish also tests whether the X-Varnish header is on the request.
      In order to disable this check, place thefollowing line into settings.php:
         $conf['authcache_varnish_header'] = FALSE;
    So here we are adding the following lines to  our 'settings.php' file under directory "sites/default/".
       $conf['authcache_varnish_validate_reverse_proxy_address'] = FALSE;
       $conf['authcache_varnish_header'] = FALSE;
 
    Finally clear cache under "admin/config/development/performance" and go to 'admin/reports/status'. You can see the Authcache area is in green color
    with report message as "hook_authcache_backend_cache_save() implemented: authcache_varnish";.
    And go to 'admin/config/system/authcache' and do the necessary configuration according to your requirement.
 
   
4. Enabling and configuring the Varnish module
   
     Download the module from https://www.drupal.org/project/varnish and extarct to the 'sites/all/modules/'.
     Reffere the README.txt file under the varnish module directory.
     Enable the module fromt the path admin/modules
     Then edit your "settings.php" file in your "sites/default/" and add the following lines.
   
       // Add Varnish as the page cache handler.
       $conf['cache_backends'] = array('sites/all/modules/varnish/varnish.cache.inc');
       $conf['cache_class_cache_page'] = 'VarnishCache';
     
     Go to admin/config/development/varnish and configure your connection
       1. Configure "Varnish Control Key",for finding the "Varnish Control Key" you need use the following commands
     
          $ sudo gedit /etc/varnish/
       
          Copy and paste in the field "Varnish Control Key"
       2. Configure "Varnish Control Terminal", For that you need to add the IP address "127.0.0.1:6082" to the field Varnish Control Terminal
       and Save Configuration.
       Finally, after saving the configuration you can see the 'Status' with green tick mark and "Varnish running" message.
     
5. Enabling the Cache Expiration module.
 
     Download the module from 'https://www.drupal.org/project/expire' and extarct to the 'sites/all/modules/'.
     And enable the module fromt the path 'admin/modules'.
 
6. Enabling necessary modules.

   Go to 'admin/modules' enable and install following modules.
      Authcache Ajax
      Authcache User
      Authcache Block
      Authcache ESI
      Authcache ESI Debug
      Authcache Field
      Authcache Form
      Authcache Menu
      Authcache Node History
      Authcache Page Manager
      Authcache Views

7. Enabling Cache Object API module.
 
     Download the module from 'https://www.drupal.org/project/cacheobject' and extarct to the 'sites/all/modules/'.
     And enable the module from the path 'admin/modules'.
   
     Add the following lines to the "settings.php" file in your "sites/default/" directory.
   
       # Include the Cache Object API Wrapper as a cache backend
       $conf['cache_backends'][] = 'sites/all/modules/cacheobject/cacheobject.inc';
       # Set the cache class for the form cache to
       $conf['cache_class_cache_form'] = 'CacheObjectAPIWrapper';
       # Optionally set the underlying cache class
       $conf['cacheobject_class_cache_form'] = 'DrupalDatabaseCache';
     
8. Enable Authcache Debug module.

    Go to 'admin/modules' enable  Enable Authcache modules.
    Then navigate to "admin/config/system/authcache/debug", check the  "Enable debug mode for all roles" and save the configuration.
 
Finally you can navigate to any page[not with admin user] and find the Authcache Debug. You can check Cache Status: "MISS" or the indicator as yellow.
If it is yellow please refresh the page , then you can find the Cache Status changes to  "HIT".
    

2 comments:

  1. what a positive effort to help developers in steps by step process drupal development. happy to read about how to configure Drupal7, varnish, authcache2 and ESI in Nginx server

    ReplyDelete
    Replies
    1. Thanks Sarah, hope my article helped you. What motivates me to write down this, when i was in position to do this configuration, i didnt find any right doc. So i decide to write little.

      Delete