Nginx Memo

Posted on 2018-10-18(목) in Nginx

Add ngx_pagespeed module to Ubuntu 17.10

Nginx install from Ubuntu Source Package

참고: Compile Nginx with ngx_pagespeed Module on Ubuntu 16.04 - LinuxBabe.Com

Get Nginx Source Package

% mkdir ~/nginx && cd ~/nginx
% sudo apt install dpkg-dev
% sudo apt source nginx
% ls
nginx-1.12.1                         nginx_1.12.1-0ubuntu2.dsc
nginx_1.12.1-0ubuntu2.debian.tar.xz  nginx_1.12.1.orig.tar.gz

Modify rules file

% sudo vim nginx-1.12.1/debian/rules
    ...
    --add-module=$(HOME)/ngx_pagespeed-latest-stable \
    ...

참고: Build ngx_pagespeed From Source

Rebuild Nginx Source Package

% sudo apt build-dep nginx
% cd nginx-1.12.1
% sudo dpkg-buildpackage -b

Install Nginx Package

% dpkg -i libnginx-mod-*
% dpkg -i nginx_1.12.1-0ubuntu2_all.deb nginx-common_1.12.1-0ubuntu2_all.deb nginx-core_1.12.1-0ubuntu2_amd64.deb

Setup RamDisk(tmpfs) for ngx_pagespeed

참고: Linux ramdisk – tmpfs – mapoo’s blog

fstab

% cat /etc/fstab
tmpfs   /dev/shm        tmpfs   size=32m        0       0

mount tmpfs

% sudo mount tmpfs
% df
Filesystem     1K-blocks    Used Available Use% Mounted on
udev              498920       0    498920   0% /dev
tmpfs             101468    5748     95720   6% /run
/dev/xvda1       8065444 2744036   5305024  35% /
tmpfs              32768     812     31956   3% /dev/shm
tmpfs               5120       0      5120   0% /run/lock
tmpfs             507324       0    507324   0% /sys/fs/cgroup
tmpfs             101464       0    101464   0% /run/user/1000

Setup nginx.conf

% cat /etc/nginx/nginx.conf
http {
        pagespeed On;
        pagespeed FileCachePath "/dev/shm/tmp";
        pagespeed EnableFilters combine_css,combine_javascript;
        
}

Restart Nginx

% sudo service nginx stop
% sudo service nginx start

Verify Results

% http -h https://oh.mypi.co
HTTP/1.1 200 OK
Cache-Control: max-age=0, no-cache
Connection: keep-alive
Content-Encoding: gzip
Content-Type: text/html; charset=utf-8
Date: Fri, 17 Nov 2017 13:57:47 GMT
Server: nginx/1.12.1 (Ubuntu)
Transfer-Encoding: chunked
X-Page-Speed: 1.12.34.3-0

THE END


Add ngx_pagespeed module to Ubuntu 16.10

2017/06/15

설치

  • 자동 설치 시작
