概要
AWS EC2(AMI: Amazon Linux 2)にnginxをインストールする
環境
- AWS EC2
- OS: Amazon Linux 2
- AMI: amzn2-ami-hvm-2.0.20200304.0-x86_64-gp2
構築手順
1. Amazon Linux Extrasを確認する
Amazon Linux2にはnginxのyumが無いので、Amazon Linux Extrasでnginxを探します。
$ yum search nginx
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
31 packages excluded due to repository priority protections
============================================================ 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.
$ amazon-linux-extras list | grep nginx
38 nginx1 available [ =stable ]
$ amazon-linux-extras info nginx1
nginx1 recommends nginx # yum install nginx
2. nginxをインストールする
Amazon Linux Extrasでnginxをインストールします。
$ sudo amazon-linux-extras install nginx1
Installed:
nginx.x86_64 1:1.16.1-1.amzn2.0.1
Dependency Installed:
gd.x86_64 0:2.0.35-26.amzn2.0.2 gperftools-libs.x86_64 0:2.6.1-1.amzn2
libXpm.x86_64 0:3.5.12-1.amzn2.0.2 nginx-all-modules.noarch 1:1.16.1-1.amzn2.0.1
nginx-filesystem.noarch 1:1.16.1-1.amzn2.0.1 nginx-mod-http-geoip.x86_64 1:1.16.1-1.amzn2.0.1
nginx-mod-http-image-filter.x86_64 1:1.16.1-1.amzn2.0.1 nginx-mod-http-perl.x86_64 1:1.16.1-1.amzn2.0.1
nginx-mod-http-xslt-filter.x86_64 1:1.16.1-1.amzn2.0.1 nginx-mod-mail.x86_64 1:1.16.1-1.amzn2.0.1
nginx-mod-stream.x86_64 1:1.16.1-1.amzn2.0.1
Complete!
38 nginx1=latest enabled [ =stable ]
$ nginx -v
nginx version: nginx/1.16.1
3. nginxを起動する
nginxを起動して、次回以降はサーバー起動時に自動で起動するように設定します。
$ sudo systemctl start nginx
$ sudo systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: active (running) since Mon 2020-03-30 15:38:21 JST; 7s ago
$ sudo systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
$ systemctl is-enabled nginx
enabled
セキュリティグループで80を開放していれば、EC2のパブリックIPでnginxのデフォルト画面が表示されます。