PageSpeed is one of the Nginx’s modules developed by Google to speed up the web site or web applications response time, it can automatically optimize the returned HTML to reduce the page load time. PageSpeed has the ability of image optimization using stripping meta-data, dynamic resizing and recompression.

Some of the main benefits of PageSpeed are listed below:

  • CSS & JavaScript minification, concatenation, inlining, and outlining.
  • Small resource inlining.
  • HTML rewriting.
  • Cache lifetime extension.

Install Dependencies

First of all, we need to install some dependencies because we are going to compile Nginx from the source, so execute the following command to install all of the needed dependencies:

yum install gcc cmake unzip wget gcc-c++ pcre-devel zlib-devel

Download and compile Nginx with Ngx_pagespeed

To install Nginx with “pagespeed” module you need to compile Nginx from the source but first, you have to download it using the following command:

wget http://nginx.org/download/nginx-1.12.0.tar.gz

Then download the Ngx_pagespeed source as well:

wget https://github.com/pagespeed/ngx_pagespeed/archive/v1.12.34.2-stable.zip

Now that both needed sources are downloaded, extract them with the commands below:

tar xvzf nginx-1.12.0.tar.gz
unzip v1.12.34.2-stable.zip

You will also need to download the PageSpeed Optimization Libraries to compile Nginx, execute the commands below one by one to get it download and extract it in the preferred route:

cd ngx_pagespeed-1.12.34.2-stable
wget https://dl.google.com/dl/page-speed/psol/1.12.34.2-x64.tar.gz
tar -xvzf 1.12.34.2-x64.tar.gz

Now we can start compiling Nginx with Ngx_pagespeed module.

First switch to the main directory of the Nginx’s source:

cd ~/nginx-1.12.0

Run the following command to start compiling it:

./configure --add-module=$HOME/ngx_pagespeed-1.12.34.2-stable --user=nobody --group=nobody --pid-path=/var/run/nginx.pid

Once the configuration is completed, compile the Nginx by the following command:

make && make install

The process will take a few minutes.

When the installation process is finished you have to create Nginx symlinks:

ln -s /usr/local/nginx/conf/ /etc/nginx
ln -s /usr/local/nginx/sbin/nginx /usr/sbin/nginx

Create Nginx Systemd service

You will need to create a systemd service for Nginx in order to “start, stop and make your service run as startup.

Create a file named “nginx.service” with your text editor in the following path:

nano /lib/systemd/system/nginx.service

Paste the following lines in the file then save and exit:

[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

Run the following command to take effect:

systemctl daemon-reload

Now you can start your Nginx and make it run as a startup service:

systemctl start nginx
systemctl enable nginx

Configure Nginx

Now, Nginx is installed and has the PageSpeed support, but the module is disabled by default but before enabling it we need to create a cache directory for PageSpeed and also assign ownership for it to Nginx.

mkdir -p /var/ngx_pagespeed_cache
chown -R nobody:nobody /var/ngx_pagespeed_cache

Next, you need to add some lines in the Nginx configuration. Open the “nginx.conf” with your text editor using the command below:

nano /etc/nginx/nginx.conf

Add the following lines to the “server” directive:

# Pagespeed main settings

pagespeed on;
pagespeed FileCachePath /var/ngx_pagespeed_cache;

# Ensure requests for pagespeed optimized resources go to the pagespeed
# handler and no extraneous headers get set.

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

Test your configuration with the command below:

nginx -t

You should see the following output:

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

Restart Nginx service to take effect:

systemctl restart nginx

Test if PageSpeed is working

You can easily test your PageSpeed module using “curl” command:

curl -I -p localhost

You should see the red line in your output:

HTTP/1.1 200 OK
Server: nginx/1.12.0
Content-Type: text/html
Connection: keep-alive
Vary: Accept-Encoding
Date: Sat, 22 Jul 2017 11:17:53 GMT
X-Page-Speed: 1.12.34.2-0
Cache-Control: max-age=0, no-cache
Was this answer helpful? 0 Users Found This Useful (5 Votes)