Caching

From wiki.kungfootek.net
Jump to: navigation, search

APC Install

OPcache improves PHP performance by storing precompiled script bytecode in shared memory, thereby removing the need for PHP to load and parse scripts on each request.

This extension is bundled with PHP 5.5.0 and later, and is » available in PECL for PHP versions 5.2, 5.3 and 5.4.


Again, I use a method that's generic across multiple platforms for consistency. Here are instructions for installing php opcache via Pecl.

Make sure APC has not been installed via the yum package management system. If it has, remove it before proceeding.

yum -y install php-pear php-devel httpd-devel pcre-devel gcc make && pecl install apc


Copy the options below into a file apache can reach. Usually ‘ /etc/php.d/apc.ini ‘.


restart or reload the webserver

/etc/init.d/httpd graceful

or

/etc/init.d/httpd restart


Verifying installation

Copy the apc.php file from ‘/usr/share/pear/apc.php’ to an executable location on your webserver and load in your web browser. You should see a pie-graph on the right, and general cache information on the left. Gotchas:

  • Giving way too much Ram to the cache will slow the site down.
  • Not giving enough Ram may cause strange out of memory errors that aren’t fixed by changing php.ini.
  • If you set a password, make it a good one.


!! Warning !!

Centos has a package for APC. If you install this over the package it will not alert you that APC is already installed and your webserver will -Cease to function- To fix this:

  1. Prevent the extension from loading. -> Remove the .so from php.ini or move the apc.ini from /etc/php.d.
  2. Reload Apache so your pages start getting served again.
  3. Remove the pecl version -> 'pecl uninstall apc'
  4. Perform 'yum searc APC' to find the exact package name, execute -> 'yum erase <package_name>'
  5. Return to the above steps to install APC.


APC Reference: http://www.if-not-true-then-false.com/2012/php-apc-configuration-and-usage-tips-and-tricks/


apc.ini example file. ‘ /etc/php.d/apc.ini ‘ ( I use these myself )


; Enable apc extension module
extension = apc.so

; Options for the APC module version >= 3.1.3
; See http://www.php.net/manual/en/apc.configuration.php

apc.enabled=1
apc.shm_segments=1
apc.shm_size=128M
apc.num_files_hint=2048
apc.user_entries_hint=4096
apc.ttl=86400
apc.use_request_time=1
apc.user_ttl=7200
apc.gc_ttl=3600
apc.cache_by_default=1
apc.filters
apc.mmap_file_mask=/tmp/apc.XXXXXX
apc.file_update_protection=2
apc.enable_cli=0
apc.max_file_size=1M
apc.stat=1
apc.stat_ctime=0
apc.canonicalize=0
apc.write_lock=1
apc.report_autofilter=0
apc.rfc1867=1
apc.rfc1867_prefix =upload_
apc.rfc1867_name=APC_UPLOAD_PROGRESS
apc.rfc1867_freq=0
apc.rfc1867_ttl=3600
apc.include_once_override=0
apc.lazy_classes=0
apc.lazy_functions=0
apc.coredump_unmap=0
apc.file_md5=0
; not documented
apc.preload_path



Generic Configs

Varnish

Centos 7 Install


yum install epel-release
yum install varnish


Check your config file syntax.


Checking the file syntax Shamelessly copied from David Goodwin. Centos boxes also check when you issue a service reload. This command is good for checking syntax of a file you may be developing outside of the varnish config folder.

varnishd -C -f /etc/varnish/default.vcl


Filtering content by user agent


This section goes into sub vcl_recv

   if (req.http.user-agent ~ "Lynx") {
      error 200 "Health Check";
   }


This section goes into sub vcl_error

        if (obj.status == 200) {
        set obj.http.Content-Type = "text/html; charset=utf-8";
        synthetic {"<html><head><title>Varnish Health Status</title></head><body>Every time you check this page, God postpones the Zombie Apocalypse.<br><BR>( <i>Still waiting for my Nuclear Winter though</i>. )</body></html>"};
        return(deliver);
        }


Rejecting access by url


Wouldn't it be nice to restrict access to Drupal or Wordpress admin panels to a specific IP or range of Ip addresses in varnish?


In it's own section, create a new acl rule:

 acl acl_allowed {       # <-- Name is arbitrary. Avoid reserved words.
	"1.2.3.0"/24;
	"1.2.0.0"/16;
	"1.0.0.0"/8;
	"1.2.3.4";       # <-- Same as /32
 }

The next section goes into vcl_recv:


To restrict access to the Drupal admin panel in Varnish:

        if (    req.url ~ "^/user*" ||
                req.url ~ "^/admin*") {
                if (client.ip ~ acl_allowed) {
                        return(pass);
                } else { error 404; }
        }


To restrict access to the wordpress admin panel in Varnish:

        if (    req.url ~ "^/wp-admin*") {
                if (client.ip ~ acl_allowed) {
                        return(pass);
                } else { error 404; }
        }


Forward domain.tld to www.domain.tld


In vcl_recv:

        if (req.http.host ~ "domain.tld" && req.http.host != "www.domain.tld") {  
                error 302;
        }


in vcl_error:

        if (obj.status == 302 && req.http.host == "domain.tld") {
                set obj.http.Location = "http://www.domain.tld/" req.url;

        }


Purging Singular pages from Varnish

Taken from: http://stackoverflow.com/questions/9215617/how-to-send-a-purge-request-in-varnish



sub vcl_recv {
    if (req.request == "PURGE") {
            if (!client.ip ~ acl_allowed) {
                    error 405 "Not allowed.";
            }
            return (lookup);
    }
}


sub vcl_hit {
    if (req.request == "PURGE") {
            purge;
    }
 }


sub vcl_miss {
        if (req.request == "PURGE") {
                 purge;
        }
}

#!/usr/bin/php
<?php


$curl = curl_init("http://sandbox.domain.tld/test");
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PURGE");
$output = curl_exec($curl);
?>

Solr