Home » Random Goodies » CentOS + cPanel + Mod Pagespeed

CentOS + cPanel + Mod Pagespeed

Tired of applications with poor or no caching options? Don’t wish to change your PHP Handler for OPCode? Need a boost? Give Mod_Pagespeed a try! Mod_Pagespeed was developed by Google and it acts as an internal proxy which combines your CSS/Javascript/Images then serves them inline and much more. It also makes use of Gzip compression, and can boost your Pagespeed & Yslow grade from an F to an easy A! (true story).

The following is a basic script which will install and configure mod_pagespeed along with mod_deflate (required for gzip compression) on a cPanel server. It is compatible with x86 and x64_64 architectures:

# Mod_Pagespeed installer for CentOS + cPanel
# author: Andrei D
# contact: [email protected]
# Released under the GNU v2 License.
#
if [ -z "$(grep CentOS /etc/redhat-release)" ];then
echo "This installer is provided for a CentOS server." && exit
fi
[[ httpd -V|awk '/version/ {print$3}'|cut -d/ -f2|grep -oP '^2.4.1$' ]] && echo -e "***WARNING***\n\nApache 2.4.1 has a known bug that causes stability problems with mod_pagespeed. Upgrading Apache is suggested.\n\nYou have 10 seconds to cancel the installation." && sleep 10
if [ -z "$(httpd -M 2>&1|grep deflate)" ];then
apxs -cia /home/cpeasyapache/src/httpd-$(httpd -V|awk -F'/' '/Server version:/ {print$2}'|cut -d' ' -f1)/modules/filters/mod_deflate.c
/usr/local/cpanel/bin/apache_conf_distiller --update
fi
mkdir -p /usr/local/src/mod_pagespeed
cd /usr/local/src/mod_pagespeed
arch=$(uname -i)
wget https://dl-ssl.google.com/dl/linux/direct/mod-pagespeed-stable_current_${arch}.rpm
rpm2cpio mod-pagespeed-stable_current_*.rpm | cpio -idmv
cp /usr/local/src/mod_pagespeed/usr/{lib,lib64}/httpd/modules/mod_pagespeed*.so /usr/local/apache/modules/
chmod +x /usr/local/apache/modules/mod_pagespeed*.so
mkdir -p /tmp/mod_pagespeed/{cache,files}
chown -R nobody. /tmp/mod_pagespeed/
wget http://www.gurutek.biz/configs/pagespeed.conf -P /usr/local/apache/conf/
echo "Include /usr/local/apache/conf/pagespeed.conf" >> /usr/local/apache/conf/includes/pre_main_global.conf
service httpd restart
if [ -n "$(httpd -M 2>&1|grep pagespeed)" ];then
echo "Mod_Pagespeed was successfully installed"
echo "Cache will be built in /tmp/mod_pagespeed"
else
echo "Uh oh... Looks like it didn't properly install."
echo "Check for errors above and get in touch."
fi

Direct download available: here
Oneline install: bash <(wget -qO- http://gurutek.biz/scripts/pagespeed.sh)

Also, for those of you who wish to make better use of your RAM, the following commands will load Pagespeed in RAM:

First we make a backup:

cp -pv /etc/fstab /etc/fstab.bak

Now we create 512MB tmpfs entry in /etc/fstab so it gets recreated on boot and mount it:

echo "tmpfs /tmp/mod_pagespeed/cache tmpfs size=512m,mode=0775,uid=nobody,gid=nobody 0 0" >> /etc/fstab
mount -a

Questions? Comments? Drop a line and enjoy!

  • jorge

    how to remove?

  • admin

    Hi,

    To remove the installed files:

    rm -rf /usr/local/src/mod_pagespeed /usr/local/apache/modules/mod_pagespeed{,_ap24}.so /tmp/mod_pagespeed /usr/local/apache/conf/pagespeed.conf
    i='Include /usr/local/apache/conf/pagespeed.conf'
    sed -i.bak "/$i/d" /usr/local/apache/conf/includes/pre_main_global.conf
    service httpd restart

  • Works as charm. One suggestiong tough, you could a check for apache version to your script, eg. if it’s 2.4 or 2.2.

    Thanks and keep the good work up!

  • Also I found out some issue with SSL sites, it there way to disable it on SSL websites or particular folder even?

  • admin

    If you’re on cPanel you can create:

    /usr/local/apache/conf/userdata/ssl/pagespeed.conf

    To disable HTML rewriting but continue to serve .pagespeed. resources and parse query options use:

    ModPagespeed off

    To completely disable mod_pagespeed (.pagespeed. resources will result in 404) use:

    ModPagespeed unplugged

    I’ll modify the script for Apache 2.4 detection/support tomorrow or so when I have some time – thanks 🙂

  • admin

    Apache 2.4 support added, script/post updated. It will also give you a nice big warning if 2.4.1 is detected due to a known bug causing stability issues with mod_pagespeed.