Thursday, April 07, 2011

Mod_Pagespeed Update On Linux With cPanel & WHM

I started using a really good new mod, mod_pagespeed on my server that speeds up web page delivery alot. It can be put in Virtual Hosts for individual websites, or applied to all websites on a server.

If a server has cpanel/whm installed, manually updates are required when a new release comes out, so I wrote a bash script to put in cron.monthly to do this. It is based on the original installation guide at http://i-comers.com/showthread.php?t=1598691

It just checks to see if the file at code.google.com is newer than the file on the server and then goes through the various installation commands, if it is, then restarts httpd. The sed section could be further modified to include any specific config that is in pagespeed.conf, but my config is in virtual hosts so I can just rewrite the paths to the mods and switch it off there and leave the config as is in httpd.conf, virtual hosts. Although it would be worth checking whether any changes have been made in the new version of pagespeed.conf, that I have not written into the code, just an email to tell me if updated, so that can be checked manually against the bak version, because the config file, as far as I know, usually stays the same....

#!/bin/bash

##if date of remote file is newer than date of local file then update mod pagespeed##

if [[ https://dl-ssl.google.com/dl/linux/direct/mod-pagespeed-beta_current_x86_64.rpm -nt /usr/local/src/mod_pagespeed/mod-pagespeed-beta_current_x86_64.rpm ]]; then


echo Mod_Pagespeed Upgrade Required!

##remove previous version##
rm -r /usr/local/src/mod_pagespeed/

cd /usr/local/src
mkdir mod_pagespeed
cd mod_pagespeed
##this is the specific rpm url required for download from google##
wget https://dl-ssl.google.com/dl/linux/direct/mod-pagespeed-beta_current_x86_64.rpm

rpm2cpio mod-pagespeed-beta_current_x86_64.rpm | cpio -idmv
cp /usr/local/src/mod_pagespeed/usr/lib64/httpd/modules/mod_pagespeed.so /usr/local/apache/modules/

##bakup previous pagespeed config##
cp /usr/local/apache/conf/pagespeed.conf /usr/local/apache/conf/pagespeedbak.conf

##the following sed commands rewrite parts of pagespeed.conf as required for different paths to files on cpanel servers. Changes should be made as per each individuals pagespeed.conf requirements.##

sed -i 's/ModPagespeed on/ModPagespeed off/g' /usr/local/src/mod_pagespeed/etc/httpd/conf.d/pagespeed.conf

sed -i 's/LoadModule pagespeed_module \/usr\/lib64\/httpd\/modules\/mod_pagespeed\.so/LoadModule pagespeed_module modules\/mod_pagespeed\.so/g' /usr/local/src/mod_pagespeed/etc/httpd/conf.d/pagespeed.conf

sed -i 's/LoadModule deflate_module \/usr\/lib64\/httpd\/modules\/mod_deflate\.so/LoadModule deflate_module modules\/mod_deflate\.so/g' /usr/local/src/mod_pagespeed/etc/httpd/conf.d/pagespeed.conf

sed -i 's/ModPagespeedFileCachePath \"\/var\/www\/mod_pagespeed\/cache\/\"/ModPagespeedFileCachePath \"\/var\/mod_pagespeed\/cache\/\"/g' /usr/local/src/mod_pagespeed/etc/httpd/conf.d/pagespeed.conf

sed -i 's/ModPagespeedGeneratedFilePrefix \"\/var\/www\/mod_pagespeed\/files\/\"/ModPagespeedGeneratedFilePrefix \"\/var\/mod_pagespeed\/files\/\"/g' /usr/local/src/mod_pagespeed/etc/httpd/conf.d/pagespeed.conf


##copy the new pagespeed.conf to httpd##
cp /usr/local/src/mod_pagespeed/etc/httpd/conf.d/pagespeed.conf /usr/local/apache/conf/

##set directory permissions and create directories for files and cache##
chmod 755 /usr/local/apache/modules/mod_pagespeed.so
mkdir /var/mod_pagespeed/{cache,files} -p
chown nobody:nobody /var/mod_pagespeed/*

##restart httpd##
service httpd restart

##send mail to alert of update so that pagespeed.conf can be altered if necessary, can be checked against pagespeedbak.conf created during the update.##

SUBJECT="Mod Pagespeed has been updated"
EMAIL="someone@somewhere.com"
EMAILMESSAGE="/tmp/emailmessage.txt"
echo "Mod Pagespeed has been updated, check pagespeed.conf"> $EMAILMESSAGE
/bin/mail -s "$SUBJECT" "$EMAIL" < $EMAILMESSAGE
rm /tmp/emailmessage.txt

##else if the rpm on google has not been updated then just say so##
else
echo Mod_Pagespeed Upgrade not required at this time!
fi




Obviously the SED section of the code would need to be altered depending on what configs are set in pagespeed.conf, the above example is what I require in mine.

No comments: