LinuxTutorialsUbuntu

How To Install Nginx With Google PageSpeed Module on Ubuntu 20.04 LTS

Install Nginx With Google PageSpeed Module on Ubuntu 20.04

In this tutorial, we will show you how to install Nginx With Google PageSpeed Module on Ubuntu 20.04 LTS. For those of you who didn’t know, Google PageSpeed is an open-source Apache module created by Google to help Make the Web Faster by rewriting web pages to reduce latency and bandwidth. The loading speed of website pages affects the user experience and the position of the site in search engine results. To analyze and speed up the loading of content, there is a Google PageSpeed module.

This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo‘ to the commands to get root privileges. I will show you the step-by-step installation of Nginx With the Google PageSpeed Module on Ubuntu 20.04 (Focal Fossa). You can follow the same instructions for Ubuntu 18.04, 16.04, and any other Debian-based distribution like Linux Mint.

Prerequisites

  • A server running one of the following operating systems: Ubuntu 20.04, 18.04, 16.04, and any other Debian-based distribution like Linux Mint.
  • It’s recommended that you use a fresh OS install to prevent any potential issues.
  • SSH access to the server (or just open Terminal if you’re on a desktop).
  • A non-root sudo user or access to the root user. We recommend acting as a non-root sudo user, however, as you can harm your system if you’re not careful when acting as the root.

Install Nginx With Google PageSpeed Module on Ubuntu 20.04 LTS Focal Fossa

Step 1. First, make sure that all your system packages are up-to-date by running the following apt commands in the terminal.

sudo apt update
sudo apt upgrade
sudo apt install curl libssl-dev

Step 2. Installing Nginx on Ubuntu 20.04.

You can install Nginx using the following command:

sudo apt install nginx

Once Nginx is installed you can verify the version:

nginx -V

Output:

nginx version: nginx/1.18.0 (Ubuntu)
built with OpenSSL 1.1.1f  20 May 2021
TLS SNI support enabled
configure arguments:
...

Step 3. Installing the Google PageSpeed module.

Now we run the following command to install all dependencies requires and the PageSpeed module:

bash <(curl -f -L -sS https://ngxpagespeed.com/install) \
--nginx-version 1.18.0

During installation, all questions should be answered positively. After the next request, you can enter what other modules need to be included in the assembly:

About to build nginx. Do you have any additional ./configure
arguments you would like to set? For example, if you would like
to build nginx with https support give --with-http_ssl_module
If you don't have any, just press enter.
>

The default set of parameters is shown below:

--prefix=/etc/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=/var/run/nginx.pid --lock-path=/var/run/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-stream=dynamic --with-stream_ssl_module --with-mail=dynamic --with-mail_ssl_module --with-http_ssl_module --with-http_v2_module

Once the installation is done, create the following symbolic link:

ln -s /usr/lib/nginx/modules /etc/nginx/modules

To verify that the installation:

nginx -V

Output:

...
configure arguments: --add-module=/root/incubator-pagespeed-ngx-latest-stable ...

Step 4. Setup Google PageSpeed Module.

Now we edit the Nginx main configuration file and define the enabling Google PageSpeed module path:

nano /etc/nginx/sites-available/default

Add the following file:

server {
…
pagespeed on;
pagespeed FileCachePath "/var/cache/ngx_pagespeed/";
pagespeed RewriteLevel OptimizeForBandwidth;
location ~ ".pagespeed.([a-z].)?[a-z]{2}.[^.]{10}.[^.]+" {
add_header "" "";
}
location ~ "^/pagespeed_static/" { }
location ~ "^/ngx_pagespeed_beacon$" { }
…
}

To apply the changes reload the systemd daemon:

nginx -t

Next, we create a new index.html file in the webroot directory:

echo "Hallo, Linux!" > /var/www/html/index.html

Finally, restart the Nginx service to apply the changes:

sudo systemctl restart nginx

Step 5. Test the Google PageSpeed Module.

The easiest way to make sure that the Google PageSpeed module is working is to access our website using curl:

curl -I -p http://your-server-IP or your-domain-name

Output:

HTTP/1.1 200 OK
Server: nginx/1.18.0
Content-Type: text/html
Connection: keep-alive
Date: Wed, 21 May 2021 16:36:08 GMT
X-Page-Speed: 1.13.35.2-0
Cache-Control: max-age=0, no-cache

Congratulations! You have successfully installed Nginx With Google PageSpeed. Thanks for using this tutorial for installing Nginx With Google PageSpeed Module on Ubuntu 20.04 LTS Focal Fossa system. For additional help or useful information, we recommend you check the official Google PageSpeed website.

VPS Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “VPS Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!

r00t

r00t is a seasoned Linux system administrator with a wealth of experience in the field. Known for his contributions to idroot.us, r00t has authored numerous tutorials and guides, helping users navigate the complexities of Linux systems. His expertise spans across various Linux distributions, including Ubuntu, CentOS, and Debian. r00t's work is characterized by his ability to simplify complex concepts, making Linux more accessible to users of all skill levels. His dedication to the Linux community and his commitment to sharing knowledge makes him a respected figure in the field.
Back to top button