{"id":18845,"date":"2016-04-03T12:42:23","date_gmt":"2016-04-03T17:42:23","guid":{"rendered":"https:\/\/www.rosehosting.com\/blog\/?p=18845"},"modified":"2022-06-03T03:43:41","modified_gmt":"2022-06-03T08:43:41","slug":"how-to-build-nginx-with-pagespeed-module","status":"publish","type":"post","link":"https:\/\/www.rosehosting.com\/blog\/how-to-build-nginx-with-pagespeed-module\/","title":{"rendered":"How to build nginx with Pagespeed module"},"content":{"rendered":"
In this guide, we will show you how to build and install the latest stable nginx version with the ngx_pagespeed module on an Ubuntu 14.04 VPS<\/a>. With the Pagespeed module you can combine and minify CSS and JavaScript, optimize and convert images, remove comments and whitespaces from HTML, and perform many other optimizations.<\/p>\n <\/p>\n To update your VPS and install all necessary dependencies, run:<\/p>\n Ubuntu 14.04 comes with Nginx version 1.4, to get the latest stable version of Nginx v1.8.1 we’ll need to add the Nginx PPA repository :<\/p>\n Edit the PPA’s sources file and uncomment the deb-src directive.<\/p>\n and update the package lists from the repositories:<\/p>\n Install the Nginx build dependencies:<\/p>\n cd to the download and unpack the latest ngx_pagespeed and psol modules using the following commands:<\/p>\n Switch to the directory where you downloaded the Nginx source packages<\/p>\n The PPA If you want to use nginx-light instead of the nginx-full you should add the same line to the Now we are ready to build Nginx using the following command:<\/p>\n The build process will take a few minutes to complete.<\/p>\n Once the build process is complete, you will find few .deb files in the We added the Pagespeed Module to the “full” package, so we will install that package including the nginx-common package:<\/p>\n Once the installation is complete, to see what modules are installed run Create a directory for the cached files:<\/p>\n Create a new nginx configuration for the Pagespeed Module.<\/p>\n You can uncomment the filters you need for your website or add a new ones, for more info about Pagespeed filters please check the official PageSpeed<\/a> documentation.<\/p>\n In every Nginx server block where you want to enable the PageSpeed Module add:<\/p>\n Example:<\/p>\n Restart your nginx service and you are ready to go.<\/p>\n Of course you don\u2019t have to do any of this if you use one of our Linux VPS Hosting<\/a> services, in which case you can simply ask our expert Linux admins to setup this for you. They are available 24×7 and will take care of your request immediately.<\/p>\n PS<\/strong><\/span>. If you liked this post please share it with your friends on the social networks using the buttons on the left or simply leave a reply below. Thanks.<\/p>\n","protected":false},"excerpt":{"rendered":" In this guide, we will show you how to build and install the latest stable nginx version with the ngx_pagespeed … <\/p>\nLogin to your VPS via SSH<\/h4>\n
ssh user@vps_IP<\/pre>\n
Update the system and install necessary packages<\/h4>\n
[user]$ sudo apt-get update && sudo apt-get -y upgrade\r\n[user]$ sudo apt-get install software-properties-common nano wget dpkg-dev \\ \r\n\t\t\tbuild-essential zlib1g-dev libpcre3 libpcre3-dev unzip<\/pre>\n
Download Nginx and Pagespeed Sources<\/h4>\n
[user]$ sudo add-apt-repository -y ppa:nginx\/stable<\/pre>\n
[user]$ sudo nano \/etc\/apt\/sources.list.d\/nginx-stable-trusty.list<\/pre>\n
# deb http:\/\/ppa.launchpad.net\/nginx\/stable\/ubuntu trusty main\r\ndeb-src http:\/\/ppa.launchpad.net\/nginx\/stable\/ubuntu trusty main<\/pre>\n
[user]$ sudo apt-get update<\/pre>\n
[user]$ sudo apt-get build-dep nginx<\/pre>\n
\/usr\/local\/src<\/code> and download the Nginx source package:<\/p>\n
[user]$ cd \/usr\/local\/src\r\nsudo apt-get source nginx<\/pre>\n
[user]$ cd nginx-1.8.1\/debian\/modules\r\n[user]$ NPS_VERSION=1.10.33.6\r\n[user]$ sudo wget https:\/\/github.com\/pagespeed\/ngx_pagespeed\/archive\/release-${NPS_VERSION}-beta.zip -O release-${NPS_VERSION}-beta.zip\r\n[user]$ sudo unzip release-${NPS_VERSION}-beta.zip\r\n[user]$ cd ngx_pagespeed-release-${NPS_VERSION}-beta\/\r\n[user]$ sudo wget https:\/\/dl.google.com\/dl\/page-speed\/psol\/${NPS_VERSION}.tar.gz\r\n[user]$ sudo tar -xzvf ${NPS_VERSION}.tar.gz<\/pre>\n
Build Nginx with Pagespeed Module<\/h4>\n
[user]$ cd \/usr\/local\/src\/nginx-1.8.1<\/pre>\n
ppa:nginx\/stable<\/code> we have previously added has multiple Nginx packages available: nginx-common, nginx-full, nginx-light, nginx-extras and nginx-doc. For this guide we will add the Pagespeed Module to the nginx-full package, to do that we need to edit the
debian\/rules<\/code> file and add the module to the
full_configure_flags<\/code>as shown bellow:<\/p>\n
[user]$ cat debian\/rules<\/pre>\n
full_configure_flags := \\\r\n $(common_configure_flags) \\\r\n --with-http_addition_module \\\r\n --with-http_dav_module \\\r\n --with-http_geoip_module \\\r\n --with-http_gunzip_module \\\r\n --with-http_gzip_static_module \\\r\n --with-http_image_filter_module \\\r\n --with-http_spdy_module \\\r\n --with-http_sub_module \\\r\n --with-http_xslt_module \\\r\n --with-mail \\\r\n --with-mail_ssl_module \\\r\n --add-module=$(MODULESDIR)\/nginx-auth-pam \\\r\n --add-module=$(MODULESDIR)\/nginx-dav-ext-module \\\r\n --add-module=$(MODULESDIR)\/nginx-echo \\\r\n --add-module=$(MODULESDIR)\/nginx-upstream-fair \\\r\n --add-module=$(MODULESDIR)\/ngx_http_substitutions_filter_module \\\r\n --add-module=$(MODULESDIR)\/ngx_pagespeed-release-1.10.33.6-beta\r\n<\/pre>\n
light_configure_flags<\/code> block.<\/p>\n
[user]$ sudo dpkg-buildpackage -b<\/pre>\n
\/usr\/local\/src\/<\/code> directory.<\/p>\n
[user]$ ls -1 \/usr\/local\/src\/ | grep .deb$<\/pre>\n
nginx_1.8.1-1+trusty0_all.deb\r\nnginx-common_1.8.1-1+trusty0_all.deb\r\nnginx-doc_1.8.1-1+trusty0_all.deb\r\nnginx-extras_1.8.1-1+trusty0_amd64.deb\r\nnginx-extras-dbg_1.8.1-1+trusty0_amd64.deb\r\nnginx-full_1.8.1-1+trusty0_amd64.deb\r\nnginx-full-dbg_1.8.1-1+trusty0_amd64.deb\r\nnginx-light_1.8.1-1+trusty0_amd64.deb\r\nnginx-light-dbg_1.8.1-1+trusty0_amd64.deb\r\n<\/pre>\n
[user]$ cd \/usr\/local\/src\r\n[user]$ sudo dpkg -i nginx-full_1.8.1-1+trusty0_amd64.deb nginx-common_1.8.1-1+trusty0_all.deb\r\n<\/pre>\n
nginx -V<\/code> .<\/p>\n
nginx version: nginx\/1.8.1\r\nbuilt with OpenSSL 1.0.2g 1 Mar 2016\r\nTLS SNI support enabled\r\nconfigure arguments:\r\n...\r\n--add-module=\/usr\/local\/src\/nginx-1.8.1\/debian\/modules\/ngx_pagespeed-release-1.10.33.6-beta\r\n...\r\n<\/pre>\n
[user]$ sudo mkdir -p \/var\/ngx_pagespeed_cache\r\n[user]$ sudo chown -R www-data: \/var\/ngx_pagespeed_cache<\/pre>\n
[user]$ sudo nano \/etc\/nginx\/pagespeed.conf<\/pre>\n
pagespeed on;\r\npagespeed FileCachePath \/var\/ngx_pagespeed_cache;\r\n\r\nlocation ~ \"\\.pagespeed\\.([a-z]\\.)?[a-z]{2}\\.[^.]{10}\\.[^.]+\" {\r\n add_header \"\" \"\";\r\n}\r\nlocation ~ \"^\/pagespeed_static\/\" { }\r\nlocation ~ \"^\/ngx_pagespeed_beacon$\" { }\r\n\r\n# Minify CSS\r\n# pagespeed EnableFilters combine_css,rewrite_css;\r\n\r\n# Minify JS \r\n# pagespeed EnableFilters combine_javascript,rewrite_javascript;\r\n\r\n# Remove comments and whitespace from HTML \r\n# pagespeed EnableFilters remove_comments,collapse_whitespace; \r\n<\/pre>\n
include \/etc\/nginx\/pagespeed.conf;<\/pre>\n
server {\r\n\r\n listen 80;\r\n server_name your_website;\r\n root \/path\/to\/website;\r\n\t\r\n include \/etc\/nginx\/pagespeed.conf;\r\n # omitted code\r\n}\r\n<\/pre>\n
[user]$ sudo nginx -t\r\n[user]$ sudo service nginx restart<\/pre>\n
\n