安装Pagespeed模块
有了这些信息, 开始使用nginx编译Pagespeed
bash <(curl -f -L -sS https://ngxpagespeed.com/install) --nginx-version 1.20.0
从主目录运行命令时,它将下载主文件夹中的Pagespeed软件包。
对于本教程,该软件包在My/Home/jack文件夹中下载了名为incubator-pagespeed-ngx-latest-stable的文件
/home/jack/incubator-pagespeed-ngx-latest-stable
在构建过程中,系统将提示我们为nginx配置其他参数。
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. >
在构建过程中包含该文件夹:
--add-dynamic-module=/home/jack/incubator-pagespeed-ngx-latest-stable
现在,在构建过程中提示时,将上面的路径和nginx原始模块参数输出组合起来。
整行看起来是这样的:
--add-dynamic-module=/home/jack/incubator-pagespeed-ngx-latest-stable --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/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-g -O2 -fdebug-prefix-map=/data/builder/debuild/nginx-1.20.0/debian/debuild-base/nginx-1.20.0=. -fstack-protector-strong -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fPIC' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -Wl,--as-needed -pie'
当再次提示时,选择Y为YES。
You have set --with-debug for building nginx, but precompiled Debug binaries for PSOL, which ngx_pagespeed depends on, aren't available. If you're trying to debug PSOL you need to build it from source. If you just want to run nginx with debug-level logging you can use the Release binaries. Use the available Release binaries? [Y/n] Y
构建完成后,NGINX应包含Pagespeed模块。
我们可以通过运行以下命令来测试:
nginx -V
输出示例:
nginx version: nginx/1.20.0 built by gcc 9.3.0 (Ubuntu 9.3.0-17ubuntu1~20.04) built with OpenSSL 1.1.1f 31 Mar 2016 TLS SNI support enabled configure arguments: --add-module=/home/jack/incubator-pagespeed-ngx-latest-stable --add-dynamic-module=/home/jack/incubator-pagespeed-ngx-latest-stable --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules
接下来,启用Pagespeed模块,并为nginx模块创建符号链接。
sudo cp ~/nginx-1.20.0/objs/*.so /etc/nginx/modules sudo ln -s /usr/lib/nginx/modules /etc/nginx/modules
在启用PagePeed的每个服务器块中,添加:
server { . 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$" { } . }
从文件中保存退出并重新加载nginx
sudo systemctl reload nginx
测试Pagespeed设置。
curl -I -p http://example.com
我们应该看到类似于下面的输出
HTTP/1.1 200 OK Server: nginx/1.20.0 Date: Sat, 15 Jan 2021 14:58:16 GMT Content-Type: text/html Content-Length: 612 Last-Modified: Tue, 20 Apr 2021 13:35:47 GMT X-Page-Speed: 1.13.35.2-0 Cache-Control: max-age=0, no-cache
PageSpeed模块是一个开源的、Apache支持的、由Google创建的模块,通过重写网页来提高延迟和减少带宽,从而帮助提高网页加载速度。
安装所需的软件包
在Ubuntu中编译和构建PageSpeed模块需要这些包。运行以下命令以安装这些软件包:
sudo apt update sudo apt install libssl-dev libxslt-dev libgd-dev curl nano
安装nginx http服务器
在Ubuntu中安装nginx非常简单。
ubuntu默认存储库中有nginx稳定包。
只需运行以下命令即可安装nginx。
sudo apt install nginx
因为一些原因,在我安装的Nginx中构建PageSpeed模块时,进行得不是很顺利。
所以升级它。
安装支持的软件包。
sudo apt install gnupg2 ca-certificates lsb-release
然后在Ubuntu中创建存储库文件。
echo "deb http://nginx.org/packages/ubuntu lsb_release -cs nginx" | sudo tee/etc/apt/sources.list.d/nginx.list
添加存储库密钥:
curl -fsSL https://nginx.org/keys/nginx_signing.key | sudo apt-key add
之后,通过运行以下命令升级:
sudo apt update sudo apt install nginx
查看 nginx版本号
nginx -v
输出示例:
nginx version: nginx/1.20.0
查看 nginx当前安装模块和参数
nginx -V
输出示例:
--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/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-g -O2 -fdebug-prefix-map=/data/builder/debuild/nginx-1.20.0/debian/debuild-base/nginx-1.20.0=. -fstack-protector-strong -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fPIC' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -Wl,--as-needed -pie'