웹사이트 검색

Ubuntu 20.04에서 Google PageSpeed와 함께 Nginx를 설치하는 방법


이 튜토리얼은 다음 OS 버전에 대해 존재합니다.

  • Ubuntu 20.04(Focal Fossa)
  • Ubuntu 15.04(Vivid Vervet)

이 페이지에서

  1. 전제 조건
  2. 시작하기\n
  3. Nginx 웹서버 설치
  4. ngx_pagespeed 다운로드 및 컴파일
  5. ngx_pagespeed 모듈을 사용하도록 Nginx 구성\n
  6. ngx_pagespeed 모듈 확인\n
  7. 결론

Nginx는 인터넷의 많은 사이트를 지원하는 무료 오픈 소스 웹 서버입니다. 리버스 프록시 및 로드 밸런서로 사용할 수 있습니다. 높은 성능과 안정성으로 유명합니다.

ngx_pagespeed는 웹사이트 성능을 최적화하는 데 사용할 수 있는 오픈 소스 Nginx 모듈입니다. Google에서 개발했으며 페이지 로드 시간을 줄이고 웹 사이트 응답 시간을 단축합니다.

이 튜토리얼에서는 Ubuntu 20.04 서버에서 Nginx를 사용하여 ngx_pagespeed를 동적 모듈로 컴파일하는 방법을 보여줍니다.

전제 조건

  • Ubuntu 20.04를 실행하는 서버.\n
  • 서버 IP를 가리키는 유효한 도메인 이름입니다. 이 자습서에서는 example.com 도메인을 사용합니다.\n
  • 루트 암호는 서버에 구성됩니다.\n

시작하기

시작하기 전에 시스템 패키지를 최신 버전으로 업데이트해야 합니다. 다음 명령을 실행하여 업데이트할 수 있습니다.

apt-get update -y

모든 패키지가 설치되면 시스템에 필요한 몇 가지 종속 항목을 설치해야 합니다. 다음 명령으로 모두 설치할 수 있습니다.

apt-get install dpkg-dev build-essential zlib1g-dev libpcre3 git libpcre3-dev unzip -y

모든 패키지가 설치되면 다음 단계로 진행할 수 있습니다.

Nginx 웹서버 설치

다음으로 시스템에 Nginx 웹 서버를 설치해야 합니다. 다음 명령으로 설치할 수 있습니다.

apt-get install nginx -y

설치가 완료되면 다음 명령을 사용하여 설치된 Nginx 버전을 확인할 수 있습니다.

nginx -v

다음 출력에 Nginx 버전이 표시되어야 합니다.

nginx version: nginx/1.18.0 (Ubuntu)

완료되면 다음 단계로 진행할 수 있습니다.

ngx_pagespeed 다운로드 및 컴파일

ngx_pagespeed를 다운로드하고 컴파일하기 전에. 시스템에 Nginx 소스를 다운로드해야 합니다. 다음 명령을 사용하여 Nginx 소스 패키지를 다운로드할 수 있습니다.

참고: 다운로드한 Nginx 버전이 설치된 Nginx 버전과 일치하는지 확인하십시오. 여기서 설치된 Nginx 버전은 1.18.0입니다. 따라서 Nginx 웹사이트에서 버전 1.18.0의 Nginx 소스를 다운로드해야 합니다.

wget http://nginx.org/download/nginx-1.18.0.tar.gz

다운로드가 완료되면 다음 명령을 사용하여 다운로드한 파일의 압축을 풉니다.

tar -xvzf nginx-1.18.0.tar.gz

다음으로 다음 명령을 사용하여 Git 리포지토리에서 ngx_pagespeed 소스를 다운로드합니다.

git clone https://github.com/apache/incubator-pagespeed-ngx.git

다운로드가 완료되면 다운로드한 디렉토리로 디렉토리를 변경하고 다음 명령을 사용하여 안정 버전을 확인하십시오.

cd incubator-pagespeed-ngx
git checkout latest-stable

다음 출력이 표시되어야 합니다.

Note: switching to 'latest-stable'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:

  git switch -c 

Or undo this operation with:

  git switch -

Turn off this advice by setting config variable advice.detachedHead to false

HEAD is now at 11ba8ea54 Update PSOL_BINARY_URL

위의 출력에서 PSOL 다운로드 URL을 보려면 \PSOL_BINARY_URL\ 파일을 찾으십시오.

cat PSOL_BINARY_URL

다음 출력이 표시되어야 합니다.

https://dl.google.com/dl/page-speed/psol/1.13.35.2-$BIT_SIZE_NAME.tar.gz

이제 다음 명령을 실행하여 위 URL을 사용하여 PSOL을 다운로드합니다.

wget https://dl.google.com/dl/page-speed/psol/1.13.35.2-x64.tar.gz

그런 다음 다음 명령을 사용하여 다운로드한 파일의 압축을 풉니다.

tar -xvzf 1.13.35.2-x64.tar.gz

다음으로 디렉터리를 Nginx 소스로 변경하고 다음 명령을 사용하여 필요한 모든 종속성을 설치합니다.

