std9.jp

Ubuntu 22.04 サーバーに nginx の最新安定板をインストール、SSL対応

目次 (11)
  1. Ubuntu 22.04 LTS 標準の nginx バージョンを確認
  2. nginx の最新安定バージョンを確認
  3. 最新安定版の nginx をインストールする
  4. nginx 起動状態の確認と自動起動設定
  5. nginx の状態を確認
  6. nginx の自動起動設定
  7. ブラウザで動作確認
  8. nginx 設定の反映
  9. (1) 設定ファイルの構文チェック
  10. (2) nginx を再起動
  11. certbot による SSL 対応

Ubuntu 22.04 LTS 標準の nginx バージョンを確認

Ubuntu 22.04 で nginx パッケージのバージョンを apt info nginx コマンドで調べると 1.18.0 となっていた。

$ apt info nginx
Package: nginx
Version: 1.18.0-6ubuntu14.3

なお、1.18.0 のリリース日は 2020/04/21 のようなので 結構古い。

nginx の最新安定バージョンを確認

2022年12月における、nginx の最新安定バージョン(Stable version)は nginx-1.22.1

https://nginx.org/en/download.html

なお、この最新安定バージョンである nginx-1.22.1 は 2022/10/19 にリリースされている。

最新安定版の nginx をインストールする

https://nginx.org/en/linux_packages.html#Ubuntu ここに記載されていた。

# (1) nginxインストールに必要な前提パッケージをインストール
$ sudo apt install curl gnupg2 ca-certificates lsb-release ubuntu-keyring -y

# (2) aptがパッケージの信頼性を検証できるように、公式のnginx署名キーをインポート
$ curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor \
    | sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null

# (3) ダウンロードしたファイルに適切なキーが含まれていることを確認
#     『573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62』フィンガープリントが表示されること
#     フィンガープリントが異なる場合、curlで取得したファイルが間違っている可能性あり
$ gpg --dry-run --quiet --no-keyring --import --import-options import-show /usr/share/keyrings/nginx-archive-keyring.gpg

# (4) 安定バージョンのnginxパッケージのaptリポジトリを設定する
$ echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] \
http://nginx.org/packages/ubuntu `lsb_release -cs` nginx" \
    | sudo tee /etc/apt/sources.list.d/nginx.list

# (5) Ubuntu標準パッケージよりもnginxのリポジトリを優先するように設定
$ echo -e "Package: *\nPin: origin nginx.org\nPin: release o=nginx\nPin-Priority: 900\n" \
    | sudo tee /etc/apt/preferences.d/99nginx

# (6) nginxをインストールする
$ sudo apt update
$ sudo apt install nginx

インストールした nginx のバージョンを確認する。

$ nginx -v
nginx version: nginx/1.22.1

無事に、nginx の最新安定バージョンである 1.22.1 がインストールされていた。

nginx 起動状態の確認と自動起動設定

nginx の状態を確認

systemctl status nginx コマンドで nginx の状態を確認する。

$ systemctl status nginx
 nginx.service - nginx - high performance web server
     Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
     Active: inactive (dead)
       Docs: https://nginx.org/en/docs/

Active: inactive (dead) 状態で、起動していなかったので、sudo systemctl start nginx コマンドで nginx を起動する。

$ sudo systemctl start nginx
$ systemctl status nginx
 nginx.service - nginx - high performance web server
     Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
     Active: active (running) since Wed 2022-12-28 16:42:49 JST; 2s ago
       Docs: https://nginx.org/en/docs/
    Process: 3186 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
   Main PID: 3187 (nginx)
      Tasks: 3 (limit: 1030)
     Memory: 2.6M
        CPU: 6ms
     CGroup: /system.slice/nginx.service
             ├─3187 "nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf"
             ├─3188 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""
             └─3189 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""

Dec 28 16:42:49 dev-nginx systemd[1]: Starting nginx - high performance web server...
Dec 28 16:42:49 dev-nginx systemd[1]: Started nginx - high performance web server.

ステータスが Active: active (running) になり、無事に起動できた。

nginx の自動起動設定

sudo systemctl enable nginx コマンドで OS 起動時に nginx も自動起動させる。

$ sudo systemctl enable nginx

ブラウザで動作確認

ブラウザの URL 欄に IP を入れて動作を確認できた。

nginx 設定の反映

(1) 設定ファイルの構文チェック

$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

(2) nginx を再起動

$ sudo nginx -s reload

なお、sudo systemctl restart nginx コマンドでも nginx を再起動できるが、再起動中はWeb サービスも止まってしまう。

certbot による SSL 対応

certbot 公式 を参考にインストールを行う。

まずは、snapd をインストールする。

$ sudo apt update
$ sudo apt install snapd

$ snap --version
snap    2.57.5+22.04ubuntu0.1
snapd   2.57.5+22.04ubuntu0.1
series  16
ubuntu  22.04
kernel  5.15.0-56-generic

snap を最新状態にする。

$ sudo snap install core; sudo snap refresh core
$ snap --version
snap    2.57.6
snapd   2.57.6
series  16
ubuntu  22.04
kernel  5.15.0-56-generic

Certbot をインストールする。

$ sudo snap install --classic certbot
certbot 1.32.2 from Certbot Project (certbot-eff✓) installed

certbot コマンドを実行できることを確認する。

$ sudo ln -s /snap/bin/certbot /usr/bin/certbot
$ certbot --version
certbot 1.32.2

証明書を取得する。

# [自動] nginxの設定ファイルを自動で書き替える場合
$ sudo certbot --nginx

# [手動] nginxの設定ファイルを手動で書き替える場合
$ sudo certbot certonly --nginx
shell
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Enter email address (used for urgent renewal and security notices)
 (Enter 'c' to cancel): <ここにメールアドレスを入れる>

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.3-September-21-2022.pdf. You must
agree in order to register with the ACME server. Do you agree?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: <利用規約に同意するならYと入力>
shell
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing, once your first certificate is successfully issued, to
share your email address with the Electronic Frontier Foundation, a founding
partner of the Let's Encrypt project and the non-profit organization that
develops Certbot? We'd like to send you email about our work encrypting the web,
EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: <ニュースやキャンペーン情報は必要なかったのでNと入力した>
shell
Which names would you like to activate HTTPS for?
We recommend selecting either all domains, or all domains in a VirtualHost/server block.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: std9.jp
2: hanyan.std9.jp
3: watermark.std9.jp
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate numbers separated by commas and/or spaces, or leave input
blank to select all options shown (Enter 'c' to cancel): <すべてSSL化が必要なので空白のままEnter>

ブラウザで動作確認して完了。