一、自动安装
使用最新稳定版本的ngx_pagespeed自动安装依赖项并构建最新的主线版nginx,请运行:
$ sudo bash <(curl -f -L -sS https://ngxpagespeed.com/install) --nginx-version latest
二、手动安装
1.安装依赖
$ sudo apt-get install build-essential zlib1g-dev libpcre3 libpcre3-dev unzip uuid-dev libxslt-dev libgd-dev libgeoip-dev openssl libssl-dev libperl-dev
2.下载ngx_pagespeed
$ NPS_VERSION=1.13.35.2-stable #版本号可以自选,官方提供的版本(https://www.modpagespeed.com/doc/release_notes) $ cd $ wget https://github.com/apache/incubator-pagespeed-ngx/archive/v${NPS_VERSION}.zip $ unzip v${NPS_VERSION}.zip $ nps_dir=$(find . -name "*pagespeed-ngx-${NPS_VERSION}" -type d) $ cd "$nps_dir" $ NPS_RELEASE_NUMBER=${NPS_VERSION/beta/} $ NPS_RELEASE_NUMBER=${NPS_VERSION/stable/} $ psol_url=https://dl.google.com/dl/page-speed/psol/${NPS_RELEASE_NUMBER}.tar.gz $ [ -e scripts/format_binary_url.sh ] && psol_url=$(scripts/format_binary_url.sh PSOL_BINARY_URL) $ wget ${psol_url} $ tar -xzvf $(basename ${psol_url})
3. 下载nginx
$ NGINX_VERSION=1.14.0 #如果是在已安装nginx基础之上build,nginx版本需要和当前使用版本一致 $ cd $ wget http://nginx.org/download/nginx-${NGINX_VERSION}.tar.gz $ tar -xvzf nginx-${NGINX_VERSION}.tar.gz $ cd nginx-${NGINX_VERSION}/ $ NGINX_BIN=‘/usr/sbin/nginx‘ $ NGINX_BUILD_CONF=`$NGINX_BIN -V 2>&1 >/dev/null | grep ‘configure‘ --color | awk -F‘:‘ ‘{print $2;}‘` $ NGINX_BUILD_CONF="$NGINX_BUILD_CONF --add-dynamic-module=/home/ubuntu/incubator-pagespeed-ngx-1.13.35.2-stable"
如果本机已安装了nginx,在已有的基础上安装,则需要执行以下操作:
$ vi configure #默认prefix路径是/usr/local/nginx,可以修改为自己当前的nginx路径再编译,否则无法匹配so文件
通过nginx -V 找到configure的参数, 然后在末尾加上--add-dynamic-module=/home/ubuntu/incubator-pagespeed-ngx-1.13.35.2-stable开始编译
直接使用$NGINX_BUILD_CONF 变量会有问题,可以先通过nginx -V命令查看当前的编译变量,以下是自己使用的编译变量,请根据自己需要修改:
./configure --with-cc-opt=‘-g -O2 -fdebug-prefix-map=/build/nginx-FIJPpj/nginx-1.14.0=. -fstack-protector-strong -Wformat -Werror=format-security -fPIC -Wdate-time -D_FORTIFY_SOURCE=2‘ --with-ld-opt=‘-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -fPIC‘ --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --modules-path=/usr/lib/nginx/modules --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-pcre-jit --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_v2_module --with-http_dav_module --with-http_slice_module --with-threads --with-http_addition_module --with-http_geoip_module=dynamic --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_sub_module --with-http_xslt_module=dynamic --with-stream=dynamic --with-stream_ssl_module --with-mail=dynamic --with-mail_ssl_module --add-dynamic-module=/home/ubuntu/incubator-pagespeed-ngx-1.13.35.2-stable
初始安装:
$ ./configure --add-dynamic-module=/home/ubuntu/incubator-pagespeed-ngx-1.13.35.2-stable
configure执行完毕之后:
$ make
$ sudo make install
3.配置nginx
如果是初始安装nginx,则找到nginx安装路径(默认是在/usr/local/nginx)执行配置pagespeed即可。
如果存在nginx先备份原来的nginx执行文件以便恢复:
$ sudo cp /usr/sbin/nginx /tmp/nginx_bak # 备份旧的nginx文件
4.配置pagespeed
添加以下配置文件到/etc/nginx/nginx.conf文件中
load_module /usr/lib/nginx/modules/ngx_pagespeed.so; http{ ...... pagespeed on; pagespeed FileCachePath /var/cache/ngx_pagespeed/; ...... include /etc/nginx/ngx_pagespeed.conf; }
创建pagespeed缓存目录
$ sudo mkdir /var/cache/ngx_pagespeed/
$ sudo chown root:root /var/cache/ngx_pagespeed/
创建pagespeed配置文件
$ sudo vi /etc/nginx/ngx_pagespeed.conf
#根据自己业务需要配置相关特性 pagespeed RewriteLevel PassThrough; pagespeed FetchHttps enable; pagespeed EnableFilters prioritize_critical_css; pagespeed EnableFilters combine_javascript; pagespeed EnableFilters combine_css; pagespeed EnableFilters flatten_css_imports; pagespeed EnableFilters insert_dns_prefetch; pagespeed EnableFilters convert_jpeg_to_webp; pagespeed EnableFilters convert_to_webp_lossless; pagespeed EnableFilters convert_to_webp_animated; pagespeed EnableFilters collapse_whitespace; pagespeed EnableFilters rewrite_images; pagespeed EnableFilters resize_rendered_image_dimensions; pagespeed EnableFilters rewrite_javascript; pagespeed EnableFilters move_css_to_head; pagespeed EnableFilters defer_javascript; pagespeed EnableFilters inline_images; pagespeed EnableFilters recompress_images; pagespeed EnableFilters rewrite_domains; pagespeed DomainRewriteHyperlinks on;
将新生成的nginx文件拷贝到执行目录
$ cd /usr/share/nginx/sbin/ #初始化安装不必执行此条命令
$ sudo cp -f nginx /usr/sbin/nginx #初始化安装不必执行此条命令
$ sudo nginx -t
$ sudo nginx -s reload
原文地址:https://www.cnblogs.com/alamisu/p/10734857.html