CentOS Stream 10でNginxをソースからビルド
FreeBSDに合わせてLinuxもGeoIPモジュールとVTSモジュールを使いたいとNginxをソールからビルドしたときの覚書。
Gemini 2.5 Proと対話。
環境: CentOS Stream 10(開発環境)
1. GeoIPを無料で使えるウェブサーバー
NginxからフォークしたAngieはGeoIP2をdnf経由でインストールできるらしい。
参考: Welcome! — Angie Software
LiteSpeedもGeoIPを使えるらしい。
参考: LiteSpeed Web Server - Apache Alternative - LiteSpeed Technologies
lighttpd(ライティ)はdnfで検索すると「lighttpd-mod_maxminddb」がEPELリポジトリにあるので使えそう。
NginxはGeoIP2をdnf経由でインストールするには有料版のNginx Plusを購入する必要がある。
ソースからビルドすればGeoIP2を無料で使える。
2. Nginxのソースをダウンロードしてビルド
dnfでインストールしたNginxのビルドオプションを確認。
改行して見やすくする。
# nginx -V 2>&1 | sed 's/ --/\n--/g'
nginx version: nginx/1.26.3built with OpenSSL 3.2.2 4 Jun 2024 (running with OpenSSL 3.5.1 1 Jul 2025)TLS SNI support enabledconfigure 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_v3_module--with-http_xslt_module=dynamic--with-mail=dynamic--with-mail_ssl_module--with-openssl-opt=enable-ktls--without-engine--with-pcre--with-pcre-jit--with-stream=dynamic--with-stream_realip_module--with-stream_ssl_module--with-stream_ssl_preread_module--with-threads
必要なライブラリをインストール。
参考: nginx/nginx: The official NGINX Open Source repository.
# dnf install gcc make cmake pcre2-devel openssl-devel libmaxminddb-devel
最新の安定版をダウンロードする。
参考: nginx: download
# cd /usr/local/src/
# curl -O https://nginx.org/download/nginx-1.28.0.tar.gz
# tar xzvf nginx-1.28.0.tar.gz
# rm -f nginx-1.28.0.tar.gz
# cd nginx-1.28.0/
サードパーティ製モジュールのソースコードをgitでクローン。
--depth 1 を付けることで、最新のコミット1つ分のみを取得。
Brotli (ngx_brotli)
ビルドに失敗するので含めないようにした。# git clone --depth 1 https://github.com/google/ngx_brotli.git
# cd ngx_brotli/
# git submodule update --init
# cd ../
GeoIP2 (ngx_http_geoip2_module)
# git clone --depth 1 https://github.com/leev/ngx_http_geoip2_module.git
VTS (nginx-module-vts)
# git clone --depth 1 https://github.com/vozlt/nginx-module-vts.git
Cache Purge (ngx_cache_purge)
# git clone --depth 1 https://github.com/nginx-modules/ngx_cache_purge.git
configureを実行。
参考: Building nginx from Sources
# ./configure \--prefix=/etc/nginx \--conf-path=/etc/nginx/nginx.conf \--sbin-path=/usr/sbin/nginx \--pid-path=/run/nginx.pid \--lock-path=/run/lock/subsys/nginx \--error-log-path=/var/log/nginx/error.log \--http-log-path=/var/log/nginx/access.log \--user=nginx \--group=www \--with-compat \--with-pcre \--with-file-aio \--http-client-body-temp-path=/var/lib/nginx/tmp/client_body_temp \--http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi_temp \--http-proxy-temp-path=/var/lib/nginx/tmp/proxy_temp \--http-scgi-temp-path=/var/lib/nginx/tmp/scgi_temp \--http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi_temp \--with-http_v2_module \--with-http_gunzip_module \--with-http_gzip_static_module \--with-http_realip_module \--with-http_slice_module \--with-http_ssl_module \--with-http_stub_status_module \--with-http_sub_module \--without-mail_imap_module \--without-mail_pop3_module \--without-mail_smtp_module \--with-threads \--add-module=./ngx_brotli \--add-module=./ngx_http_geoip2_module \--add-module=./nginx-module-vts \--add-module=./ngx_cache_purge
ビルドだけ実行。
# make
エラーになったらGemini先生に報告して指示に従う。
# make clean
# make
ビルド後のバージョン確認とテスト実行。
# objs/nginx -V
# objs/nginx -t
dnfでインストールしたNginxと置き換える
今起動しているNginxサービスファイルの場所を確認。
# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy serverLoaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; preset: disabled)Drop-In: /etc/systemd/system/nginx.service.d└─php-fpm.conf
サービスファイルを確認してコピーしておく。
/usr/lib/systemd/system/はdnfパッケージマネージャー用。
# less /usr/lib/systemd/system/nginx.service
[Unit]Description=The nginx HTTP and reverse proxy serverAfter=network-online.target remote-fs.target nss-lookup.targetWants=network-online.target[Service]Type=forkingPIDFile=/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=1268621ExecStartPre=/usr/bin/rm -f /run/nginx.pidExecStartPre=/usr/sbin/nginx -tExecStart=/usr/sbin/nginxExecReload=/usr/sbin/nginx -s reloadKillSignal=SIGQUITTimeoutStopSec=5KillMode=mixedPrivateTmp=true[Install]WantedBy=multi-user.target
nginx.confをバックアップ(dnf remove nginxすると削除されるので)。
# cd /etc/nginx/
# cp nginx.conf nginx.conf.bak20250907
dnf経由でインストールしたNginxをアンインストール。
# dnf remove nginx
ビルドしたNginxをインストール。
# cd /usr/local/src/nginx-1.28.0/
# make install
確認。
# nginx -V
設定ファイルを元に戻してテスト。
# cd /etc/nginx/
# cp nginx.conf.bak20250907 nginx.conf
# nginx -t
サービスファイルの作成。
dnfでインストールしたサービスファイルと同じにする。
/etc/systemd/system/はシステム管理者用(/usr/lib/systemd/system/を直接編集するのは推奨されない)。
# vi /etc/systemd/system/nginx.service
systemdのリロードとサービスの有効化。
# systemctl daemon-reload
# systemctl enable nginx
# systemctl start nginx
# systemctl status nginx
ブラウザで動作確認する。