Apache

Installing Mod_PageSpeed on Apache-2.4 / Gentoo

Lately, I’ve been working on improving page speeds on a variety of sites. I’ve been referencing PageSpeed Insights at Google, and it’s been quite challenging to get the speeds down. One option was to install mod_pagespeed on my apache 2.4 server. Mod_pagespeed is an Apache module that does some work transforming the output HTML to browsers, and it also does caching and optimization of images. I did run across some problem and I’m still working with them, but wanted to get this post up for reference down the road.

Since mod_pagespeed isn’t in portage, I had to do a manual compile of this. Here is a breakdown of my command history on this. I also had to update apache with ‘version’ since that is required. That’s the action with editing make.conf and emerging apache. If you have version compiled into apache, you can skip that step.

cd /usr/local/src/
wget https://dl-ssl.google.com/dl/linux/direct/mod-pagespeed-stable_current_x86_64.rpm
rpm2targz mod-pagespeed-stable_current_x86_64.rpm
gunzip mod-pagespeed-stable_current_x86_64.tar.gz
tar xvf mod-pagespeed-stable_current_x86_64.tar
mkdir mod-pagespeed-stable_current_x86_64
mv etc/ mod-pagespeed-stable_current_x86_64/.
mv usr/ mod-pagespeed-stable_current_x86_64/.
mv var/ mod-pagespeed-stable_current_x86_64/.
cd mod-pagespeed-stable_current_x86_64
cp usr/lib64/httpd/modules/mod_pagespeed_ap24.so /usr/lib64/apache2/modules/.
cp etc/httpd/conf.d/pagespeed.conf /etc/apache2/modules.d/80_mod_pagespeed.conf
cp etc/httpd/conf.d/pagespeed_libraries.conf /etc/apache2/modules.d/.
mkdir /var/cache/mod_pagespeed/
chown apache:apache /var/cache/mod_pagespeed/
sed -i 's/\/usr\/lib64\/httpd/\/usr\/lib64\/apache2/' /etc/apache2/modules.d/80_mod_pagespeed.conf
vi /etc/make.conf ## added version
emerge -v apache

Here are some useful links:

https://www.modpagespeed.com/doc/download
https://www.modpagespeed.com/doc/https_support
https://serverstuff.info/wordpress/2014/10/25/gentoo-apache-2-4-and-googles-mod_pagespeed/
https://moz.com/blog/use-googles-pagespeed-module-to-dramatically-increase-the-speed-of-your-website

I ran into some issues since my Apache chroot’d. For some reason the cache path and logs needed to be in the chroot. My sites also used SSL, so had to work with that. Here are my config options in my vhost

ModPagespeedFetchHttps enable,allow_unknown_certificate_authority
ModPagespeedSslCertDirectory /etc/apache2/ssl/
ModPagespeedSslCertFile /etc/apache2/ssl/www_example_com.crt
ModPagespeedMaxSegmentLength 250
ModPagespeedDomain https://www.example.com
ModPagespeedLoadFromFileMatch "^https://www.example.com/files/" "/var/chroot/apache/var/www/example.com/htdocs/files/"
ModPagespeedLoadFromFileMatch "^https://www.example.com/files/images/" "/var/chroot/apache/var/www/example.com/htdocs/files/images/"
ModPagespeedLoadFromFileMatch "^https://www.example.com/files/gallery/" "/var/chroot/apache/var/www/example.com/htdocs/files/gallery/"
ModPagespeedLoadFromFileMatch "^https://www.example.com/files/misc/" "/var/chroot/apache/var/www/example.com/htdocs/files/misc/"
ModPagespeedLoadFromFileMatch "^https://www.example.com/assets/images/" "/var/chroot/apache/var/www/example.com/htdocs/assets/images/"
ModPagespeedLoadFromFileMatch "^https://www.example.com/assets/css/" "/var/chroot/apache/var/www/example.com/htdocs/assets/css/"
ModPagespeedLoadFromFileMatch "^https://www.example.com/assets/js/" "/var/chroot/apache/var/www/example.com/htdocs/assets/js/"
ModPagespeedLoadFromFileMatch "^https://www.example.com/favicon.ico" "/var/chroot/apache/var/www/example.com/htdocs/favicon.ico"
ModPagespeedEnableFilters extend_cache
ModPagespeedEnableFilters sprite_images
ModPagespeedEnableFilters rewrite_images
ModPagespeedEnableFilters recompress_png
ModPagespeedEnableFilters convert_png_to_jpeg,convert_jpeg_to_webp
ModPagespeedEnableFilters collapse_whitespace,remove_comments
ModPagespeedHttpCacheCompressionLevel 9
ModPagespeedStatisticsLogging on

I’m still having a pproblem with pagespeed_admin and pagespeed_global_admin access. I continually get ‘denied’ messages even though it’s allowed. I’ve been reading about mod_rewrite potentially causing problems here, and I do run mod_rewrite with this vhost. Still working with that.

The annoying thing about this module is that’s default on globally. So you need to specifically set it to off on other vhosts.

<IfModule pagespeed_module>
	ModPagespeed off
</IfModule>

Overall, it did speed things up, but the weight was related to other problems on the site, so not sure how much this is helping overall. Small percentage because alot of the compression was already done on my end, etc.