Cái đặt Google PageSpeed module NGINX trên Ubuntu 20.04
Cài đặt Google PageSpeed module NGINX trên Ubuntu 20.04 16

Mod_pagespeed là một module Máy chủ Nginx (hoặc Apache) mã nguồn mở, tự động áp dụng các bộ lọc đã chọn cho các trang và nội dung liên quan như biểu định kiểu, tệp JavaScript và HTML, cũng như hình ảnh và các yêu cầu về bộ nhớ cache của trang web.

Một ưu điểm chính của module này là nó không yêu cầu sửa đổi nội dung hoặc quy trình làm việc hiện có, có nghĩa là tất cả các tối ưu hóa nội bộ và thay đổi đối với tệp được thực hiện ở phía máy chủ, hiển thị các tệp đã sửa đổi trực tiếp cho người dùng truy cập website.

Và trong bài viết này mình sẽ hướng dẫn bạn Thiết lập Google PageSpeed module NGINX trên Ubuntu 20.04.

Điều kiện cài đặt

  • Máy chủ sử dụng Ubuntu
  • Đã cài đặt Nginx, hoặc bạn có thể cài đặt LEMP theo hướng dẫn của mình.

Bước 1: Cập nhật hệ thống

apt-get update -y

Cài đặt một số Package cần thiết

apt-get install dpkg-dev build-essential zlib1g-dev libpcre3 git libpcre3-dev unzip -y

Bước 2: Tải và Compile ngx_pagespeed

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

Sau khi tải xong bạn thực hiện giải nén với lệnh sau

tar -xvzf nginx-1.18.0.tar.gz

Tải source ngx_pagespeed

git clone https://github.com/apache/incubator-pagespeed-ngx.git
cd incubator-pagespeed-ngx
git checkout latest-stable

Output

Note: switching to 'latest-stable'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:

  git switch -c 

Or undo this operation with:

  git switch -

Turn off this advice by setting config variable advice.detachedHead to false

HEAD is now at 11ba8ea54 Update PSOL_BINARY_URL

Từ thông báo trên, bạn hãy đọc file PSOL_BINARY_URL để xác định URL tải xuống.

cat PSOL_BINARY_URL

Bạn sẽ nhận được thông báo như sau

https://dl.google.com/dl/page-speed/psol/1.13.35.2-$BIT_SIZE_NAME.tar.gz

Bây giờ bạn hãy chạy lệnh sau để tải PSOL về

wget https://dl.google.com/dl/page-speed/psol/1.13.35.2-x64.tar.gz

Thực hiện giải nén file để sử dụng

tar -xvzf 1.13.35.2-x64.tar.gz

Tiếp theo bạn truy cập vào thư mục nginx đã tải và cài đặt các thành phần phụ sau

cd /root/nginx-1.18.0
apt-get build-dep nginx
apt-get install uuid-dev

compile ngx_pagespeed với lệnh sau

./configure --with-compat --add-dynamic-module=/root/incubator-pagespeed-ngx
Cái đặt Google PageSpeed module NGINX trên Ubuntu 20.04
Cài đặt Google PageSpeed module NGINX trên Ubuntu 20.04 17

Output

Configuration summary
  + using system PCRE library
  + OpenSSL library is not used
  + using system zlib library

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx modules path: "/usr/local/nginx/modules"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

Tiếp theo, chạy lệnh sau để xây dựng module Pagespeed:

make modules
Cái đặt Google PageSpeed module NGINX trên Ubuntu 20.04
Cài đặt Google PageSpeed module NGINX trên Ubuntu 20.04 18

Copy ngx_pagespeed vào thư mục modules để dùng

cp objs/ngx_pagespeed.so /usr/share/nginx/modules/

Bước 3: Thêm Module ngx_pagespeed vào file cấu hình Nginx

Tiếp theo, bạn sẽ cần định cấu hình Nginx để sử dụng modules ngx_pagespeed. Trước tiên, hãy chỉnh sửa tệp cấu hình chính của Nginx và xác định đường dẫn modules ngx_pagespeed:

vi /etc/nginx/nginx.conf

Sau đó bạn thêm đoạn này vào đầu file

load_module modules/ngx_pagespeed.so;
Cái đặt Google PageSpeed module NGINX trên Ubuntu 20.04
Cài đặt Google PageSpeed module NGINX trên Ubuntu 20.04 19

Tạo thư mục pagespeed caches và phân quyền cho thư mục

mkdir -p /var/ngx_pagespeed_cache
chown -R www-data:www-data /var/ngx_pagespeed_cache

Tiếp theo bạn mở file cấu hình vhost của domain và thêm vào như sau. Tuỳ vào vhost mà bạn tạo hãy mở đúng file nhé.

vi /etc/nginx/sites-available/my.domain

Cách 1: Thêm trực tiếp các dòng sau vào bên trong block server {

##Google pagespeed
pagespeed on;
     pagespeed FileCachePath "/var/ngx_pagespeed_cache/";
     pagespeed RewriteLevel OptimizeForBandwidth;

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

     location ~ "^/pagespeed_static/" { }
     location ~ "^/ngx_pagespeed_beacon$" { }
pagespeed RewriteLevel CoreFilters;
##Google pagespeed
Cái đặt Google PageSpeed module NGINX trên Ubuntu 20.04
Cài đặt Google PageSpeed module NGINX trên Ubuntu 20.04 20

Sau khi sửa xong. Bạn hãy kiểm tra file cấu hình nginx xem có bị lỗi không bằng lệnh nginx -t. Nếu không gặp lỗi sẽ hiện như sau

root@sv:~/nginx-1.18.0# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Cách 2: Tạo file cấu hình ngx_pagespeed.conf và include vào vhost

Tạo file ngx_pagespeed.conf và dán các cấu hình vào. Cấu hình mẫu bạn xem tại đây.

vi /etc/nginx/ngx_pagespeed.conf

Sau đó bạn mở file /etc/nginx/sites-available/my.domain và include vào

include /etc/nginx/ngx_pagespeed.conf;

Khởi động lại dịch vụ Nginx

systemctl restart nginx

Verify the ngx_pagespeed Module

Ngay bây giờ bạn hãy kiểm tra ngx_pagespeed với lệnh curl như sau

curl -I -p http://example.com

Nếu bạn nhận được kết quả trả về x-page-speed: 1.13.35.2-0 có nghĩa website bạn đã nhận và đã được tối ưu hoá bởi ngx_pagespeed

Cái đặt Google PageSpeed module NGINX trên Ubuntu 20.04
Cài đặt Google PageSpeed module NGINX trên Ubuntu 20.04 21

Ngoài ra bạn có thể kiểm tra header trực tiếp ở trình duyệt

Cái đặt Google PageSpeed module NGINX trên Ubuntu 20.04
Cài đặt Google PageSpeed module NGINX trên Ubuntu 20.04 22

Với các bước trên bạn đã Thiết lập Google PageSpeed module NGINX trên Ubuntu 20.04 hoàn tất rồi đó. Chúc các bạn thực hiện thành công.