Nginx:Installasi PageSpeed
Install the necessary dependencies
The first thing we’ll do is install a few dependencies. Log into your server and issue the commands:
<syntaxhighlight lang="shell"> sudo apt-get update </syntaxhighlight>
<syntaxhighlight lang="shell"> sudo apt-get install libssl-dev libxslt-dev libgd-dev curl nano gnupg2 ca-certificates lsb-release git -y </syntaxhighlight>
Download the ngx_pagespeed source
Change into the src directory with the command:
<syntaxhighlight lang="shell"> cd /usr/local/src </syntaxhighlight>
Clone the necessary source code:
<syntaxhighlight lang="shell"> sudo git clone https://github.com/apache/incubator-pagespeed-ngx.git </syntaxhighlight>
Change into the newly created directory:
<syntaxhighlight lang="shell"> cd incubator-pagespeed-ngx/ </syntaxhighlight>
Change to the latest stable branch:
<syntaxhighlight lang="shell"> sudo git checkout latest-stable </syntaxhighlight>
Locate the download URL with:
<syntaxhighlight lang="shell"> cat PSOL_BINARY_URL </syntaxhighlight>
Use that URL to download the PSOL with a command like this:
<syntaxhighlight lang="shell"> sudo wget https://dl.google.com/dl/page-speed/psol/1.13.35.2-x64.tar.gz </syntaxhighlight>
Extract the new file with:
<syntaxhighlight lang="shell"> sudo tar xvf 1.13.35.2-x64.tar.gz </syntaxhighlight>
Make sure you know which version of NGINX we’re using with:
<syntaxhighlight lang="shell"> ls /usr/local/src/nginx </syntaxhighlight>
You’ll use the most recent version number (in my case, it’s 1.21.4). Change into that directory with:
<syntaxhighlight lang="shell"> cd /usr/local/src/nginx/nginx-1.21.4 </syntaxhighlight>
Build the necessary dependencies with the following commands:
<syntaxhighlight lang="shell"> sudo apt build-dep nginx </syntaxhighlight>
<syntaxhighlight lang="shell"> sudo apt install uuid-dev </syntaxhighlight>
When those commands finish, configure the environment with:
<syntaxhighlight lang="shell"> sudo ./configure --with-compat --add-dynamic-module=/usr/local/src/incubator-pagespeed-ngx </syntaxhighlight>
Build the PageSpeed module:
<syntaxhighlight lang="shell"> sudo make modules </syntaxhighlight>
Copy the modules to the necessary directory with:
<syntaxhighlight lang="shell"> sudo cp objs/ngx_pagespeed.so /usr/share/nginx/modules/ </syntaxhighlight>
How to load the PageSpeed module Open the NGINX configuration file with:
<syntaxhighlight lang="shell"> sudo vim /etc/nginx/nginx.conf </syntaxhighlight>
Add the following line to the top of the file:
<syntaxhighlight lang="nginx"> load_module modules/ngx_pagespeed.so; </syntaxhighlight>
Save and close the file. Reload NGINX with:
<syntaxhighlight lang="shell"> sudo systemctl reload nginx </syntaxhighlight>
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:
<syntaxhighlight lang="shell"> sudo mkdir -p /var/ngx_pagespeed_cache </syntaxhighlight>
<syntaxhighlight lang="shell"> sudo chown -R www-data:www-data /var/ngx_pagespeed_cache </syntaxhighlight>
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:
<syntaxhighlight lang="shell"> sudo nano /etc/nginx/sites-available/default </syntaxhighlight>
At the bottom of the server section, add the following:
<syntaxhighlight lang="nginx" line="1">
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$" { }
</syntaxhighlight>
So that entire server block looks like this:
<syntaxhighlight lang="nginx" line="1">
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;
} </syntaxhighlight>
Save and close the file.
Reload NGINX with:
<syntaxhighlight lang="shell"> sudo systemctl reload nginx </syntaxhighlight>