{"id":5959,"date":"2016-05-17T12:33:43","date_gmt":"2016-05-17T04:33:43","guid":{"rendered":"http:\/\/rmohan.com\/?p=5959"},"modified":"2016-05-17T12:33:59","modified_gmt":"2016-05-17T04:33:59","slug":"how-to-setup-pagespeed-module-in-nginx-using-rpm-or-yum-on-centos-7","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=5959","title":{"rendered":"How to setup Pagespeed module in Nginx using rpm or yum on CentOS 7"},"content":{"rendered":"<p>How to setup Pagespeed module in Nginx using rpm or yum on CentOS 7<\/p>\n<p>Adding Pagespeed module in Apache is very easy with package manager:<\/p>\n<p>sudo yum install at # if you do not already have &#8216;at&#8217; installed<br \/>\nsudo rpm -U mod-pagespeed-*.rpm<\/p>\n<p>&#8230; 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.<br \/>\nPrepare tools<\/p>\n<p>The following procedures are tested on my Linode server running Centos 7 64-bit Linux distribution.<br \/>\nFirst we need to install the necessary tools to repackage RPM:<\/p>\n<p>sudo yum install rpm-build gcc-c++ pcre-devel zlib-devel make unzip<\/p>\n<p>It is recommended to not to use root user in building RPM, so we will create a new user:<\/p>\n<p>useradd -m builder<\/p>\n<p>* The -m is to create home directory<br \/>\nSelect 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):<\/p>\n<p>rpm -Uvh http:\/\/nginx.org\/packages\/centos\/7\/SRPMS\/nginx-1.8.1-1.el7.ngx.src.rpm<\/p>\n<p>Move the Nginx RPM source to the new user created &#8220;builder&#8221; home directory:<\/p>\n<p>mv \/root\/rpmbuild \/home\/builder\/ &amp;&amp; chown -R builder. \/home\/builder\/rpmbuild<\/p>\n<p>Pagespeed source<\/p>\n<p>Login as &#8220;builder&#8221; and go to RPM source directory:<\/p>\n<p>su builder<br \/>\ncd ~\/rpmbuild\/SOURCES\/<\/p>\n<p>Download and extract the latest version of Pagespeed:<\/p>\n<p>wget https:\/\/github.com\/pagespeed\/ngx_pagespeed\/archive\/release-1.11.33.0-beta.zip<br \/>\nunzip release-1.11.33.0-beta.zip<br \/>\nrm release-1.11.33.0-beta.zip<\/p>\n<p>Inside the extracted Pagespeed source directory, download and extract its required PSOL library:<\/p>\n<p>cd ngx_pagespeed-release-1.11.33.0-beta\/<br \/>\nwget https:\/\/dl.google.com\/dl\/page-speed\/psol\/1.11.33.0.tar.gz<br \/>\ntar -zxvf 1.11.33.0.tar.gz<br \/>\nrm 1.11.33.0.tar.gz<\/p>\n<p>Archive both Pagespeed and PSOL as one:<\/p>\n<p>cd ..<br \/>\ntar -zcvf ngx_pagespeed-release-1.11.33.0-beta.tar.gz ngx_pagespeed-release-1.11.33.0-beta\/<br \/>\nrm -r ngx_pagespeed-release-1.11.33.0-beta\/<br \/>\ncd ~<\/p>\n<p>Enable Pagespeed in Nginx spec<\/p>\n<p>Open and edit &#8220;nginx.spec&#8221; file:<\/p>\n<p>vi ~\/rpmbuild\/SPECS\/nginx.spec<\/p>\n<p>Search for &#8220;Source0: http:\/\/nginx.org\/download\/%{name}-%{version}.tar.gz&#8221; and add above it:<\/p>\n<p>%define pagespeed_version 1.11.33.0-beta<\/p>\n<p>Note: &#8216;1.11.33.0-beta&#8221; is the version of Pagespeed.<br \/>\nScroll down and search &#8220;Source10: nginx.suse.logrotate&#8221; and add under it:<\/p>\n<p>Source11: ngx_pagespeed-release-%{pagespeed_version}.tar.gz<\/p>\n<p>Again scroll down and search &#8220;%setup -q&#8221; and add under it:<\/p>\n<p>%{__tar} zxvf %{SOURCE11}<br \/>\n%setup -T -D -a 11<\/p>\n<p>And scroll down and search &#8220;&#8211;with-cc-opt=&#8221;%{optflags} $(pcre-config &#8211;cflags)&#8221; \\&#8221; and add under it (Note: there are two of these):<\/p>\n<p>&#8211;add-module=%{_builddir}\/%{name}-%{version}\/ngx_pagespeed-release-%{pagespeed_version} \\<\/p>\n<p>And save the nginx.spec file.<br \/>\nBuild and install the new Nginx with Pagespeed module<\/p>\n<p>Build the Nginx RPM package:<\/p>\n<p>rpmbuild -ba ~\/rpmbuild\/SPECS\/nginx.spec<\/p>\n<p>After the compilation completed you should see output message at the end something like these:<\/p>\n<p>Wrote: \/home\/builder\/rpmbuild\/SRPMS\/nginx-1.8.1-1.el7.centos.ngx.src.rpm<br \/>\nWrote: \/home\/builder\/rpmbuild\/RPMS\/x86_64\/nginx-1.8.1-1.el7.centos.ngx.x86_64.rpm<br \/>\nWrote: \/home\/builder\/rpmbuild\/RPMS\/x86_64\/nginx-debug-1.8.1-1.el7.centos.ngx.x86_64.rpm<br \/>\nWrote: \/home\/builder\/rpmbuild\/RPMS\/x86_64\/nginx-debuginfo-1.8.1-1.el7.centos.ngx.x86_64.rpm<br \/>\nExecuting(%clean): \/bin\/sh -e \/var\/tmp\/rpm-tmp.lWzVrm<br \/>\n+ umask 022<br \/>\n+ cd \/home\/builder\/rpmbuild\/BUILD<br \/>\n+ cd nginx-1.8.1<br \/>\n+ \/usr\/bin\/rm -rf \/home\/builder\/rpmbuild\/BUILDROOT\/nginx-1.8.1-1.el7.centos.ngx.x86_64<br \/>\n+ exit 0<\/p>\n<p>As we can see the Nginx RPM package bundled with Pagespeed is saved in ~\/rpmbuild\/RPMS\/<br \/>\nLogin as root in able to install the compiled Nginx RPM:<\/p>\n<p>su<\/p>\n<p>You can use yum or rpm to install it:<\/p>\n<p>yum update \/home\/builder\/rpmbuild\/RPMS\/x86_64\/nginx-1.8.1-1.el7.centos.ngx.x86_64.rpm<\/p>\n<p>&#8230; or:<\/p>\n<p>rpm -Uvh \/home\/builder\/rpmbuild\/RPMS\/x86_64\/nginx-1.8.1-1.el7.centos.ngx.x86_64.rpm<\/p>\n<p>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:<\/p>\n<p>yum downgrade \/home\/builder\/rpmbuild\/RPMS\/x86_64\/nginx-1.8.1-1.el7.centos.ngx.x86_64.rpm<\/p>\n<p>&#8230; or:<\/p>\n<p>rpm -Uvh &#8211;oldpackage \/home\/builder\/rpmbuild\/RPMS\/x86_64\/nginx-1.8.1-1.el7.centos.ngx.x86_64.rpm<\/p>\n<p>Make sure it auto-start upon reboot:<\/p>\n<p>chkconfig nginx on<\/p>\n<p>To check if Pagespeed is included in Nginx:<\/p>\n<p>nginx -V<\/p>\n<p>If you don&#8217;t want to build the RPM in your production server, you may use other machine.<\/p>\n<p>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).<br \/>\nFor PageSpeed Nginx configuration,<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to setup Pagespeed module in Nginx using rpm or yum on CentOS 7<\/p>\n<p>Adding Pagespeed module in Apache is very easy with package manager:<\/p>\n<p>sudo yum install at # if you do not already have &#8216;at&#8217; installed sudo rpm -U mod-pagespeed-*.rpm<\/p>\n<p>&#8230; but if we want to add Pagespeed module in Nginx, first thing [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[73,70],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/5959"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5959"}],"version-history":[{"count":1,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/5959\/revisions"}],"predecessor-version":[{"id":5960,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/5959\/revisions\/5960"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5959"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5959"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5959"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}