宝塔NGINX配置ngx_pagespeed实现前端页面加速
ngx_pagespeed 是谷歌开发的一个Nginx扩展模块,可以对网页内大量冗余的CSS,JS自动进行合并压缩处理,减少不必要的请求,提升访问速度。宝塔则是目前国内比较火的一款傻瓜化的主机管理系统,目前国内很多新手小白从装这个面板开始入手学习。因此这里就结合这两者,教下大家如何实现在宝塔的NGINX基础上重新编译,以支持pagespeed
ngx_pagespeed官网: http://ngxpagespeed.com/
项目Github主页: https://github.com/pagespeed/ngx_pagespeed (详细配置方法,也可以看这里)
GoogleDevelopers:https://developers.google.com/speed/docs/mod_pagespeed
主要功能
*图像优化:剥离元数据、动态调整,重新压缩 *CSS和JavaScript压缩、合并、级联、内联 *小资源内联 *推迟图像和JavaScript加载 *对HTML重写、压缩空格、去除注释等 *提升缓存周期 *等等
网上找到一个一键安装脚本可以实现,保存成install.sh
然后依次执行
chmod +x install.sh
./install.sh
脚本代码如下:
#!/bin/bash Green_font="[32m" && Yellow_font="[33m" && Red_font="[31m" && Font_suffix="[0m" Info="${Green_font}[Info]${Font_suffix}" Error="${Red_font}[Error]${Font_suffix}" NGX_DIR=/www/server/nginx NPS_VESION=1.13.35.2-stable echo -e "${Green_font} ${Font_suffix}" download_ngx_pagespeed(){ cd ${NGX_DIR}/src wget https://github.com/apache/incubator-pagespeed-ngx/archive/v${NPS_VESION}.zip unzip v${NPS_VESION}.zip rm v${NPS_VESION}.zip NPS_DIR=$(find . -name "*pagespeed-ngx-${NPS_VESION}" -type d) mv $NPS_DIR ngx_pagespeed cd ngx_pagespeed NPS_RELEASE_NUMBER=${NPS_VESION/beta/} NPS_RELEASE_NUMBER=${NPS_VESION/stable/} PSPL_URL=https://dl.google.com/dl/page-speed/psol/${NPS_RELEASE_NUMBER}.tar.gz [ -e scripts/format_binary_url.sh ] PSPL_URL=$(scripts/format_binary_url.sh PSOL_BINARY_URL) wget ${PSPL_URL} tar -xzvf $(basename ${PSPL_URL}) rm $(basename ${PSPL_URL}) } install_ngx_pagespeed(){ cd ${NGX_DIR}/src NGX_CONF=`/usr/bin/nginx -V 2>&1 >/dev/null | grep 'configure' --color | awk -F':' '{print $2;}'` NGX_CONF="--add-module=${NGX_DIR}/src/ngx_pagespeed $NGX_CONF" ./configure $NGX_CONF make make install } check_system() { if grep -Eqii "CentOS" /etc/issue || grep -Eq "CentOS" /etc/*-release; then DISTRO='CentOS' PM='yum' elif grep -Eqi "Red Hat Enterprise Linux Server" /etc/issue || grep -Eq "Red Hat Enterprise Linux Server" /etc/*-release; then DISTRO='RHEL' PM='yum' elif grep -Eqi "Aliyun" /etc/issue || grep -Eq "Aliyun" /etc/*-release; then DISTRO='Aliyun' PM='yum' elif grep -Eqi "Debian" /etc/issue || grep -Eq "Debian" /etc/*-release; then DISTRO='Debian' PM='apt' elif grep -Eqi "Ubuntu" /etc/issue || grep -Eq "Ubuntu" /etc/*-release; then DISTRO='Ubuntu' PM='sudo' else DISTRO='unknow' fi } install_basic(){ case ${PM} in yum) yum -y install sudo yum -y update sudo yum -y install gcc-c++ pcre-devel zlib-devel make unzip libuuid-devel ;; apt) apt -y install sudo sudo apt -y update sudo apt-get -y install build-essential zlib1g-dev libpcre3 libpcre3-dev unzip uuid-dev ;; sudo) sudo apt -y update sudo apt-get -y install build-essential zlib1g-dev libpcre3 libpcre3-dev unzip uuid-dev ;; *) echo -e "${Error} 不支持您的系统 !" ;; esac echo -e "${Info} 模块依赖安装完成 !" } check_root(){ [[ "`id -u`" != "0" ]] && echo -e "${Error} 请先进入root账户 !" } check_gcc(){ gcc --version && echo -e "${Info} 请先确认gcc版本>=4.8! 输入任意按键来确认?" read aNum } restart_ngx(){ service nginx restart echo -e "${Info} 已重启Nginx!" } temp_swap_add(){ sudo dd if=/dev/zero of=/swapfile bs=64M count=16 sudo mkswap /swapfile sudo swapon /swapfile echo -e "${Info} 临时增加Swap以解决编译中内存不足崩溃!" } temp_swap_del(){ sudo swapoff /swapfile sudo rm /swapfile echo -e "${Info} 删除临时增加的swap空间!" } setup(){ check_root check_system check_gcc install_basic temp_swap_add echo -e "${Info} 安装前配置已完成!!" } install(){ download_ngx_pagespeed install_ngx_pagespeed temp_swap_del restart_ngx echo -e "${Info} ngx_pagespeed 模块安装完成!" } status(){ NGX_CONF=`/usr/bin/nginx -V 2>&1 >/dev/null` echo $NGX_CONF | grep -q pagespeed if [ $? = 0 ]; then echo -e "${Info} Pagespeed正在运行 !" else echo -e "${Error} Pagespeed没有运行 !" fi } echo -e "${Info} 选择你要使用的功能: " echo -e "1.安装前配置 2.进行安装 3.检查运行状态 " read -p "输入数字以选择:" function while [[ ! "${function}" =~ ^[1-4]$ ]] do echo -e "${Error} 无效输入" echo -e "${Info} 请重新选择" && read -p "输入数字以选择:" function done if [[ "${function}" == "1" ]]; then setup elif [[ "${function}" == "2" ]]; then install elif [[ "${function}" == "3" ]]; then status fi
顶(0)
踩(0)
- 最新评论