Cara Install Nginx PageSpeed (ngx_pagespeed) pada CentOS

 CentOS, Nginx, Tutorial, Webserver

PageSpeed merupakan modul open source untuk web server yang akan mengoptimalkan performa website (reduces page load time), karena modul ini memiliki beberapa filter yang secara otomatis mengoptimalkan file seperti HTML, CSS, JavaScript, JPEG, PNG dan sumber daya lainnya.

Dan berikut tutorial cara install/compile nginx (from source) dengan PageSpeed Module (ngx_pagespeed) pada CentOS 5 atau 6 VPS:

  • kalau sudah terinstall nginx via yum diremove dulu
    service nginx stop
    chkconfig nginx off
    yum remove nginx
  • Install packages yang dibutuhkan untuk compile nginx
    yum install gcc gcc-c++ make zlib-devel pcre-devel openssl-devel
  • Download ngx_pagespeed and psol Module
    mkdir pagespeed
    cd pagespeed
    wget https://github.com/pagespeed/ngx_pagespeed/archive/v1.8.31.3-beta.zip
    

    Cara Install Nginx PageSpeed ngx_pagespeed pada CentOS 1

    Unzip hasil pagespeed module yang sudah didownload

    unzip v1.8.31.3-beta && cd ngx_pagespeed-1.8.31.3-beta

    Cara Install Nginx PageSpeed ngx_pagespeed pada CentOS 2

    sekarang download psol Module untuk pagespeed

    wget https://dl.google.com/dl/page-speed/psol/1.8.31.4.tar.gz
    tar -xzvf 1.8.31.4.tar.gz
    

    Cara Install Nginx PageSpeed ngx_pagespeed pada CentOS 3

  • Download Nginx
    Download versi nginx yang terakhir (stable) disini

    cd /root/pagespeed
    wget http://nginx.org/download/nginx-1.6.1.tar.gz
    

    Cara Install Nginx PageSpeed ngx_pagespeed pada CentOS 4

    kemuadian extract nginx yang sudah didownload tadi dan masuk kedirektori nginx yang sudah diekstrak

    tar -zxvf nginx-1.6.1.tar.gz && cd nginx-1.6.1

    Cara Install Nginx PageSpeed ngx_pagespeed pada CentOS 5

  • Compile Nginx dengan PageSpeed Module
    ./configure --add-module=/root/pagespeed/ngx_pagespeed-1.8.31.3-beta --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --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-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-mail --with-mail_ssl_module --with-file-aio --with-ipv6 --with-http_spdy_module --with-cc-opt='-O2 -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m32 -mtune=generic -fasynchronous-unwind-tables'; make; make install
    

    Cara Install Nginx PageSpeed ngx_pagespeed pada CentOS 6

  • Tambahkan script Nginx init
    nano /etc/init.d/nginx

    lalu paste script dibawah ini

    #!/bin/sh
    #
    # nginx - this script starts and stops the nginx daemon
    #
    # chkconfig:   - 85 15
    # description:  Nginx is an HTTP(S) server, HTTP(S) reverse 
    #               proxy and IMAP/POP3 proxy server
    # processname: nginx
    # config:      /etc/nginx/nginx.conf
    # config:      /etc/sysconfig/nginx
    # pidfile:     /var/run/nginx.pid
    
    # Source function library.
    . /etc/rc.d/init.d/functions
    
    # Source networking configuration.
    . /etc/sysconfig/network
    
    # Check that networking is up.
    [ "$NETWORKING" = "no" ] && exit 0
    
    nginx="/usr/sbin/nginx"
    prog=$(basename $nginx)
    
    sysconfig="/etc/sysconfig/$prog"
    lockfile="/var/lock/subsys/nginx"
    pidfile="/var/run/${prog}.pid"
    
    NGINX_CONF_FILE="/etc/nginx/nginx.conf"
    
    [ -f $sysconfig ] && . $sysconfig
    
    
    start() {
        [ -x $nginx ] || exit 5
        [ -f $NGINX_CONF_FILE ] || exit 6
        echo -n $"Starting $prog: "
        daemon $nginx -c $NGINX_CONF_FILE
        retval=$?
        echo
        [ $retval -eq 0 ] && touch $lockfile
        return $retval
    }
    
    stop() {
        echo -n $"Stopping $prog: "
        killproc -p $pidfile $prog
        retval=$?
        echo
        [ $retval -eq 0 ] && rm -f $lockfile
        return $retval
    }
    
    restart() {
        configtest_q || return 6
        stop
        start
    }
    
    reload() {
        configtest_q || return 6
        echo -n $"Reloading $prog: "
        killproc -p $pidfile $prog -HUP
        echo
    }
    
    configtest() {
        $nginx -t -c $NGINX_CONF_FILE
    }
    
    configtest_q() {
        $nginx -t -q -c $NGINX_CONF_FILE
    }
    
    rh_status() {
        status $prog
    }
    
    rh_status_q() {
        rh_status >/dev/null 2>&1
    }
    
    # Upgrade the binary with no downtime.
    upgrade() {
        local oldbin_pidfile="${pidfile}.oldbin"
    
        configtest_q || return 6
        echo -n $"Upgrading $prog: "
        killproc -p $pidfile $prog -USR2
        retval=$?
        sleep 1
        if [[ -f ${oldbin_pidfile} && -f ${pidfile} ]];  then
            killproc -p $oldbin_pidfile $prog -QUIT
            success $"$prog online upgrade"
            echo 
            return 0
        else
            failure $"$prog online upgrade"
            echo
            return 1
        fi
    }
    
    # Tell nginx to reopen logs
    reopen_logs() {
        configtest_q || return 6
        echo -n $"Reopening $prog logs: "
        killproc -p $pidfile $prog -USR1
        retval=$?
        echo
        return $retval
    }
    
    case "$1" in
        start)
            rh_status_q && exit 0
            $1
            ;;
        stop)
            rh_status_q || exit 0
            $1
            ;;
        restart|configtest|reopen_logs)
            $1
            ;;
        force-reload|upgrade) 
            rh_status_q || exit 7
            upgrade
            ;;
        reload)
            rh_status_q || exit 7
            $1
            ;;
        status|status_q)
            rh_$1
            ;;
        condrestart|try-restart)
            rh_status_q || exit 7
            restart
         ;;
        *)
            echo $"Usage: $0 {start|stop|reload|configtest|status|force-reload|upgrade|restart|reopen_logs}"
            exit 2
    esac
    

    Cara Install Nginx PageSpeed (ngx_pagespeed) pada CentOS 7

    Permission untuk nginx init

    useradd -r nginx
    chmod +x /etc/init.d/nginx
    

    Cara Install Nginx PageSpeed (ngx_pagespeed) pada CentOS 8

  • Enable ngx_pagespeed Module pada Nginx
    edit Nginx configuration (nginx.conf) untuk configurasi nginx pagespeed (ngx_pagespeed)

    nano /etc/nginx/nginx.conf

    tambahkan config berikut pada nginx.conf

    ...
    # enable ngx_pagespeed
    pagespeed on;
    pagespeed RespectVary on;
    pagespeed XHeaderValue "Powered By ngx_pagespeed jadoel.info";
    pagespeed FileCachePath /var/ngx_pagespeed_cache;
    ...
    

    Cara Install Nginx PageSpeed ngx_pagespeed pada CentOS 9

  • membuat folder direktori untuk pagespeed filecachepath
    mkdir /var/ngx_pagespeed_cache
    chown nginx:nginx /var/ngx_pagespeed_cache
    

    Cara Install Nginx PageSpeed ngx_pagespeed pada CentOS 10

  • Start dan startup nginx
    service nginx start
    chkconfig nginx on
    

    Cara Install Nginx PageSpeed ngx_pagespeed pada CentOS 11

terakhir ntuk melihat apakah modul nginx pagespeed sudah terinstal bisa dengan:

nginx -V

Cara Install Nginx PageSpeed ngx_pagespeed pada CentOS 12

atau dengan curl:

curl -I http://jadoel.info

Cara Install Nginx PageSpeed ngx_pagespeed pada CentOS 13

hasil gtmetrix
Cara Install Nginx PageSpeed ngx_pagespeed pada CentOS gtmetrix

NB:
untuk menambahkan nginx pagespeed config bisa dilihat disini