Nginx:Installasi PageSpeed: Difference between revisions
| (One intermediate revision by the same user not shown) | |||
| Line 2: | Line 2: | ||
The first thing we’ll do is install a few dependencies. Log into your server and issue the commands: | 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=== | ===Download the ngx_pagespeed source=== | ||
Change into the src directory with the command: | Change into the src directory with the command: | ||
<syntaxhighlight lang="shell"> | |||
cd /usr/local/src | |||
</syntaxhighlight> | |||
Clone the necessary source code: | 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: | Change into the newly created directory: | ||
<syntaxhighlight lang="shell"> | |||
cd incubator-pagespeed-ngx/ | |||
</syntaxhighlight> | |||
Change to the latest stable branch: | Change to the latest stable branch: | ||
<syntaxhighlight lang="shell"> | |||
sudo git checkout latest-stable | |||
</syntaxhighlight> | |||
Locate the download URL with: | 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: | 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: | 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: | 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: | 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: | 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: | 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: | Build the PageSpeed module: | ||
<syntaxhighlight lang="shell"> | |||
sudo make modules | |||
</syntaxhighlight> | |||
Copy the modules to the necessary directory with: | 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 | How to load the PageSpeed module | ||
Open the NGINX configuration file with: | 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: | 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: | Save and close the file. Reload NGINX with: | ||
<syntaxhighlight lang="shell"> | |||
sudo systemctl reload nginx | |||
</syntaxhighlight> | |||
How to configure the PageSpeed filters | 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: | 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: | 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: | 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: | So that entire server block looks like this: | ||
<syntaxhighlight lang="nginx" line="1"> | |||
server { | 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; | pagespeed RewriteLevel CoreFilters; | ||
} | |||
</syntaxhighlight> | |||
Save and close the file. | Save and close the file. | ||
| Line 211: | Line 259: | ||
Reload NGINX with: | Reload NGINX with: | ||
<syntaxhighlight lang="shell"> | |||
sudo systemctl reload nginx | |||
</syntaxhighlight> | |||
==Source== | ==Source== | ||
Latest revision as of 12:16, 22 September 2022
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