CDNJS

Forget about Google’s hosted libraries and say hello to CDNJS. I can’t believe I’ve only now found out about this!

Elevator pitch:

  • Has servers in Australia wheres Google doesn’t, so latency is an order of magnitude faster
  • Has tonnes of javascript, css and other frontend libraries – much larger selection than google
  • List of libraries is community managed via github

Those are pretty much the key points. Read more here, and it’s located at cdnjs.com.

My own testing confirms the massive latency benefit over Google – just check out this download of jQuery, 0.9 seconds for Google versus 0.06 seconds from CDNJS. That’s a crazy big difference!

[max@soshi ~]$ wget http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
--2014-11-06 10:52:08--  http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
Resolving ajax.googleapis.com (ajax.googleapis.com)... 74.125.31.95, 2404:6800:4008:c00::5f
Connecting to ajax.googleapis.com (ajax.googleapis.com)|74.125.31.95|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [text/javascript]
Saving to: ‘jquery.min.js’

    [    <=>                                                                                                             ] 92,629       102KB/s   in 0.9s   

2014-11-06 10:52:13 (102 KB/s) - ‘jquery.min.js’ saved [92629]

[max@soshi ~]$ wget http://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js
--2014-11-06 10:52:25--  http://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js
Resolving cdnjs.cloudflare.com (cdnjs.cloudflare.com)... 198.41.214.183, 198.41.215.182, 198.41.214.187, ...
Connecting to cdnjs.cloudflare.com (cdnjs.cloudflare.com)|198.41.214.183|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [application/javascript]
Saving to: ‘jquery.min.js.1’

    [ <=>                                                                                                                ] 92,629      --.-K/s   in 0.06s   

2014-11-06 10:52:30 (1.39 MB/s) - ‘jquery.min.js.1’ saved [92629]

If you’re supplying Australian customers, you would be mad not to use it.

Running PHP through PHP-FPM with Apache 2.2

Running PHP through PHP-FPM is pretty easy in Apache 2.4 (or Nginx for that matter) with ProxyPass, however Apache 2.2 has no built-in convenient way to do it.

There’s a number of solutions that exist to accomplish this, but it seems that the ambiguously named mod_fastcgi.c is the least bad. One of the biggest issues I ran into while setting it up, was allowing for multiple fpm pools for the different virtual hosts, and the specific set of configurations that I’ve figured out allowed me to do this quite easily.

There’s are number of intricate things which must be done to get it working correctly, and while I hope that I nor anyone else ever has to do this again – let’s face the bleak reality, it’s not entirely unlikely that we will… so here’s how we get it going;

  1. Download and compile (yep) the apache module from their website. I’ll spare the installation instructions since they are described in detail in the module files. Ensure you have the httpd-devel or equivalent package installed before commencing.
  2. In your httpd.conf, surround the configuration include directives with the following code:
    Include conf.d/modules/*.conf
    
    <IfModule mod_fastcgi.c>  
        FastCgiIpcDir /var/lib/httpd/fastcgi
    </IfModule>
    
    
    #
    # Load config files from the config directory "/etc/httpd/conf.d".
    #
    Include conf.d/*.conf
    
    <IfModule mod_fastcgi.c>  
        AddHandler php-fastcgi .php  
        Action php-fastcgi /usr/sbin/php-fpm.fcgi 
    
        <FilesMatch \.php$>
            SetHandler php-fastcgi
        </FilesMatch>
    </IfModule>
  3. Inside your virtual host definition file, but before and outside of the definition itself, define the fpm server like so:
    <IfModule mod_fastcgi.c>  
        FastCGIExternalServer /var/lib/php-fpm/php-fpm-prod -host 127.0.0.1:9001 -idle-timeout 900 
    </IfModule>
    
  4. Then define it’s use inside the virtual host definition:
        <IfModule mod_fastcgi.c>  
            ScriptAlias /usr/sbin/php-fpm.fcgi /var/lib/php-fpm/php-fpm-prod
        </IfModule>

Hopefully those configurations are all that you need. As always, different pools will require different names (in this case mine is called prod) and ports. Also note the directories, and ensure they exist – particularly /var/lib/php-fpm and /var/lib/httpd/fasicgi .