Nginxをソースコードからビルドしてみた

CentOS Stream 9の環境において、dnfコマンドでインストールされるNginxは私が必要としているオプションが無効になった状態でビルドされたものでした。
設定ファイルでどうこうできるものではなかったのでソースコードからビルドしてみました。

目次

ソースコードの取得

NginxにはStable版とMainline版があります。
Stable版は本番環境用の安定版、Mainline版は新機能やバグフィックスの提供が早い最新版という位置付けのようです。

今回はMainline版を取得しました。

オプションの確認

デフォルト状態でビルドすると何が有効になって何が無効になるのかが分かりにくいのでdnfコマンドでインストールしたnginxのオプションを確認します。

nginx -V

「configure arguments」の部分を参考にしてオプションを追加もしくは削除してビルドします。
尚、当方の環境では以下のように表示されました。

configure arguments: --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-compat --with-debug --with-file-aio --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_degradation_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_mp4_module --with-http_perl_module=dynamic --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-http_xslt_module=dynamic --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-stream_ssl_preread_module --with-threads --with-cc-opt='-O2 -flto=auto -ffat-lto-objects -fexceptions -g -grecord-gcc-switches -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -fstack-protector-strong -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -march=x86-64-v2 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection' --with-ld-opt='-Wl,-z,relro -Wl,--as-needed -Wl,-z,now -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -Wl,-E'

Makefileの作成

上記でダウンロードしたソースファイルを展開し、展開されたディレクトリに入ります。
configureスクリプトを使ってMakefileを作成しますが、その際にオプションとして上記のconfigure argumentを参考にしたパラメータを付与します。

./configure {パラメータ}

「C compiler cc is not found」というエラーメッセージが表示された場合は「gcc」をインストールします。
また、ライブラリが足りないと途中で止まります。
当方の環境では以下のライブラリをインストールする必要がありました。

  • pcre-devel
  • zlib-devel
  • rpm-build
  • libxml2-devel
  • libxslt-devel
  • gd-devel
  • perl-ExtUtils-Embed
  • openssl-devel

ビルド

Makefileが作成されたらビルドします。

make

エラーメッセージが表示されていなければビルド完了です。

インストール

ビルドが正常に終わったらインストールします。
インストール先はconfigureスクリプトを実行する際に指定した「ほにゃららpath」系のディレクトリになります。
dnfコマンドでインストールしたNginxがある場合、コンフリクトしてしまう可能性があるので事前にアンインストールしておいてください。
また、その際にディレクトリなどが削除されますので必要に応じて設定ファイルのバックアップを取得しておいてください。

sudo make install

これでインストール完了です。

systemd用設定ファイルの作成

systemctlコマンドで起動や停止ができるように設定ファイルを作成します。
「/usr/lib/systemd/system」ディレクトリに「nginx.service」として、以下の内容で作成します。

nginx.service

[Unit]
Description=The nginx HTTP and reverse proxy server
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/run/nginx.pid
# Nginx will fail to start if /run/nginx.pid already exists but has the wrong
# SELinux context. This might happen when running `nginx -t` from the cmdline.
# https://bugzilla.redhat.com/show_bug.cgi?id=1268621
ExecStartPre=/usr/bin/rm -f /run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/usr/sbin/nginx -s reload
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=mixed
PrivateTmp=true

[Install]
WantedBy=multi-user.target

設定ファイルのリロードをします。

sudo systemctl daemon-reload

以上でsystemctlコマンドによる制御が可能になります。


まとめ

以上でNginxのビルド&インストールは完了です。
あとは必要に応じて各種設定ファイルを修正してください。

アップデートの際にはまたビルドから行う必要があるので若干手間はかかりますが、必要に応じてオプションを決められるので便利だと思います。

また、ソースコードからビルドするのなら標準状態では無効になっているBrotli圧縮を使えるようにするのもいいかと思います。
よろしければこちらの記事もご覧ください。

この記事が気に入ったら
フォローしてね!

  • URLをコピーしました!
  • URLをコピーしました!

コメント

コメントする

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください

目次