Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save rodrigopasc/17be25de26e7848f8981fc960d3210b6 to your computer and use it in GitHub Desktop.
Save rodrigopasc/17be25de26e7848f8981fc960d3210b6 to your computer and use it in GitHub Desktop.
How to setup nginx with Passenger and PageSpeed modules on Ubuntu [EN]

Setup nginx with Passenger and PageSpeed modules

English | Portuguese

Preparing

Core libraries

You need to make sure these libraries are installed:

$ sudo apt-get -y update
$ sudo apt-get install -y build-essential libcurl4-openssl-dev unzip software-properties-common git libpcre3-dev libpcre3 uuid-dev libssh2-1-dev
$ cd && mkdir sources && cd sources
$ wget https://downloads.sourceforge.net/project/pcre/pcre/8.41/pcre-8.41.tar.gz && tar xzvf pcre-8.41.tar.gz
$ wget http://www.zlib.net/zlib-1.2.11.tar.gz && tar xzvf zlib-1.2.11.tar.gz
$ wget https://www.openssl.org/source/openssl-1.1.0f.tar.gz && tar xzvf openssl-1.1.0f.tar.gz

You are going to have to install MaxMind DB as well.

$ sudo add-apt-repository ppa:maxmind/ppa
$ sudo apt update
$ sudo apt install libmaxminddb0 libmaxminddb-dev mmdb-bin

RVM

It is time for setting up Ruby Version Manager.

$ sudo apt-add-repository -y ppa:rael-gc/rvm
$ sudo apt-get update
$ sudo apt-get install -y rvm
$ source /etc/profile.d/rvm.sh

Passenger

Install Phusion Passenger gem.

gem install passenger

Modules

Directory

Create the modules directory so we shall be able to clone them.

$ cd ~/ && mkdir modules && cd modules

Sources

Clone them in modules directory created before.

$ git clone https://github.com/bpaquet/ngx_http_enhanced_memcached_module
$ git clone https://github.com/leev/ngx_http_geoip2_module

PageSpeed

Run the commands down below inside modules directory. Go to the offical website (https://www.modpagespeed.com/doc/build_ngx_pagespeed_from_source) for checking its latest version. (03/01/19 is 1.13.35.1-beta, so change it for the new one)

$ NPS_VERSION=1.12.34.3-stable
$ wget https://github.com/apache/incubator-pagespeed-ngx/archive/v${NPS_VERSION}.zip
$ unzip v${NPS_VERSION}.zip
$ nps_dir=$(find . -name "*pagespeed-ngx-${NPS_VERSION}" -type d)
$ cd "$nps_dir"
$ NPS_RELEASE_NUMBER=${NPS_VERSION/beta/}
$ NPS_RELEASE_NUMBER=${NPS_VERSION/stable/}
$ psol_url=https://dl.google.com/dl/page-speed/psol/${NPS_RELEASE_NUMBER}.tar.gz [ -e scripts/format_binary_url.sh ] && psol_url=$(scripts/format_binary_url.sh PSOL_BINARY_URL)
$ wget ${psol_url}
$ tar -xzvf $(basename ${psol_url})

nginx

Download

Go to the offical website (http://nginx.org/) for checking its latest version. (03/01/19 is 1.15.18, so change it for the new one)

$ wget http://nginx.org/download/nginx-1.15.8.tar.gz
$ tar xvzf nginx-1.15.8.tar.gz

Installation

We are going to compile PageSpeed by its Phusion Passenger setup. Type and run the following command:

sudo passenger-install-nginx-module

Follow the steps, but pick the option to customize and add the following settings:

--prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/run/nginx.pid --lock-path=/var/lock/nginx.lock --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_slice_module --with-http_ssl_module --with-http_sub_module --with-http_stub_status_module --with-http_v2_module --with-http_secure_link_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-debug --with-openssl=../sources/openssl-1.1.0f --with-openssl-opt=enable-ec_nistp_64_gcc_128 --with-openssl-opt=no-nextprotoneg --with-openssl-opt=no-weak-ssl-ciphers --with-openssl-opt=no-ssl3 --with-zlib=../sources/zlib-1.2.11 --with-pcre=../sources/pcre-8.41 --add-module=../modules/ngx_http_enhanced_memcached_module --add-module=../modules/ngx_http_geoip2_module --add-module=../modules/incubator-pagespeed-ngx-1.12.34.3-stable

Wait till the process is done and everything should be ready. 🎉🎊

Turn on PageSpeed

In your nginx.conf add in html or specifically inside a server block:

pagespeed on;
pagespeed FileCachePath /tmp/ngx_pagespeed_cache;
pagespeed LowercaseHtmlNames on;
pagespeed XHeaderValue "Powered By ngx_pagespeed";

Now for each server block:

location ~ "\.pagespeed\.([a-z]\.)?[a-z]{2}\.[^.]{10}\.[^.]+" {
  add_header "" "";
}

location ~ "^/pagespeed_static/" { }
location ~ "^/ngx_pagespeed_beacon$" { }

Finally run sudo service nginx restart and you are ready to rock.

If you want to check if everything is working:

nginx: $ service nginx status
Passenger: $ sudo /usr/sbin/passenger-memory-stats
PageSpeed: $ curl -I yourip and check if it is showing X-Page-Speed.

Enjoy your brand new fast app.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment