🐧

AmazonLinux2のnginxを1.24.0にバージョンアップ(導入)

2023/09/14に公開

AmazonLinux2のnginxのを1.24.0にバージョンアップ

はじめに

初めまして。アイディオットDX開発部、インフラ担当の小野です。
EC2を使っていて、AmazonLinux2のnginxのバージョンを1.24に上げる対応をした際にハマったので
同じように苦しんでいる方の助けになればと思い記します。

前提

  • AmazonLinux2でサーバを起動していること
  • 9/14現在、nginxのStable版である1.24を導入することとする

yum を利用したnginxのバージョンアップ

リポジトリを追加してyumでインストールする方法では、色々と頑張ってみましたがnginx1.22.0までしか入りませんでした。

nginxをソースコードからコンパイルしてインストールする

yumが不可能ならソースコードからコンパイルしてインストールしましょう。

手順

※rootユーザで実行しています。

  • 実行しているnginxを停止する
systemctl stop nginx
  • 既存のnginxの設定を使いたい場合はbackupを作成する(例)
cd /etc
cp -Rp nginx nginx.bk
  • nginxパッケージのアンインストール
yum remove nginx
  • nginxのインストール
    wgetを使ってnginxのソースコードをダウンロードします。 nginxの公式サイトからダウンロードできます
    ※バージョンは希望のものを公式サイトより選択ください
cd /usr/local/src
sudo wget https://nginx.org/download/nginx-1.24.0.tar.gz
sudo tar xvzf nginx-1.24.0.tar.gz
  • configureスクリプトファイルを実行
cd /usr/local/src/nginx-1.24.0
./configure
  • ./configure実行時のエラー対応

Cコンパイラが足りないときのエラー

[root@ip-xxxx.xx.xx.x nginx-1.24.0]# ./configure
checking for OS
 + Linux 5.10.192-182.736.amzn2.x86_64 x86_64
checking for C compiler ... not found

./configure: error: C compiler cc is not found

以下コマンドを実行

sudo yum install gcc gcc-c++ make

PCRE(Perl Compatible Regular Expressions)ライブラリに依存するエラー

./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.

以下コマンドを実行

yum install pcre pcre-devel

gzipモジュールがzlibライブラリに依存していると指摘された場合のエラー

./configure: error: the HTTP gzip module requires the zlib library.
You can either disable the module by using --without-http_gzip_module
option, or install the zlib library into the system, or build the zlib library
statically from the source with nginx by using --with-zlib=<path> option.

以下コマンドを実行

yum install zlib zlib-devel
  • コンパイルしてインストール
make
make install
  • 既存のnginxの設定を使用したい場合はbackupから戻す
cd /etc/
cp -Rp nginx.bk nginx
  • systemdにサービスを登録
    Linuxの起動処理やシステム管理を行うsystemdの起動スクリプトを生成してsystemctl start nginx みたいな感じで起動できるようにする
vim /usr/lib/systemd/system/nginx.service
  • 以下の通り記述し、保存(既存のnginx設定を使用したい場合は次項目を参考)
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target
  • 既存のnginx設定を使用する場合の記述例(nginxアンインストール前のご自身の設定を参考に編集してください)
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/bin/rm -f /run/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /etc/nginx/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true

[Install]
WantedBy=multi-user.target
  • 上記のポイント
    以下のように、-t -cコマンドなどを使用して場所を指定する
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /etc/nginx/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /etc/nginx/nginx.conf
  • パスを通す
vim /etc/profile.d/nginx.sh
  • 以下の通り記述し、保存
export PATH=$PATH:/usr/local/nginx/sbin
  • すぐさま反映させたい場合(ユーザごとに実行する必要がある)
    ※ssh接続し直す場合は必要なし
source /etc/profile
  • nginxの起動
systemctl start nginx
systemctl enable nginx
  • version確認
nginx -v

まとめ

AmazonLinux2に入れれることができるyumでの限界nginx1.22
これだとEOLに達しているのであまりよろしくないですね。
はやくAmazonLinux2023にあげろーということでしょうか笑
同じようにAmazonLinux2のnginxバージョンを1.24に上げたい・導入したいという方の
参考になれましたら幸いです。

あとがき

AI・データ利活用をリードし、世界にインパクトを与えるプロダクトを開発しませんか?

アイディオットでは、今後の事業拡大及びプロダクト開発を担っていただけるエンジニアチームの強化を行っております。
さらに会社の成長を加速させるため、フロントエンドエンジニア、バックエンドエンジニア、インフラエンジニアのメンバーを募集しております!
日本を代表する企業様へ自社プロダクトを活用した、新規事業コンサルティング、開発にご興味のある方はお気軽にご連絡ください。

【リクルートページ】
https://aidiot.jp/recruit/
【募集ポジション一覧】
https://open.talentio.com/r/1/c/aidiot/homes/3925
【採用についてのお問合せ先】
株式会社アイディオット 採用担当:大島
メールアドレス:recruit@aidiot.jp

今回の記事の参考サイト

Discussion