0
0

Amazon Linux2023にnginxをインストールする

Last updated at Posted at 2024-08-13

概要

AmazonLinux2からAmazonLinux2023に乗り換えるべく、同じ環境を作ります。

環境

  • AWS EC2
    • OS:Amazon Linux 2023
    • AMI:al2023-ami-2023.5.20240730.0-kernel-6.1-x86_64

構築手順

1. パッケージの確認

AmazonLinux2023の場合はamazon-linux-extrasを使わずともデフォルトでnginxがあるらしいので確認します。

$ dnf search nginx
Last metadata expiration check: 3 days, 0:23:38 ago on Mon Aug  5 16:21:27 2024.
============================================ Name Exactly Matched: nginx ============================================
nginx.x86_64 : A high performance web server and reverse proxy server
=========================================== Name & Summary Matched: nginx ===========================================
collectd-nginx.x86_64 : Nginx plugin for collectd
nginx-all-modules.noarch : A meta package that installs all available Nginx modules
nginx-core.x86_64 : nginx minimal core
nginx-filesystem.noarch : The basic directory layout for the Nginx server
nginx-mimetypes.noarch : MIME type mappings for nginx
nginx-mod-devel.x86_64 : Nginx module development files
nginx-mod-http-image-filter.x86_64 : Nginx HTTP image filter module
nginx-mod-http-perl.x86_64 : Nginx HTTP perl module
nginx-mod-http-xslt-filter.x86_64 : Nginx XSLT module
nginx-mod-mail.x86_64 : Nginx mail modules
nginx-mod-stream.x86_64 : Nginx stream modules
python3-certbot-nginx.noarch : The nginx plugin for certbot

2. nginxのインストール

$ sudo dnf -y install nginx

Dependencies resolved.
=====================================================================================================================
 Package                        Architecture      Version                               Repository              Size
=====================================================================================================================
Installing:
 nginx                          x86_64            1:1.24.0-1.amzn2023.0.2               amazonlinux             32 k
Installing dependencies:
 generic-logos-httpd            noarch            18.0.0-12.amzn2023.0.3                amazonlinux             19 k
 gperftools-libs                x86_64            2.9.1-1.amzn2023.0.3                  amazonlinux            308 k
 libunwind                      x86_64            1.4.0-5.amzn2023.0.2                  amazonlinux             66 k
 nginx-core                     x86_64            1:1.24.0-1.amzn2023.0.2               amazonlinux            586 k
 nginx-filesystem               noarch            1:1.24.0-1.amzn2023.0.2               amazonlinux            9.1 k
 nginx-mimetypes                noarch            2.1.49-3.amzn2023.0.3                 amazonlinux             21 k

Transaction Summary
=====================================================================================================================
Install  7 Packages

$ nginx -v
nginx version: nginx/1.24.0

3. nginxを起動する

$ sudo systemctl start nginx
$ systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
     Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; preset: disabled)
     Active: active (running) since Thu 2024-08-08 17:01:11 JST; 8s ago

EC2のパブリックIPでnginxのデフォルト画面が表示されればOKです。(セキュリティグループで80を開けていれば)

4. 自動起動設定を入れる

サーバーを再起動した時に自動で起動するように自動起動の設定を入れます。

$ sudo systemctl enable nginx
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.

$ systemctl is-enabled nginx
enabled

5. ログローテーションの設定を変更する

nginxのログは /var/log/nginx/ に出力されるので、権限等を変更します。

$ ls -l /var/log/ | grep nginx
drwx--x--x. 2 root   root                41 Aug  8 17:01 nginx
$ sudo chown -R nginx:root /var/log/nginx/
$ sudo chmod 755 /var/log/nginx/
$ ls -l /var/log/ | grep nginx
drwxr-xr-x. 2 nginx  root                41 Aug  8 17:01 nginx

ローテーションの設定を確認して必要に応じて追記します。

$ ls -l /etc/logrotate.d/ | grep nginx
-rw-r--r--. 1 root root 261 Oct 13  2023 nginx
$ sudo vi /etc/logrotate.d/nginx

/var/log/nginx/*.log /var/log/nginx/*/*.log { --(1)
    create 0664 nginx root --(2)
    daily
    dateext --(3)
    rotate 10
    missingok
    notifempty
    compress
    sharedscripts
    postrotate
        /bin/kill -USR1 `cat /run/nginx.pid 2>/dev/null` 2>/dev/null || true
    endscript
}

今回追加したのは3か所です。

  • (1) /var/log/nginx/*/*.log
    /var/log/nginx/ 配下にディレクトリを切ったので、そのログにもローテートの設定を入れるために追加しました。
  • (2) 0664
    デフォルトは0640です。
    g+wにしたのはグループがrootなのでローテートするのに権限が必要なんじゃないかと思ったので。(違ったらすいません。)
    o+rにしたのはログファイルを見るためにわざわざsudoするのがめんどくさかったので。
  • (3) dateext
    ローテート後のログファイルのファイル名に日時を入れたかったので追加しました。これを追加するとaccess.log-yyyyMMdd.gzのようになります。
  • (4) delaycompress を削除
    1つ前のログの圧縮を1回分遅らせる設定ですが、AL2には入っていない設定かつ今まで必要に感じたことがなかったので削除しました。

ログがローテートされない

設定した翌日に確認したらログがローテートされておらず、前日のログにどんどん追記されていて焦りましたが調べたら正常な挙動でした。

これは初回(設定した当日の深夜)は /var/lib/logrotate/logrotate.status に記録がなく前回の logrotate 時刻を認識できないので、まずは logrotate.status ファイルに logrotate した時刻(設定した当日の深夜)が記録されるからです。

$ ls -l /var/lib/logrotate/logrotate.status
-rw-r-----. 1 root root 482 Aug 13 00:00 /var/lib/logrotate/logrotate.status
$ sudo grep nginx /var/lib/logrotate/logrotate.status
"/var/log/nginx/error.log" 2024-8-10-0:0:13
"/var/log/nginx/*/*.log" 2024-8-9-0:0:0
"/var/log/nginx/access.log" 2024-8-10-0:0:13

なので、その次から実際にログのローテートが実施されます。なので設定した翌日の深夜からログがローテートされていれば正常です。

上記の例は8/9に初回の logrotate が走って記録され、access.logerror.log は中身があったので8/10に logrotate が実施された状態のstatusファイルです。/*/*.log が初回の8/9のままなのは、該当するファイルが存在しなかったからです。

もし初日からログをローテートしたければ /var/lib/logrotate/logrotate.status に過去日付を入れれば初回からログがローテートされると思います。

6. 設定ファイルを変更する

必要に応じて設定ファイルを変更します。

$ sudo vi /etc/nginx/nginx.conf

// 設定ファイルをいじったら必ずsyntax testをする
$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

// nginxを再起動せずに設定を反映する
$ sudo nginx -s reload

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0