April 2024
M T W T F S S
1234567
891011121314
15161718192021
22232425262728
2930  

Categories

April 2024
M T W T F S S
1234567
891011121314
15161718192021
22232425262728
2930  

How to setup Pagespeed module in Nginx using rpm or yum on CentOS 7

How to setup Pagespeed module in Nginx using rpm or yum on CentOS 7

Adding Pagespeed module in Apache is very easy with package manager:

sudo yum install at # if you do not already have ‘at’ installed
sudo rpm -U mod-pagespeed-*.rpm

… but if we want to add Pagespeed module in Nginx, first thing we think of is compiling its source along with the Pagespeed module. What if Nginx is installed in our server from yum or rpm? The process we need is to repackage RPM and that is what this tutorial will show.
Prepare tools

The following procedures are tested on my Linode server running Centos 7 64-bit Linux distribution.
First we need to install the necessary tools to repackage RPM:

sudo yum install rpm-build gcc-c++ pcre-devel zlib-devel make unzip

It is recommended to not to use root user in building RPM, so we will create a new user:

useradd -m builder

* The -m is to create home directory
Select the latest version of Nginx RPM source here and download (as of this writing the latest is nginx-1.8.1-1.el7.ngx.src.rpm):

rpm -Uvh http://nginx.org/packages/centos/7/SRPMS/nginx-1.8.1-1.el7.ngx.src.rpm

Move the Nginx RPM source to the new user created “builder” home directory:

mv /root/rpmbuild /home/builder/ && chown -R builder. /home/builder/rpmbuild

Pagespeed source

Login as “builder” and go to RPM source directory:

su builder
cd ~/rpmbuild/SOURCES/

Download and extract the latest version of Pagespeed:

wget https://github.com/pagespeed/ngx_pagespeed/archive/release-1.11.33.0-beta.zip
unzip release-1.11.33.0-beta.zip
rm release-1.11.33.0-beta.zip

Inside the extracted Pagespeed source directory, download and extract its required PSOL library:

cd ngx_pagespeed-release-1.11.33.0-beta/
wget https://dl.google.com/dl/page-speed/psol/1.11.33.0.tar.gz
tar -zxvf 1.11.33.0.tar.gz
rm 1.11.33.0.tar.gz

Archive both Pagespeed and PSOL as one:

cd ..
tar -zcvf ngx_pagespeed-release-1.11.33.0-beta.tar.gz ngx_pagespeed-release-1.11.33.0-beta/
rm -r ngx_pagespeed-release-1.11.33.0-beta/
cd ~

Enable Pagespeed in Nginx spec

Open and edit “nginx.spec” file:

vi ~/rpmbuild/SPECS/nginx.spec

Search for “Source0: http://nginx.org/download/%{name}-%{version}.tar.gz” and add above it:

%define pagespeed_version 1.11.33.0-beta

Note: ‘1.11.33.0-beta” is the version of Pagespeed.
Scroll down and search “Source10: nginx.suse.logrotate” and add under it:

Source11: ngx_pagespeed-release-%{pagespeed_version}.tar.gz

Again scroll down and search “%setup -q” and add under it:

%{__tar} zxvf %{SOURCE11}
%setup -T -D -a 11

And scroll down and search “–with-cc-opt=”%{optflags} $(pcre-config –cflags)” \” and add under it (Note: there are two of these):

–add-module=%{_builddir}/%{name}-%{version}/ngx_pagespeed-release-%{pagespeed_version} \

And save the nginx.spec file.
Build and install the new Nginx with Pagespeed module

Build the Nginx RPM package:

rpmbuild -ba ~/rpmbuild/SPECS/nginx.spec

After the compilation completed you should see output message at the end something like these:

Wrote: /home/builder/rpmbuild/SRPMS/nginx-1.8.1-1.el7.centos.ngx.src.rpm
Wrote: /home/builder/rpmbuild/RPMS/x86_64/nginx-1.8.1-1.el7.centos.ngx.x86_64.rpm
Wrote: /home/builder/rpmbuild/RPMS/x86_64/nginx-debug-1.8.1-1.el7.centos.ngx.x86_64.rpm
Wrote: /home/builder/rpmbuild/RPMS/x86_64/nginx-debuginfo-1.8.1-1.el7.centos.ngx.x86_64.rpm
Executing(%clean): /bin/sh -e /var/tmp/rpm-tmp.lWzVrm
+ umask 022
+ cd /home/builder/rpmbuild/BUILD
+ cd nginx-1.8.1
+ /usr/bin/rm -rf /home/builder/rpmbuild/BUILDROOT/nginx-1.8.1-1.el7.centos.ngx.x86_64
+ exit 0

As we can see the Nginx RPM package bundled with Pagespeed is saved in ~/rpmbuild/RPMS/
Login as root in able to install the compiled Nginx RPM:

su

You can use yum or rpm to install it:

yum update /home/builder/rpmbuild/RPMS/x86_64/nginx-1.8.1-1.el7.centos.ngx.x86_64.rpm

… or:

rpm -Uvh /home/builder/rpmbuild/RPMS/x86_64/nginx-1.8.1-1.el7.centos.ngx.x86_64.rpm

If the current nginx is higher version than we have compiled (like in my case I have nginx v1.9.4), execute the following to force install the Nginx RPM we have build:

yum downgrade /home/builder/rpmbuild/RPMS/x86_64/nginx-1.8.1-1.el7.centos.ngx.x86_64.rpm

… or:

rpm -Uvh –oldpackage /home/builder/rpmbuild/RPMS/x86_64/nginx-1.8.1-1.el7.centos.ngx.x86_64.rpm

Make sure it auto-start upon reboot:

chkconfig nginx on

To check if Pagespeed is included in Nginx:

nginx -V

If you don’t want to build the RPM in your production server, you may use other machine.

In my case, I just build my RPM in my laptop using CentOS 7 64-bit Vagrant box. Just make sure they have both the same system type (OS and processor).
For PageSpeed Nginx configuration,

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>