Debian 11 (bullseye) - Web サーバ Nginx 構築(Nginx 公式リポジトリ使用)!

Updated:


Debian GNU/Linux 11 (bullseye) に Web サーバ Nginx を Nginx 公式リポジトリを使用して導入する方法についての記録です。

以前古いバージョンでの作業時に残していた記録を参考に作業を行い、今回更新した作業記録を貼付する形式の内容となっています。
(当然ながら、興味がなければスルーしてください)

0 前提条件

  • Debian GNU/Linux 11.0.0 (bullseye; 64bit) での作業を想定。
  • 接続元のマシンも Debian GNU/Linux 11 (bullseye; 64bit) を想定。
  • Debian 公式リポジトリの Nginx はバージョンが古いため、Nginx リポジトリを使用して 1.21.3(当記事執筆時点最新安定版)をインストールする。
  • 実際に運用する際は、ドキュメントルートを変更する等、設定を編集すること。
  • root ユーザでの作業を想定。

1. 依存パッケージのインストール

# apt -y install curl gnupg2 ca-certificates lsb-release debian-archive-keyring

2. 署名鍵のインポート

# curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor \
  | tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null

# gpg --dry-run --quiet --import --import-options import-show \
  /usr/share/keyrings/nginx-archive-keyring.gpg
  • フィンガープリントが 573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62 であることを確認。

3. リポジトリの追加設定

リポジトリの追加、 APT のデフォルト優先度の設定を行う。

# echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] \
  http://nginx.org/packages/mainline/debian `lsb_release -cs` nginx" \
  | tee /etc/apt/sources.list.d/nginx.list

# echo -e "Package: *\nPin: origin nginx.org\nPin: release o=nginx\nPin-Priority: 900\n" \
  | tee /etc/apt/preferences.d/99nginx

4. Nginx のインストール

Apt パッケージリストを更新後、 Nginx をインストールする。

# apt -y update
# apt -y install nginx

5. インストールの確認

Nginx がインストールできたか確認してみる。(-v の代わりに -V オプションを使用すると詳細に表示される)

# nginx -v
nginx version: nginx/1.21.3

6. 設定

今回のインストール環境の場合、基本的(グローバル)な設定のファイルは /etc/nginx/nginx.conf で、このファイルから /etc/nginx/conf.d ディレクトリ配下の設定ファイルを読み込む形式となっている。
取り急ぎ、デフォルトのままとした。(当方、最終的には詳細に設定している)
詳細な設定は、「当ブログ Nginx 関連の過去記事」を参照。

7. ファイアウォール(ufw)の設定

TCP ポート 80 を開放する必要がある。(HTTPS の場合は TCP: 443)

# ufw allow 80/tcp
Rule added

# ufw status
    :
80/tcp                     ALLOW       Anywhere
    :

8. サーバの起動

# systemctl start nginx

9. 起動確認

ブラウザで http://<サーバアドレス or ホスト名>/ にアクセスしてみる。
Welcome to nginx! と以下5行くらい表示されば成功。

10. 自動起動の設定

マシン起動時に自動で Nginx を起動させるには以下のようにする。(インストール直後は自動起動するようになっているはずなので、そうなっていない場合)

# systemctl enable nginx
nginx.service is not a native service, redirecting to systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable nginx

# systemctl is-enabled nginx
enabled

nginx はネイティブなサービスでないため、 systemd-sysv-install にリダイレクトされる)

自動起動しないようにするには、

# systemctl disable nginx
nginx.service is not a native service, redirecting to systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install disable nginx
insserv: warning: current start runlevel(s) (empty) of script `nginx' overrides LSB defaults (2 3 4 5).
insserv: warning: current stop runlevel(s) (0 1 2 3 4 5 6) of script `nginx' overrides LSB defaults (0 1 6).

# systemctl is-enabled nginx
nginx.service is not a native service, redirecting to systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install is-enabled nginx
disabled

11. 参考サイト


以上。





 

Sponsored Link

 

Comments