Nginx(エンジンX)サーバのインストールと設定、そしてHTTPS化

Nginx(エンジンX)はApache(アパッチ)と並ぶWebサーバで、インターネットで情報を公開する際にはこのWebサーバを使って公開することになります。今回の記事ではNginxのインストールとその基本設定について解説していきます。AWSのEC2インスタンス(Amazon Linux 2023)にSSH接続ができていて、基本的なLinuxコマンドが使えることが前提となります。基礎学習がまだの方は「EC2インスタンスの作成手順」と「Linuxの基礎コマンド」の記事を学習してから本記事を読み進めてください。

目次

Nginxサーバのインストール

インストール方法

Amazon Linux 2023のインスタンスを使用しているので、パッケージ管理のコマンドには dnf コマンドを使用してNginxをインストールします。dnf コマンドは今まで長い間使われてきた yum コマンドの後継になります。以下の手順でNginxをインストールしていきましょう。Nginxのパッケージ名は nginx になります。

【システムのアップデート】
sudo dnf update -y

【Nginxがインストール済みかを確認】
sudo dnf list installed nginx

【Nginxのパッケージが存在するかとバージョンの確認】
sudo dnf info nginx

【Nginxをインストール】
sudo dnf install nginx -y

【前述のコマンドでもNginxのインストールを確認】
sudo dnf list installed nginx

【バージョンの表示】
nginx -version

Nginxサーバの起動

【Nginxサーバの起動】
sudo systemctl start nginx
【Nginxサーバの停止】
sudo systemctl stop nginx

これでブラウザからWebサーバにアクセスできるようになりました。EC2インスタンスに関連付けたドメイン名でをブラウザに入力してみましょう。画面に「Welcome to nginx!」の文字が表示されれば成功です。

http://mydomain.example.com

Nginxサーバはデフォルト状態では暗号化通信をしないHTTPでアクセスできることに注意してください。
HTTPS化の暗号化通信の設定は後述のセクションで解説します。

システムブート時のNginx起動設定

現状のままEC2インスタンスを再起動した場合、Nginxサーバは停止したままの状態となります。Nginxサーバの様に常駐するタイプのプログラムOSの再起時に既に起動を開始しておくのが一般的です。

次にこの設定の手順を解説します。

【現状ではシステムブート時の起動設定がdisabledであることを確認】
sudo systemctl is-enabled nginx

【システムブート時の起動を有効化】
sudo systemctl enable nginx

【再度前述のコマンドでシステムブート時の起動設定がenabledになったことを確認】
sudo systemctl is-enabled nginx

Nginxの設定ファイル

設定ファイルとはプログラムの挙動を定義するファイルです。この項目ではNginxの設定ファイルについて理解を深めていきましょう。

nginx.confファイル

設定ファイルの場所は以下になります。

/etc/nginx/nginx.conf

設定ファイルを変更する場合は必ずcpコマンドでファイルのバックアップコピーを作成してから作業しましょう。

知っておくべき設定を以下列挙しておきます。

user nginx;
->Nginxプログラムで使用されるユーザを定義しています。

listen       80;
listen       [::]:80;
->Webサーバの使用するポートを定義しています。デフォルトの80番ポートが使用されています。

server_name  _;
->使用するドメイン名を定義します。
ここでは _ EC2インスタンスに紐づけたドメイン名を指定してください。

root         /usr/share/nginx/html;
->Linuxファイルシステム内のどこのディレクトリを公開するかを定義しています。
任意のディレクトリに変更することができます。

Nginxの再起動

設定ファイルの変更した後、その変更を有効にするにはNginxの再起動が必要です。以下のコマンドで再起動しておいてください。

sudo systemctl restart nginx

ファイルを公開してみよう

ここではドキュメントルート(公開ディレクトリ)に実際に作ったフィアルを配置してブラウザで表示してみましょう。test.htmlというファイルを作成し、以下のディレクトリにファイルを配置します。ファイル内には「Hello World」と記述しておきましょう。

/usr/share/nginx/html/test.html

以下のURLでtest.htmlにアクセスできます。

http://mydomain.example.com/test.html

http://mydomain.example.com/の最後のスラッシュ=/usr/share/nginx/html/の最後のスラッシュとなります。

Let’s Encryptを使ったHTTS化

ここではWebサーバとの通信を暗号化してより安全にサーバにアクセスできるHTTPS化の設定について解説していきます。HTTPS化にはSSL証明書というものが必要になりますが、無料でこの証明書を発行してくれる Let’s Encrypt というサービスを使用してHTTPS化をしていきます。

Let’s Encrypt 用にCertbotのインストールと実行

Certbotのインストール

Let’s Encryptの証明書を発行してくれるcertbotというプログラムはPython言語でできているので、まずはPythonのパッケージ管理ツールをインストールであるPIPをインストールする必要があります。

【Pythonと関連パッケージをインストール】
sudo dnf install python3 augeas-libs -y

【Pythonの仮想環境の設定とPIPのインストール】
sudo python3 -m venv /opt/certbot/
sudo /opt/certbot/bin/pip install --upgrade pip

【PIPコマンドを使ってcertbotをインストール】
sudo /opt/certbot/bin/pip install certbot certbot-nginx