cd /root/nginx-1.18.0
apt-get build-dep nginx
apt-get install uuid-dev

다음으로 다음 명령을 사용하여 ngx_pagespeed 모듈을 컴파일합니다.

./configure --with-compat --add-dynamic-module=/root/incubator-pagespeed-ngx

다음과 같은 결과가 표시되어야 합니다.

Configuration summary
  + using system PCRE library
  + OpenSSL library is not used
  + using system zlib library

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx modules path: "/usr/local/nginx/modules"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

그런 다음 다음 명령을 실행하여 Pagespeed 모듈을 빌드합니다.

make modules

다음으로 생성된 모듈을 Nginx 모듈 디렉터리에 복사합니다.

cp objs/ngx_pagespeed.so /usr/share/nginx/modules/

이 시점에서 ngx_pagespeed 모듈이 시스템에 컴파일됩니다. 이제 다음 단계를 진행할 수 있습니다.

ngx_pagespeed 모듈을 사용하도록 Nginx 구성

다음으로 ngx_pagespeed 모듈을 사용하도록 Nginx를 구성해야 합니다. 먼저 Nginx 기본 구성 파일을 편집하고 ngx_pagespeed 모듈 경로를 정의합니다.

nano /etc/nginx/nginx.conf

파일 시작 부분에 다음 줄을 추가합니다.

load_module modules/ngx_pagespeed.so;

완료되면 파일을 저장하고 닫습니다.

다음으로 다음 명령을 사용하여 pagespeed 캐시용 디렉토리를 만듭니다.

mkdir -p /var/ngx_pagespeed_cache

다음 명령을 사용하여 적절한 소유권을 설정합니다.

chown -R www-data:www-data /var/ngx_pagespeed_cache

다음으로 Nginx 기본 가상 호스트 구성 파일을 편집하고 pagespeed 구성을 추가합니다.

nano /etc/nginx/sites-available/default

모든 줄을 다음 줄로 교체했습니다.

server {
     listen 80;
     server_name example.com; 

     root /var/www/html;
     index index.nginx-debian.html index.html index.htm;

     access_log   /var/log/nginx/access.log;
     error_log    /var/log/nginx/error.log;

     location / {
         try_files $uri $uri/ =404;
     }

     pagespeed on;
     pagespeed FileCachePath "/var/ngx_pagespeed_cache/";
     pagespeed RewriteLevel OptimizeForBandwidth;

     location ~ ".pagespeed.([a-z].)?[a-z]{2}.[^.]{10}.[^.]+" {
         add_header "" "";
     }

     location ~ "^/pagespeed_static/" { }
     location ~ "^/ngx_pagespeed_beacon$" { }
pagespeed RewriteLevel CoreFilters;

 }

완료되면 파일을 저장하고 닫습니다. 그런 다음 다음 명령을 사용하여 구문 오류가 있는지 Nginx를 확인합니다.

nginx -t

다음 출력이 표시되어야 합니다.

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

마지막으로 Nginx 서비스를 다시 시작하여 변경 사항을 적용합니다.

systemctl restart nginx

다음 명령을 사용하여 Nginx 서비스의 상태를 확인할 수도 있습니다.

systemctl status nginx

다음 출력이 표시되어야 합니다.

? nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
     Active: active (running) since Thu 2020-11-26 05:32:23 UTC; 20s ago
       Docs: man:nginx(8)
    Process: 363 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
    Process: 385 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
   Main PID: 386 (nginx)
      Tasks: 3 (limit: 2353)
     Memory: 14.4M
     CGroup: /system.slice/nginx.service
             ??386 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
             ??387 nginx: worker process
             ??388 nginx: worker process

Nov 26 05:32:23 ubunt4 systemd[1]: Starting A high performance web server and a reverse proxy server...
Nov 26 05:32:23 ubunt4 systemd[1]: Started A high performance web server and a reverse proxy server.

완료되면 다음 단계로 진행할 수 있습니다.

ngx_pagespeed 모듈 확인

이 시점에서 Nginx는 ngx_pagespeed 지원으로 구성됩니다. 이제 설치 여부를 테스트할 시간입니다.

페이지 속도를 테스트하려면 도메인 이름을 지정하여 다음 명령을 실행하십시오.

curl -I -p http://example.com

모든 것이 정상이면 다음과 같은 결과가 표시됩니다.

HTTP/1.1 200 OK
Server: nginx/1.18.0 (Ubuntu)
Content-Type: text/html
Connection: keep-alive
Vary: Accept-Encoding
Date: Wed, 25 Nov 2020 11:58:56 GMT
X-Page-Speed: 1.13.35.2-0
Cache-Control: max-age=0, no-cache

위 출력에서 X-Page-Speed: 1.13.35.2-0은 PageSpeed가 활성화되어 웹사이트를 최적화하고 있음을 의미합니다.

결론

축하합니다! Ubuntu 20.04 서버에서 Nginx와 함께 ngx_pagespeed를 성공적으로 설치했습니다. 이 모듈이 Nginx 웹 서버 성능을 높이고 웹 사이트 속도를 높이길 바랍니다. 궁금한 점이 있으면 언제든지 문의해 주세요.