Amazon Linux 2にExtrasレポジトリからNginxをインストールする

2017.12.23

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

Amazon Web Services (AWS) が提供しているAmazon Linuxに新世代のAmazon Linux 2(以下AL2)がリリースされました。 AL2 には Extras と呼ばれるレポジトリが存在し、Python、Golang、MariaDB、Ansible などの特定のパッケージをより新しいバージョンで利用することが出来ます。 今回は Extras レポジトリ経由で Nginx をインストールする方法を紹介します。

3行まとめ

  • Amazon Linux 2に Nginx をインストール
  • Extras レポジトリで提供される Nginx を利用
  • サービス管理は systemd を利用

Extras Library から Nginx をインストール

AL2 では標準で Nginx の YUM 向けパッケージが提供されていません。

$ yum search nginx
Loaded plugins: langpacks, update-motd
================================================================================ N/S matched: nginx =================================================================================
pcp-pmda-nginx.x86_64 : Performance Co-Pilot (PCP) metrics for the Nginx Webserver

  Name and summary matches only, use "search all" for everything.

かわりに、AL2 が提供する Extras レポジトリから Nginx をインストールします。

まずは提供されているトピック(=ソフトウェア群)一覧を確認してみます。

$  amazon-linux-extras
  0  ansible2   disabled  [ =2.4.2 ]
  1  emacs   disabled  [ =25.3 ]
  2  memcached1.5   disabled  [ =1.5.1 ]
  3  nginx1.12   disabled  [ =1.12.2 ]
  4  postgresql9.6   disabled  [ =9.6.6 ]
  5  python3   disabled  [ =3.6.2 ]
  6  redis4.0   disabled  [ =4.0.5 ]
  7  R3.4   disabled  [ =3.4.3 ]
  8  rust1   disabled  [ =1.22.1 ]
  9  vim   disabled  [ =8.0 ]
 10  golang1.9   disabled  [ =1.9.2 ]
 11  ruby2.4   disabled  [ =2.4.2 ]
 12  nano   disabled  [ =2.9.1 ]
 13  php7.2   disabled  [ =7.2.0 ]
 14  lamp-mariadb10.2-php7.2   disabled  [ =10.2.10_7.2.0 ]

未インストール状態のため disabled となっています

Nginx の詳細を確認します

$ amazon-linux-extras info nginx1.12
nginx1.12 recommends nginx                      # yum install nginx

Nginx をインストールします

$ sudo amazon-linux-extras install nginx1.12
  0  ansible2   disabled  [ =2.4.2 ]
  1  emacs   disabled  [ =25.3 ]
  2  memcached1.5   disabled  [ =1.5.1 ]
  3  nginx1.12=latest  enabled  [ =1.12.2 ]
  4  postgresql9.6   disabled  [ =9.6.6 ]
  5  python3   disabled  [ =3.6.2 ]
  6  redis4.0   disabled  [ =4.0.5 ]
  7  R3.4   disabled  [ =3.4.3 ]
  8  rust1   disabled  [ =1.22.1 ]
  9  vim   disabled  [ =8.0 ]
 10  golang1.9   disabled  [ =1.9.2 ]
 11  ruby2.4   disabled  [ =2.4.2 ]
 12  nano   disabled  [ =2.9.1 ]
 13  php7.2   disabled  [ =7.2.0 ]
 14  lamp-mariadb10.2-php7.2   disabled  [ =10.2.10_7.2.0 ]

先程 disabled だったステータスが enabled に変わっているのを確認出来ます。

Nginx のバージョンやモジュールを確認します

$ nginx -v
nginx version: nginx/1.12.2

$ nginx -V
nginx version: nginx/1.12.2
built by gcc 7.2.1 20170915 (Red Hat 7.2.1-2) (GCC)
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
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-file-aio --with-ipv6 --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_geoip_module=dynamic --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --with-http_perl_module=dynamic --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-google_perftools_module --with-debug --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic' --with-ld-opt='-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E'

Nginx を起動させる

旧 Amazon Linux から Amazon Linux 2の大きな違いとして systemd の導入があります。 /etc/init.d/ 配下には init スクリプトは存在しません。

/usr/lib/systemd/system 配下に関連ファイルが存在します。

systemctl コマンド経由でサービスを管理します。

Nginx プロセスの起動

systemctl start コマンドで起動します

$ sudo systemctl start nginx.service
$ sudo systemctl status nginx.service
● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: active (running) since Thu 2017-12-21 21:20:57 UTC; 6s ago
  Process: 3516 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
  Process: 3515 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
  Process: 3512 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
 Main PID: 3519 (nginx)
   CGroup: /system.slice/nginx.service
           ├─3519 nginx: master process /usr/sbin/nginx
           └─3520 nginx: worker process

Dec 21 21:20:57 ip-172-31-20-4.eu-central-1.compute.internal systemd[1]: Starting The nginx HTTP and reverse proxy server...
Dec 21 21:20:57 ip-172-31-20-4.eu-central-1.compute.internal nginx[3515]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Dec 21 21:20:57 ip-172-31-20-4.eu-central-1.compute.internal nginx[3515]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Dec 21 21:20:57 ip-172-31-20-4.eu-central-1.compute.internal systemd[1]: Failed to read PID from file /run/nginx.pid: Invalid argument
Dec 21 21:20:57 ip-172-31-20-4.eu-central-1.compute.internal systemd[1]: Started The nginx HTTP and reverse proxy server.

HEAD メソッドで Nginx にアクセスしてみます

$ curl -I localhost
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Thu, 21 Dec 2017 21:21:35 GMT
Content-Type: text/html
Content-Length: 3520
Last-Modified: Wed, 13 Dec 2017 18:36:55 GMT
Connection: keep-alive
ETag: "5a317347-dc0"
Accept-Ranges: bytes

Nginxプロセスの自動起動設定

EC2サーバー起動時にNginxプロセスも立ち上がるように設定します。

自動起動設定を確認します

# 確認方法:その1
$ systemctl is-enabled nginx.service
disabled
# 確認方法:その2
$ systemctl list-unit-files --type=service | grep nginx
nginx.service                                 disabled

無効(disabled)になっているため、有効にします

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

Nginx のユニットファイルの確認

ついでに Nginx の systemd 用ユニットファイルも確認しましょう

$ systemctl cat nginx.service
# /usr/lib/systemd/system/nginx.service
[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
# 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=/bin/kill -s HUP $MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true

[Install]
WantedBy=multi-user.target

Extras レポジトリ以外のインストール方法

Nginx の要件(バージョンAAAの機能が必要、モジュールXXXが必要、など)により、インストール方法も異なるでしょう。

EC2 上に Nginx をインストールする場合、今回紹介した以外にも、以下の様な手段があります

  • ソースコードからコンパイル
  • EPEL レポジトリからインストール
  • Nginx の提供するレポジトリを登録してインストール

詳細は次の URL をご確認下さい

https://www.nginx.com/resources/admin-guide/installing-nginx-open-source/

まとめ

今回は Amazon Linux 2から新たに導入された Extras レポジトリを利用して Nginx をインストールする方法を紹介しました。

現時点では、Extras レポジトリはパッケージの種類・バージョンなどの点でそれほど選択肢が豊富にあるわけではありませんが、EC2 の AL2 にソフトウェアをインストールする際には、Extras レポジトリを利用するという選択肢があることも、頭の片隅にとどめていただければと思います。

参照