GNU/Linux >> Linux の 問題 >  >> Ubuntu

Ubuntu20.04でGooglePageSpeedを使用してNginxをインストールする方法

Nginxは、インターネット上の多くのサイトに電力を供給する無料のオープンソースWebサーバーです。リバースプロキシおよびロードバランサーとして使用できます。高性能と安定性で知られています。

ngx_pagespeedは、Webサイトのパフォーマンスを最適化するために使用できるオープンソースのNginxモジュールです。これはGoogleによって開発され、ページの読み込み時間を短縮し、ウェブサイトの応答時間を短縮します。

このチュートリアルでは、Ubuntu20.04サーバーでNginxを使用してngx_pagespeedを動的モジュールとしてコンパイルする方法を示します。

前提条件
  • Ubuntu20.04を実行しているサーバー。
  • サーバーIPで指定された有効なドメイン名。このチュートリアルでは、example.comドメインを使用します。
  • ルートパスワードがサーバーに設定されます。
はじめに

開始する前に、システムパッケージを最新バージョンに更新する必要があります。次のコマンドを実行して更新できます:

apt-get update -y

すべてのパッケージをインストールしたら、必要な依存関係をシステムにインストールする必要があります。次のコマンドですべてをインストールできます:

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

すべてのパッケージがインストールされたら、次のステップに進むことができます。

NginxWebサーバーをインストール

次に、NginxWebサーバーをシステムにインストールする必要があります。次のコマンドでインストールできます:

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です。そのため、バージョン1.18.0のNginxソースをNginxWebサイトからダウンロードする必要があります。

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_BINARY_URL」ファイルをcatして、PSOLダウンロード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;

終了したら、ファイルを保存してclsoeします。

次に、次のコマンドを使用して、ページ速度キャッシュ用のディレクトリを作成します。

mkdir -p /var/ngx_pagespeed_cache

次のコマンドを使用して、適切な所有権を設定します。

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

次に、Nginxのデフォルトの仮想ホスト構成ファイルを編集し、ページ速度構成を追加します。

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 ubuntu2004 systemd[1]: Starting A high performance web server and a reverse proxy server...
Nov 26 05:32:23 ubuntu2004 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がアクティブで、ウェブサイトを最適化していることを意味します。

結論

おめでとう!これで、Ubuntu20.04サーバーにNginxを使用してngx_pagespeedが正常にインストールされました。このモジュールがNginxWebサーバーのパフォーマンスを向上させ、Webサイトを高速化することを願っています。ご不明な点がございましたら、お気軽にお問い合わせください。


Ubuntu
  1. Ubuntu18.04にNginxでWordPressをインストールする方法

  2. Ubuntu18.04にNginxを使用してphpMyAdminをインストールする方法

  3. Ubuntu16.04にNginxでMediaWikiをインストールする方法

  1. Ubuntu16.04にNginxを使用してGravCMSをインストールする方法

  2. Ubuntu16.04にNginxを使用してSuiteCRMをインストールする方法

  3. Ubuntu 15.04(Vivid Vervet)にnginxとgooglepagespeedをインストールする方法

  1. Ubuntu15.10にNginxを使用してMagentoをインストールする方法

  2. Ubuntu15.10にNginxでMailpileをインストールする方法

  3. Ubuntu15.10でNginxを使用してRedmine3をインストールする方法