Sunday, December 21, 2014

Adding pagespeed module to nginx 1.7.8 on Ubuntu 14.04

Wanted run fresh nginx version with Google Pagespeed on Ubuntu 14.04.


# We can't use pre-build binaries because of pagespeed module is available only as source

# Get nginx 1.7.8 from Ubuntu PPA and patch it before recompiling

# Get rebuild deps
add-apt-repository -s -y ppa:nginx/development
apt-get update
apt-get -y install devscripts
apt-get -y build-dep nginx=1.7.8

# Create build environment
adduser bobbuilder --shell /bin/bash \
        --disabled-password --gecos bobbuilder

# Switch to non-priviledged user for build
sudo su - bobbuilder

# Download ubuntu sources
mkdir -p ~/build/nginx
cd ~/build/nginx
NGINX_VERSION=1.7.8
apt-get -y source nginx=${NGINX_VERSION}

# Download Pagespeed
cd nginx-${NGINX_VERSION}/debian/modules
NPS_VERSION=1.9.32.2
wget https://github.com/pagespeed/ngx_pagespeed/archive/release-${NPS_VERSION}-beta.zip
unzip release-${NPS_VERSION}-beta.zip
rm -f release-${NPS_VERSION}-beta.zip
cd ngx_pagespeed-release-${NPS_VERSION}-beta/
wget https://dl.google.com/dl/page-speed/psol/${NPS_VERSION}.tar.gz
tar --no-same-owner -xzvf ${NPS_VERSION}.tar.gz
rm -f ${NPS_VERSION}.tar.gz

# Fix configure script parameters to include aio and pagespeed in full and extra package variants
cd ../..
sed -i.bak rules \
    -e's|filter_module$|filter_module --with-file-aio --add-module=$(MODULESDIR)/ngx_pagespeed-release-'${NPS_VERSION}'-beta|g'

# Build
cd ..
debuild -i -us -uc -b -j8

# You should end up with something like this:

# -rw-r--r--  1 bobbuilder bobbuilder    21406 Dec  7 21:29 nginx_1.7.8-0ubuntu1_all.deb
# -rw-r--r--  1 bobbuilder bobbuilder    34474 Dec  7 21:29 nginx-common_1.7.8-0ubuntu1_all.deb
# -rw-r--r--  1 bobbuilder bobbuilder    32640 Dec  7 21:29 nginx-doc_1.7.8-0ubuntu1_all.deb
# -rw-r--r--  1 bobbuilder bobbuilder  3342726 Dec  7 21:29 nginx-extras_1.7.8-0ubuntu1_amd64.deb
# -rw-r--r--  1 bobbuilder bobbuilder 32103256 Dec  7 21:29 nginx-extras-dbg_1.7.8-0ubuntu1_amd64.deb
# -rw-r--r--  1 bobbuilder bobbuilder  3172516 Dec  7 21:29 nginx-full_1.7.8-0ubuntu1_amd64.deb
# -rw-r--r--  1 bobbuilder bobbuilder 30220032 Dec  7 21:29 nginx-full-dbg_1.7.8-0ubuntu1_amd64.deb
# -rw-r--r--  1 bobbuilder bobbuilder   286010 Dec  7 21:29 nginx-light_1.7.8-0ubuntu1_amd64.deb
# -rw-r--r--  1 bobbuilder bobbuilder  2250330 Dec  7 21:29 nginx-light-dbg_1.7.8-0ubuntu1_amd64.deb

# Switch back to root
exit

# Remove now unneeded and unwanted ppa repo
add-apt-repository -r -y ppa:nginx/development
apt-get update

# Install our customized version
cd ~bobbuilder/build/nginx
dpkg -i nginx_*_all.deb nginx-common_*_all.deb nginx-doc_*_all.deb nginx-extras_*_amd64.deb

# Prevent updates
echo "nginx hold"        | dpkg --set-selections 
echo "nginx-common hold" | dpkg --set-selections 
echo "nginx-doc hold"    | dpkg --set-selections 
echo "nginx-extras hold" | dpkg --set-selections 

# Edit /etc/nginx/sites-available/default and add under "server {" following
pagespeed on;
pagespeed RewriteLevel CoreFilters;
pagespeed FileCachePath "/var/cache/ngx_pagespeed/";
pagespeed EnableFilters combine_css,combine_javascript,remove_comments,collapse_whitespace;

# Fix permissions
mkdir -p /var/cache/ngx_pagespeed/
chown www-data:www-data /var/cache/ngx_pagespeed/

# Restart nginx
service nginx restart

# Load test page and check source, optimizations should be visible in whitespace for example

# Without optimizations:
# <title>Welcome to nginx on Debian!</title>
# <style>
#     body {
#         width: 35em;
#         margin: 0 auto;
#         font-family: Tahoma, Verdana, Arial, sans-serif;
#     }
# </style>

# With optimizations:
# <title>Welcome to nginx on Debian!</title>
# <style>body{width:35em;margin:0 auto;font-family:Tahoma,Verdana,Arial,sans-serif}</style>

# Ok, so now we know basic nginx and pagespeed module work ok.


If you're trying to compile newer nginx than present in Ubuntu PPA it's not much different process. Currently 1.7.8 is latest on both nginx.org and Ubuntu PPA, but last week PPA only had 1.7.7.

Only difference to process described above to build 1.7.8 based on Ubuntu 1.7.7 was:

# Download ubuntu sources
mkdir -p ~/build/nginx
cd ~/build/nginx
apt-get -y source nginx=1.7.7

# Build 1.7.8 instead of 1.7.7
wget http://nginx.org/download/nginx-1.7.8.tar.gz
cd nginx-1.7.7
uupdate ../nginx-1.7.8.tar.gz -v 1.7.8

# Download Pagespeed
cd ../nginx-1.7.8/debian/modules


No comments:

Post a Comment

Got something to say?!