【certbotコマンドの使用準備】
sudo ln -s /opt/certbot/bin/certbot /usr/bin/certbot

Certbotの実行

ここまでで、Certbotを実行する準備が整いました。次のコマンドで証明書の発行をしてみましょう。

sudo certbot --nginx

プロンプトに従って質問内容に答えていきます。

1.メールアドレスの入力。
2.利用条件への同意(Y)
3.関連組織へのメールアドレスの共有とニュースなどの情報を受け取りたいか(N)
->ここはNでよいと思います。
4.HTTPSを適用したいドメインの番号を選択します。
5.証明書の発行が成功した旨のメッセージが表示されて完了になります。

以下はcertbotコマンド実行時の出力例になります。

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Enter email address (used for urgent renewal and security notices)
 (Enter 'c' to cancel): myemail123@gmail.com

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at:
https://letsencrypt.org/documents/LE-SA-v1.5-February-24-2025.pdf
You must agree in order to register with the ACME server. Do you agree?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: Y

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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
Account registered.

Which names would you like to activate HTTPS for?
We recommend selecting either all domains, or all domains in a VirtualHost/server block.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: mydomain.example.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate numbers separated by commas and/or spaces, or leave input
blank to select all options shown (Enter 'c' to cancel): 1
Requesting a certificate for mydomain.example.com

Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/mydomain.example.com/fullchain.pem
Key is saved at:         /etc/letsencrypt/live/mydomain.example.com/privkey.pem
This certificate expires on 2025-06-02.
These files will be updated when the certificate renews.

Deploying certificate
Successfully deployed certificate for mydomain.example.com to /etc/nginx/nginx.conf
Congratulations! You have successfully enabled HTTPS on https://mydomain.example.com

NEXT STEPS:
- The certificate will need to be renewed before it expires. Certbot can automatically renew the certificate in the background, but you may need to take steps to enable that functionality. See https://certbot.org/renewal-setup for instructions.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
If you like Certbot, please consider supporting our work by:
 * Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
 * Donating to EFF:                    https://eff.org/donate-le
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Nginxの再起動とHTTPS化の確認

Certbot実行後には必ずNginxの再起動を忘れずに行いましょう。

sudo systemctl restart nginx

先ほど表示したドメインのページをリロードするかURLの先頭をhttps://に変更してページを表示してみましょう。ULR欄の左側に鍵マークが表示されたのが確認できるはずです。これでHTTPS化が完了しました。

https://mydomain.example.com/test.html

Let’s Encryptの証明書の更新を自動化する

Cronieのインストール

新しいAmazon Linxu 2023のEC2インスタンス、旧来のAmazon Linux 2のインスタンスと違って、定期実行プログラムのcrondパッケージはインストールされていません。代わりに Amazon Linxu 2023 のインスタンスではcronieというパッケージをインストールする必要があります。

【cronieがインストール済みかを確認】
sudo dnf installed cronie

【cronieパッケージが存在するか確認】
sudo dnf info cronie

【cronieをインストール】
sudo dnf install cronie -y

【システムブート時にcoronieも起動するように設定】
sudo systemctl enable crond.service

【cronieプログラムw再起動】
sudo systemctl restart crond

【cronieプログラムの実行状態を確認】
sudo systemctl status crond | grep Active
sudo systemctl status crond.service
->上記共にActiveと表示されればOKです。

Crontabファイルの設定

まずは、以下のファイルのバックアップを取りましょう。

/etc/crontab

ファイル内には以下を追記しましょう。以下の例では毎日0時 10分 にcertbotコマンドを使用してSSL証明書の更新を行います。証明書の期限の 30 日より前は、コマンドは実行されますが期限よりだいぶ前なので実際の更新は行われませんが、更新期限から 30 日を切ると実際に更新処理が行われます。

10 0 * * * root certbot renew --no-self-upgrade > /etc/crontab.log 2>&1

定期実行処理については「Linux|定期処理(Cronジョブ)実行の設定方法」の記事を参考にして下さい。

あわせて読みたい
Linux|定期処理(Cronジョブ)実行の設定方法 本日の記事は、Webシステムの処理の自動化に大いに役立つ定期処理実行(Cronジョブ)の設定方法について学習していきます。クロン(Cron)とは定期処理の実行を管理する...

まとめ

今回は、Apache同様に世界中で広く使用されているWebサーバの代表例の一つ、Nginxについて学習しました。Nginxのインストール方法、起動・停止、設定ファイルについても知識を深めていきました。システムブート時にWebサーバを起動する方法も学びましたね。Webサーバの運用に通信のHTTPS化は今や必須です。証明書の取得にはCertbotツールを使ってLet’s Encryptから証明書を取得しました。そして、その証明書の更新も自動化することができました。サーバのインストールからHTTPS化までスムースに設定できるようにしておきましょう。

Apacheサーバのインストールについては以下の記事を参考にして下さい。

あわせて読みたい
Apache(アパッチ)サーバのインストールと設定、そしてHTTPS化 ブログなどをインターネットで公開するには、Webサーバが必要になります。Webサーバの代表的なプログラムにはApacheとNginxの2つがありますが、今回の記事ではApacheの...
よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

コメント

コメントする

目次