Jump to content

Nginx:Installasi PageSpeed

From Wiki

Install the necessary dependencies

The first thing we’ll do is install a few dependencies. Log into your server and issue the commands:

sudo apt-get update
sudo apt-get install libssl-dev libxslt-dev libgd-dev curl nano gnupg2 ca-certificates lsb-release git -y

Download the ngx_pagespeed source

Change into the src directory with the command:

cd /usr/local/src

Clone the necessary source code:

sudo git clone https://github.com/apache/incubator-pagespeed-ngx.git

Change into the newly created directory:

cd incubator-pagespeed-ngx/

Change to the latest stable branch:

sudo git checkout latest-stable

Locate the download URL with:

cat PSOL_BINARY_URL

Use that URL to download the PSOL with a command like this:

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

Extract the new file with:

sudo tar xvf 1.13.35.2-x64.tar.gz

Make sure you know which version of NGINX we’re using with:

ls /usr/local/src/nginx

You’ll use the most recent version number (in my case, it’s 1.21.4). Change into that directory with:

cd /usr/local/src/nginx/nginx-1.21.4

Build the necessary dependencies with the following commands:

sudo apt build-dep nginx
sudo apt install uuid-dev

When those commands finish, configure the environment with:

sudo ./configure --with-compat --add-dynamic-module=/usr/local/src/incubator-pagespeed-ngx

Build the PageSpeed module:

sudo make modules

Copy the modules to the necessary directory with:

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

How to load the PageSpeed module Open the NGINX configuration file with:

sudo vim /etc/nginx/nginx.conf

Add the following line to the top of the file:

load_module modules/ngx_pagespeed.so;

Save and close the file. Reload NGINX with:

sudo systemctl reload nginx

How to configure the PageSpeed filters First, create the folder PageSpeed will use for its cache and give it the proper permission with the following commands:

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

I’m now going to enable PageSpeed for the default NGINX site. You’ll want to do this on the configuration file for the page/site you’re serving up with NGINX. For my demonstration, I’ll open the default file with:

sudo nano /etc/nginx/sites-available/default

At the bottom of the server section, add the following:

            pagespeed on;

            pagespeed FileCachePath /var/ngx_pagespeed_cache;

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

              add_header "" "";

            }

            location ~ "^/pagespeed_static/" { }

            location ~ "^/ngx_pagespeed_beacon$" { }

So that entire server block looks like this:

 server {

            listen 80 default_server;

            listen [::]:80 default_server;

            # SSL configuration

            #

            # listen 443 ssl default_server;

            # listen [::]:443 ssl default_server;

            #

            # Note: You should disable gzip for SSL traffic.

            # See: https://bugs.debian.org/773332

            #

            # Read up on ssl_ciphers to ensure a secure configuration.

            # See: https://bugs.debian.org/765782

            #

            # Self signed certs generated by the ssl-cert package

            # Don't use them in a production server!

            #

            # include snippets/snakeoil.conf;

            root /var/www/html;

            # Add index.php to the list if you are using PHP

            index index.html index.htm index.nginx-debian.html;

            server_name _;

            location / {

                    # First attempt to serve request as file, then

                    # as directory, then fall back to displaying a 404.

                    try_files $uri $uri/ =404;

            }

            # pass PHP scripts to FastCGI server

            #

            #location ~ \.php$ {

            #           include snippets/fastcgi-php.conf;

            #

            #           # With php-fpm (or other unix sockets):

            #           fastcgi_pass unix:/run/php/php7.4-fpm.sock;

            #           # With php-cgi (or other tcp sockets):

            #           fastcgi_pass 127.0.0.1:9000;

            #}

            # deny access to .htaccess files, if Apache's document root

            # concurs with nginx's one

            #

            #location ~ /\.ht {

            #           deny all;

            #}

            pagespeed on;

            pagespeed FileCachePath /var/ngx_pagespeed_cache;

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

              add_header "" "";

            }

            location ~ "^/pagespeed_static/" { }

            location ~ "^/ngx_pagespeed_beacon$" { }

            pagespeed RewriteLevel CoreFilters;
 
}

Save and close the file.

Reload NGINX with:

sudo systemctl reload nginx

Source