さくらVPSで運用していたサーバを、オペレーションミスによってバルスしてしまったので
久しぶりにnginxをソースからビルドしたお話です。
今回はさくらVPSのテンプレートOSであるCentOS 7.4でビルド・構築しました。
ビルドツール準備
初期状態からソースのビルドを試みようとしているので、
ひとまずDevelopment Toolsさんに力を借りようと思います。
1 2 3 4 5 6 7 8 9 10 11 12 13 |
# yum groupinstall “Development Tools” [root@www ~]# yum groupinstall "Development Tools" Loaded plugins: fastestmirror, langpacks There is no installed groups file. Maybe run: yum groups mark convert (see man yum) Loading mirror speeds from cached hostfile * base: ftp.iij.ad.jp * epel: ftp.iij.ad.jp * extras: ftp.iij.ad.jp * updates: ftp.iij.ad.jp Warning: Group development does not have any packages to install. Maybe run: yum groups mark install (see man yum) No packages in any requested group available to install or update |
なんやて?
そのコマンド使えんて?
orz
調べてみるとCentOS 7 からyumの仕様が若干変更になった?ようなので、
groupinstallでの指定が少し変わるようです。
以下のようにコマンド実行したら正常にインストールされます。
1 2 3 4 |
# yum -y groupinstall base "Development tools" --setopt=group_package_types=mandatory,default,optional xerces-j2.noarch 0:2.11.0-17.el7_0 xml-commons-apis.noarch 0:1.4.01-16.el7 xml-commons-resolver.noarch 0:1.2-15.el7 xorg-x11-font-utils.x86_64 1:7.5-20.el7 xorg-x11-fonts-Type1.noarch 0:7.5-9.el7 xorg-x11-proto-devel.noarch 0:7.7-20.el7 zlib-devel.x86_64 0:1.2.7-17.el7 Complete! |
*参考にさせていただいたサイトはこちら
CentOS7 で yum groupinstall が出来ないんですけど。。。 | Qiita
おけおけ
インストールできたーよ
インストールが完了したら、ビルドに必要なパッケージ群を追加しましょう。
依存関係パッケージのインストール
nginxで必要になりそうなパッケージをドカドカ入れていきます。
*個人的な趣味で入れているものとかあるので、必要に応じて消してくださいね
1 2 |
# yum -y install pcre-devel openssl-devel libxslt-devel gd-devel perl-ExtUtils-Embed epel-release # yum install GeoIP-devel |
作業ディレクトリに必要なファイルをダウンロード
ビルドするものが無いと話になりませんので、
サイトからcurlで取ってきます。
1 2 3 4 5 6 7 8 9 10 |
# mkdir /tmp/nginx # cd /tmp/nginx # curl -L -O http://nginx.org/download/nginx-1.13.2.tar.gz % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 962k 100 962k 0 0 248k 0 0:00:03 0:00:03 --:--:-- 248k # curl -L -O https://www.openssl.org/source/openssl-1.0.2n.tar.gz % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 5249k 100 5249k 0 0 4628k 0 0:00:01 0:00:01 --:--:-- 4629k |
ダウンロードしたファイルを解凍
いつものコマンドで解凍しておきましょう。
1 2 3 |
# tar zxvf nginx-1.13.2.tar.gz # tar zxvf openssl-1.0.2n.tar.gz # cd nginx-1.13.2/ |
設定
./configureコマンドでビルド用の設定を反映します。
1 |
# ./configure --sbin-path=/usr/local/nginx/nginx --conf-path=/etc/nginx/nginx.conf --with-http_ssl_module --user=nginx --group=nginx --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --http-client-body-temp-path=/var/lib/nginx/body --http-proxy-temp-path=/var/lib/nginx/proxy --with-http_stub_status_module --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --with-debug --with-http_gzip_static_module --with-http_gunzip_module --with-pcre-jit --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_random_index_module --with-http_auth_request_module --with-threads --with-stream --with-stream_ssl_module --with-mail --with-mail_ssl_module --with-file-aio --with-http_v2_module --with-openssl=../openssl-1.0.2n/ |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
~~~~~~~~~~~~~~~~~~~~~~~~ checking for PCRE JIT support ... found checking for zlib library ... found creating objs/Makefile Configuration summary + using threads + using system PCRE library + using OpenSSL library: ../openssl-1.0.2n/ + using system zlib library nginx path prefix: "/usr/local/nginx" nginx binary file: "/usr/local/nginx/nginx" nginx modules path: "/usr/local/nginx/modules" nginx configuration prefix: "/etc/nginx" nginx configuration file: "/etc/nginx/nginx.conf" nginx pid file: "/usr/local/nginx/logs/nginx.pid" nginx error log file: "/var/log/nginx/error.log" nginx http access log file: "/var/log/nginx/access.log" nginx http client request body temporary files: "/var/lib/nginx/body" nginx http proxy temporary files: "/var/lib/nginx/proxy" nginx http fastcgi temporary files: "/var/lib/nginx/fastcgi" nginx http uwsgi temporary files: "uwsgi_temp" nginx http scgi temporary files: "scgi_temp" |
上記のような出力がされて入れば問題ないはず!
次は、お待ちかねのビルドタイムですよ〜
ビルド!
以下のコマンドでビルドを開始します。
1 |
# make |
1 2 3 4 5 6 7 |
-Wl,-E sed -e "s|%%PREFIX%%|/usr/local/nginx|" \ -e "s|%%PID_PATH%%|/usr/local/nginx/logs/nginx.pid|" \ -e "s|%%CONF_PATH%%|/etc/nginx/nginx.conf|" \ -e "s|%%ERROR_LOG_PATH%%|/var/log/nginx/error.log|" \ < man/nginx.8 > objs/nginx.8 make[1]: Leaving directory `/tmp/nginx/nginx-1.13.2' |
上記のように、特別エラーもでなければ無事にビルドできてます。
お疲れ様でした。
さて、起動する前に他のデーモンなどでポートが使用されていないことを確認しておきましょう。
80/tcp,443/tcpが使用されていなければとりあえず問題ないはずです。
1 2 3 |
# ss -ln | grep tcp tcp LISTEN 0 128 *:111 *:* tcp LISTEN 0 100 127.0.0.1:25 *:* |
特に問題なさそうですね。
じゃあ起動しちゃいましょー
Usergroup Useradd
ポートの占有確認も終えたので、
早速デーモン起動といきましょう。
と、その前にここでもう一手間加えておきましょう。
自身でビルドしてインストールした場合、
パッケージインストールとは違ってコマンドパスをフル指定しなければなりません。
実行時に毎度指定するのは大変なので、.bashrcの環境変数に出力されるようにしておきましょう。
1 2 |
# vim ~/.bashrc export PATH="$PATH:/usr/local/nginx" |
保存したら以下のコマンドで.bashrcを再読み込みし、
環境変数を有効化しましょう。
1 |
# source ~/.bashrc |
nginxを起動する際にプロセスを特定のユーザ権限で起動するようにしたいところです。
標準設定のままでは特にそのようなものはないので、以下のコマンドでnginxユーザを設定しておきましょう。
1 2 3 |
# groupadd nginx # useradd -g nginx nginx # usermod -s /bin/false nginx |
1 2 |
# vim /etc/nginx/nginx.conf user nginx nginx; |
起動!
起動した時に以下のようなエラーメッセージが出て来ました。
1 2 |
# nginx nginx: [emerg] mkdir() "/var/lib/nginx/body" failed (2: No such file or directory) |
意訳:/var/lib/nginxなんてフォルダはねーよ
作成してからもう一回出直してこい
らいしいので、作成してから出直します。
1 |
# mkdir /var/lib/nginx |
nginx 今度こそ起動!
さてさて、お待ちかねの起動タァァァン!!!
うるさかったですね、すみません。。。
1 |
# nginx |
起動だけならこのコマンドのみで完了です。
やったね!
特にエラーも出なかったよ!
なお、稀に以下のようなエラーメッセージが出る場合もあるようです。
1 |
nginx: [emerg] getpwnam("nginx") failed |
調べてみると、nginx.conf内のユーザ指定がない場合や、
そもそもユーザとグループが作成されていない場合に起きることがあるみたいです。
上記の手順でも実施していますので、このエラーが出るとは思えないですが。。。
と、思っていたのですが、何度かソースビルドをしていて気づきました。
ビルド完了後のnginx.confにはuserの指定がないようですね。。。
#user nobody;
こんな指定ならあったので、おそらく明示的にuser指定されていないため、
起動時にエラーを吐いていたという結論でしょうか。
既にWebサーバを運用中の場合にも注意が必要
すでに別のサーバが起動している場合も注意したいですね。
Apacheを運用している場合などで、かつ80番を利用していた場合、
以下のようなエラーが表示されるようです。
1 |
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) |
読んで字のごとくですが、nginxのデフォルトポートと被るために
発生するエラーのようです。
nginx.conf側でポート番号を調整するか、
別デーモンの設定を変更しましょう。
nginxの起動を阻害する可能性のあるデーモン
今回構築しているサーバはCentOS 7であるため、
標準でfirewalldというデーモンが起動しています。
基本的な設定であれば、80/tcpや443/tcpはクローズしたままになっています。
ですから、以下のコマンドでポート開放の設定をしましょう。
1 2 3 |
# firewall-cmd --zone=public --permanent --add-port=80/tcp # firewall-cmd --zone=public --permanent --add-port=443/tcp # firewall-cmd --reload |
firewallがポートをきちんと開けてくれているか確認もしておきます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
# firewall-cmd --list-all public (active) target: default icmp-block-inversion: no interfaces: eth0 sources: services: dhcpv6-client ssh ports: 80/tcp 443/tcp protocols: masquerade: no forward-ports: source-ports: icmp-blocks: rich rules: |
確認のため先ほどのポート確認コマンドでオープンしているか見てみましょう。
1 |
# ss -ln | grep tcp |
今回はHTTP/2に対応したnginxをビルドして終了しました。
次回HTTP/2を有効化する設定やSSL化に対応する手順なども交えて解説します。
関連記事
CentOS 7.4でnginxにhttp/2とHTTPS対応設定をする
CentOS 7 firewalldを無効化する手順