% bash <(curl -f -L -sS https://ngxpagespeed.com/install) --nginx-version latest

컴파일 옵션 추가

  • —with-http_v2_module —with-http_ssl_module” 컴파일 옵션 추가.
...
Extracting 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.
> --with-http_v2_module --with-http_ssl_module
...

NginX 설정

  • pagespeed module 설정 확인 (nginx.conf)
% cat /usr/local/nginx/conf/nginx.conf | grep "pagespeed"
...
    server {
        ...
        pagespeed on;
# https://developers.google.com/speed/pagespeed/module/system
        pagespeed FileCacheSizeKb            102400;
        pagespeed FileCacheCleanIntervalMs   3600000;
        pagespeed FileCacheInodeLimit        500000;
        pagespeed LRUCacheKbPerProcess     8192;
        pagespeed LRUCacheByteLimit        16384;
# pagespeed DefaultSharedMemoryCacheKB 50000;
        pagespeed FileCachePath /var/ngx_pagespeed_cache;
# pagespeed EnableFilters combine_css,combine_javascript;
# Ensure requests for pagespeed optimized resources go to the pagespeed handler
        location ~ "\.pagespeed\.([a-z]\.)?[a-z]{2}\.[^.]{10}\.[^.]+" {
        location ~ "^/pagespeed_static/" { }
        location ~ "^/ngx_pagespeed_beacon$" { }
        ...
    }
...
  • http2 module 설정 확인 (nginx.conf)
% cat /usr/local/nginx/conf/nginx.conf | grep "http2"
...
    server {
        ...
        listen 443 ssl http2;
        listen [::]:443 ssl http2;
        ...
    }
...

Ubuntu Service 설정

  • nginx.service 수정 PIDFile과 nginx 실행 파일 위치 변경. /run/nginx.pid => /usr/local/nginx/logs/nginx.pid /usr/sbin/nginx => /usr/local/nginx/sbin/nginx
% cat /lib/systemd/system/nginx.service
[Service]
...
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -q -g 'daemon on; master_process on;'
ExecStart=/usr/local/nginx/sbin/nginx -g 'daemon on; master_process on;'
ExecReload=/usr/local/nginx/sbin/nginx -g 'daemon on; master_process on;' -s reload
ExecStop=/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /usr/local/nginx/logs/nginx.pid
...
  • nginx.service 실행
% sudo -s
% service nginx start

결과 확인

  • NginX 서비스 확인
  • http : httpie
% http --headers https://oh.mypi.co/
HTTP/1.1 200 OK
Cache-Control: max-age=0, no-cache
Connection: keep-alive
Content-Encoding: gzip
Content-Type: text/html; charset=utf-8
Date: Thu, 15 Jun 2017 03:26:52 GMT
Server: nginx/1.13.1       <===  NginX 버전 확인
Strict-Transport-Security: max-age=15768000
Transfer-Encoding: chunked
Vary: Accept-Encoding
X-Page-Speed: 1.11.33.4-0  <===  PageSpeed 버전 확인

2016/10/15

1. Update of Ubuntu packages

% alias agud # This is oh-my-zsh's ubuntu plug-in
agud='sudo apt-get update && sudo apt-get dist-upgrade'
% agud

2. Prepare for making deb

2.1 getting deb-source of nginx

% sudo apt-get install dpkg-dev build-essential zlib1g-dev libpcre3 libpcre3-dev unzip
% mkdir ~/tmp/nginx_source
% cd ~/tmp/nginx_source
% apt-get source nginx
% apt-get build-dep nginx

2.2 setup of ngx_pagespeed

% mkdir ~/tmp/ngx_pagespeed
% cd ~/tmp/ngx_pagespeed
% ngx_version='1.11.33.4'
% http -d https://github.com/pagespeed/ngx_pagespeed/archive/release-%{ngx_version}-beta.tar.gz
% aunpack release-${ngx_version]-beta.tar.gz
% http -d https://dl.google.com/dl/page-speed/psol/${ngx_version}.tar.gz
% aunpack ${ngx_version}.tar.gz

3. Rebuild nginx-extras package from deb-source

3.1 Edit rules file for adding ngx_pagespeed module on deb-source of Nginx

% sudo vim ~/tmp/nginx_source/nginx-1.10.1/debian/rules
...
common_configure_flags := \
...
                        --add-module=$(HOME)/tmp/ngx_pagespeed/ngx_pagespeed-1.11.33.4-beta
...

3.2 Rebuild nginx

% cd ~/tmp/nginx_source/nginx-1.10.1
% sudo dpkg-buildpackage -b

4. Install nginx-extras

% cd ~/tmp/nginx_source
% sudo dpkg -i nginx-common_1.10.1-0ubuntu1_all.deb nginx-extras_1.10.1-0ubuntu1_amd64.deb

5. Hold nginx package

% sudo apt-mark hold nginx-extras

Add ngx_pagespeed module to Ubuntu 15.10

2016년 3월 6일 기준으로,

3개의 파일을 받는다. * https://dl.google.com/dl/linux/mod-pagespeed/tar/beta/mod-pagespeed-beta-1.9.32.10-r7423.tar.bz2) * https://github.com/pagespeed/ngx_pagespeed/archive/v1.9.32.10-beta.zip * http://nginx.org/download/nginx-1.9.12.tar.gz

참고 사이트: * https://developers.google.com/speed/pagespeed/module/build_mod_pagespeed_from_source#prerequisites * https://developers.google.com/speed/pagespeed/module/build_ngx_pagespeed_from_source#dependencies * https://github.com/pagespeed/ngx_pagespeed/releases * http://nginx.org/en/download.html * http://www.tecmint.com/install-mod_pagespeed-to-boost-apache-nginx-performance/

./generate.sh

1. Required

sudo apt-get install apache2 g++ python subversion gperf make devscripts fakeroot git curl
sudo apt-get install icu-devtools libicu-dev libapr1-dev libaprutil1-dev apache2-dev

net/instaweb/system/apr_mem_cache.cc:23:34: fatal error: apr_pools.h: No such file or directory

2. Mod_Pagespeed Compile

mkdir -p ~/bin
cd ~/bin
git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git

export PATH=$PATH:~/bin/depot_tools

cd
mkdir ~/mod_pagespeed    # Any directory is fine.
cd ~/mod_pagespeed
gclient config https://github.com/pagespeed/mod_pagespeed.git --unmanaged --name=src # depot_tools
git clone https://github.com/pagespeed/mod_pagespeed.git src
cd src

git checkout latest-stable
cd ..
gclient sync --force --jobs=1

cd ~/mod_pagespeed/src
~/mod_pagespeed/src % make AR.host=`pwd`/build/wrappers/ar.sh AR.target
=`pwd`/build/wrappers/ar.sh BUILDTYPE=Release

3. Ngx_Pagespeed - Nginx Module Complie

cd
NPS_VERSION=1.10.33.6
wget https://github.com/pagespeed/ngx_pagespeed/archive/release-${NPS_VERSION}-beta.zip -O release-${NPS_VERSION}-beta.zip
unzip release-${NPS_VERSION}-beta.zip
cd ngx_pagespeed-release-${NPS_VERSION}-beta/
wget https://dl.google.com/dl/page-speed/psol/${NPS_VERSION}.tar.gz
tar -xzvf ${NPS_VERSION}.tar.gz  # extracts to psol/

4. Nginx Compile

cd
# check http://nginx.org/en/download.html for the latest version
NGINX_VERSION=1.9.12
wget http://nginx.org/download/nginx-${NGINX_VERSION}.tar.gz
tar -xvzf nginx-${NGINX_VERSION}.tar.gz

sudo chown -R nginx: nginx-1.9.12

cd nginx-${NGINX_VERSION}/
./configure --add-module=$HOME/ngx_pagespeed-release-${NPS_VERSION}-beta \
--user=www-data \
--group=www-data \
--with-http_ssl_module \
--with-threads \
--with-file-aio \
--with-ipv6 \
--with-http_v2_module

make
sudo make install

~/works/nginx-1.9.12 % MOD_PAGESPEED_DIR=&quot;$HOME/mod_pagespeed/src&quot; ./configure --add-module=$HOME/works/ngx_pagespeed-release-1.9.32.10-beta

5. Nginx Configure

cat /usr/local/nginx/conf/nginx.conf
        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
}
sudo vim /etc/nginx/sites-enabled/default
...
server {

#########################################
pagespeed on;

# https://developers.google.com/speed/pagespeed/module/system
pagespeed FileCacheSizeKb            102400;
pagespeed FileCacheCleanIntervalMs   3600000;
pagespeed FileCacheInodeLimit        500000;
pagespeed LRUCacheKbPerProcess     8192;
pagespeed LRUCacheByteLimit        16384;
# pagespeed DefaultSharedMemoryCacheKB 50000;

# Needs to exist and be writable by nginx.  Use tmpfs for best performance.
pagespeed FileCachePath /var/ngx_pagespeed_cache;

# Ensure requests for pagespeed optimized resources go to the pagespeed handler
# and no extraneous headers get set.
location ~ "\.pagespeed\.([a-z]\.)?[a-z]{2}\.[^.]{10}\.[^.]+" {
  add_header "" "";
}
location ~ "^/pagespeed_static/" { }
location ~ "^/ngx_pagespeed_beacon$" { }
#########################################

listen 80 default_server;

6. Verify

% curl -D- https://oh.mypi.co/ | less
HTTP/1.1 200 OK
Server: nginx/1.9.12
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Accept-Encoding
Strict-Transport-Security: max-age=15768000
Date: Sun, 06 Mar 2016 05:59:06 GMT
X-Page-Speed: 1.10.33.6-0
Cache-Control: max-age=0, no-cache