EC2にnginxをインストールする方法

#はじめに
EC2にNginxをインストールしたときのメモ

##nginxをインストールする
```
$sudo amazon-linux-extras install nginx1
Installing nginx
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
Cleaning repos: amzn2-core amzn2extra-docker amzn2extra-kernel-5.10
              : amzn2extra-nginx1
17 metadata files removed
6 sqlite files removed
0 metadata files removed
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
amzn2-core                                               | 3.7 kB     00:00
amzn2extra-docker                                        | 3.0 kB     00:00
amzn2extra-kernel-5.10                                   | 3.0 kB     00:00
amzn2extra-nginx1                                        | 3.0 kB     00:00
(1/9): amzn2-core/2/x86_64/group_gz                        | 2.5 kB   00:00
(2/9): amzn2-core/2/x86_64/updateinfo                      | 445 kB   00:00
(3/9): amzn2extra-kernel-5.10/2/x86_64/primary_db          | 6.9 MB   00:00
(4/9): amzn2extra-docker/2/x86_64/updateinfo               | 4.7 kB   00:00
(5/9): amzn2extra-kernel-5.10/2/x86_64/updateinfo          |  11 kB   00:00
(6/9): amzn2extra-docker/2/x86_64/primary_db               |  83 kB   00:00
(7/9): amzn2extra-nginx1/2/x86_64/updateinfo               |   76 B   00:00
(8/9): amzn2extra-nginx1/2/x86_64/primary_db               |  42 kB   00:00
(9/9): amzn2-core/2/x86_64/primary_db                      |  60 MB   00:01
Resolving Dependencies
--> Running transaction check
---> Package nginx.x86_64 1:1.20.0-2.amzn2.0.4 will be installed
--> Processing Dependency: nginx-filesystem = 1:1.20.0-2.amzn2.0.4 for package: 1:nginx-1.20.0-2.amzn2.0.4.x86_64
--> Processing Dependency: nginx-filesystem for package: 1:nginx-1.20.0-2.amzn2.0.4.x86_64
--> Processing Dependency: libssl.so.1.1(OPENSSL_1_1_1)(64bit) for package: 1:nginx-1.20.0-2.amzn2.0.4.x86_64
--> Processing Dependency: libssl.so.1.1(OPENSSL_1_1_0)(64bit) for package: 1:nginx-1.20.0-2.amzn2.0.4.x86_64
--> Processing Dependency: libcrypto.so.1.1(OPENSSL_1_1_0)(64bit) for package: 1:nginx-1.20.0-2.amzn2.0.4.x86_64
--> Processing Dependency: libssl.so.1.1()(64bit) for package: 1:nginx-1.20.0-2.amzn2.0.4.x86_64
--> Processing Dependency: libprofiler.so.0()(64bit) for package: 1:nginx-1.20.0-2.amzn2.0.4.x86_64
--> Processing Dependency: libcrypto.so.1.1()(64bit) for package: 1:nginx-1.20.0-2.amzn2.0.4.x86_64
--> Running transaction check
---> Package gperftools-libs.x86_64 0:2.6.1-1.amzn2 will be installed
---> Package nginx-filesystem.noarch 1:1.20.0-2.amzn2.0.4 will be installed
---> Package openssl11-libs.x86_64 1:1.1.1g-12.amzn2.0.4 will be installed
--> Processing Dependency: openssl11-pkcs11 for package: 1:openssl11-libs-1.1.1g-12.amzn2.0.4.x86_64
--> Running transaction check
---> Package openssl11-pkcs11.x86_64 0:0.4.10-6.amzn2.0.1 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package            Arch     Version                  Repository           Size
================================================================================
Installing:
 nginx              x86_64   1:1.20.0-2.amzn2.0.4     amzn2extra-nginx1   579 k
Installing for dependencies:
 gperftools-libs    x86_64   2.6.1-1.amzn2            amzn2-core          274 k
 nginx-filesystem   noarch   1:1.20.0-2.amzn2.0.4     amzn2extra-nginx1    23 k
 openssl11-libs     x86_64   1:1.1.1g-12.amzn2.0.4    amzn2-core          1.4 M
 openssl11-pkcs11   x86_64   0.4.10-6.amzn2.0.1       amzn2-core           61 k

Transaction Summary
================================================================================
Install  1 Package (+4 Dependent packages)

Total download size: 2.3 M
Installed size: 6.6 M
Is this ok [y/d/N]: y
Downloading packages:
(1/5): gperftools-libs-2.6.1-1.amzn2.x86_64.rpm            | 274 kB   00:00
(2/5): nginx-1.20.0-2.amzn2.0.4.x86_64.rpm                 | 579 kB   00:00
(3/5): openssl11-pkcs11-0.4.10-6.amzn2.0.1.x86_64.rpm      |  61 kB   00:00
(4/5): nginx-filesystem-1.20.0-2.amzn2.0.4.noarch.rpm      |  23 kB   00:00
(5/5): openssl11-libs-1.1.1g-12.amzn2.0.4.x86_64.rpm       | 1.4 MB   00:00
--------------------------------------------------------------------------------
Total                                              5.8 MB/s | 2.3 MB  00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : 1:openssl11-libs-1.1.1g-12.amzn2.0.4.x86_64                  1/5
  Installing : openssl11-pkcs11-0.4.10-6.amzn2.0.1.x86_64                   2/5
  Installing : gperftools-libs-2.6.1-1.amzn2.x86_64                         3/5
  Installing : 1:nginx-filesystem-1.20.0-2.amzn2.0.4.noarch                 4/5
  Installing : 1:nginx-1.20.0-2.amzn2.0.4.x86_64                            5/5
  Verifying  : openssl11-pkcs11-0.4.10-6.amzn2.0.1.x86_64                   1/5
  Verifying  : 1:nginx-1.20.0-2.amzn2.0.4.x86_64                            2/5
  Verifying  : 1:nginx-filesystem-1.20.0-2.amzn2.0.4.noarch                 3/5
  Verifying  : 1:openssl11-libs-1.1.1g-12.amzn2.0.4.x86_64                  4/5
  Verifying  : gperftools-libs-2.6.1-1.amzn2.x86_64                         5/5

Installed:
  nginx.x86_64 1:1.20.0-2.amzn2.0.4

Dependency Installed:
  gperftools-libs.x86_64 0:2.6.1-1.amzn2
  nginx-filesystem.noarch 1:1.20.0-2.amzn2.0.4
  openssl11-libs.x86_64 1:1.1.1g-12.amzn2.0.4
  openssl11-pkcs11.x86_64 0:0.4.10-6.amzn2.0.1

Complete!
  0  ansible2                 available    \
        [ =2.4.2  =2.4.6  =2.8  =stable ]
  2  httpd_modules            available    [ =1.0  =stable ]
  3  memcached1.5             available    \
        [ =1.5.1  =1.5.16  =1.5.17 ]
  5  postgresql9.6            available    \
        [ =9.6.6  =9.6.8  =stable ]
  6  postgresql10             available    [ =10  =stable ]
  9  R3.4                     available    [ =3.4.3  =stable ]
 10  rust1                    available    \
        [ =1.22.1  =1.26.0  =1.26.1  =1.27.2  =1.31.0  =1.38.0
          =stable ]
 11  vim                      available    [ =8.0  =stable ]
 15  php7.2                   available    \
        [ =7.2.0  =7.2.4  =7.2.5  =7.2.8  =7.2.11  =7.2.13  =7.2.14
          =7.2.16  =7.2.17  =7.2.19  =7.2.21  =7.2.22  =7.2.23
          =7.2.24  =7.2.26  =stable ]
 17  lamp-mariadb10.2-php7.2  available    \
        [ =10.2.10_7.2.0  =10.2.10_7.2.4  =10.2.10_7.2.5
          =10.2.10_7.2.8  =10.2.10_7.2.11  =10.2.10_7.2.13
          =10.2.10_7.2.14  =10.2.10_7.2.16  =10.2.10_7.2.17
          =10.2.10_7.2.19  =10.2.10_7.2.22  =10.2.10_7.2.23
          =10.2.10_7.2.24  =stable ]
 18  libreoffice              available    \
        [ =5.0.6.2_15  =5.3.6.1  =stable ]
 19  gimp                     available    [ =2.8.22 ]
 20  docker=latest            enabled      \
        [ =17.12.1  =18.03.1  =18.06.1  =18.09.9  =stable ]
 21  mate-desktop1.x          available    \
        [ =1.19.0  =1.20.0  =stable ]
 22  GraphicsMagick1.3        available    \
        [ =1.3.29  =1.3.32  =1.3.34  =stable ]
 23  tomcat8.5                available    \
        [ =8.5.31  =8.5.32  =8.5.38  =8.5.40  =8.5.42  =8.5.50
          =stable ]
 24  epel                     available    [ =7.11  =stable ]
 25  testing                  available    [ =1.0  =stable ]
 26  ecs                      available    [ =stable ]
 27  corretto8                available    \
        [ =1.8.0_192  =1.8.0_202  =1.8.0_212  =1.8.0_222  =1.8.0_232
          =1.8.0_242  =stable ]
 28  firecracker              available    [ =0.11  =stable ]
 29  golang1.11               available    \
        [ =1.11.3  =1.11.11  =1.11.13  =stable ]
 30  squid4                   available    [ =4  =stable ]
 31  php7.3                   available    \
        [ =7.3.2  =7.3.3  =7.3.4  =7.3.6  =7.3.8  =7.3.9  =7.3.10
          =7.3.11  =7.3.13  =stable ]
 32  lustre2.10               available    \
        [ =2.10.5  =2.10.8  =stable ]
 33  java-openjdk11           available    [ =11  =stable ]
 34  lynis                    available    [ =stable ]
 35  kernel-ng                available    [ =stable ]
 36  BCC                      available    [ =0.x  =stable ]
 37  mono                     available    [ =5.x  =stable ]
 38  nginx1=latest            enabled      [ =stable ]
 39  ruby2.6                  available    [ =2.6  =stable ]
 40  mock                     available    [ =stable ]
 41  postgresql11             available    [ =11  =stable ]
 42  php7.4                   available    [ =stable ]
 43  livepatch                available    [ =stable ]
 44  python3.8                available    [ =stable ]
 45  haproxy2                 available    [ =stable ]
 46  collectd                 available    [ =stable ]
 47  aws-nitro-enclaves-cli   available    [ =stable ]
 48  R4                       available    [ =stable ]
  _  kernel-5.4               available    [ =stable ]
 50  selinux-ng               available    [ =stable ]
 51  php8.0                   available    [ =stable ]
 52  tomcat9                  available    [ =stable ]
 53  unbound1.13              available    [ =stable ]
 54  mariadb10.5              available    [ =stable ]
 55  kernel-5.10=latest       enabled      [ =stable ]
 56  redis6                   available    [ =stable ]
 57  ruby3.0                  available    [ =stable ]
 58  postgresql12             available    [ =stable ]
 59  postgresql13             available    [ =stable ]
 60  mock2                    available    [ =stable ]
 61  dnsmasq2.85              available    [ =stable ]
```

##インストール後の確認

```
$nginx -v
nginx version: nginx/1.20.0
```

##nginxを起動する
```
$sudo systemctl start nginx
```
##nginxの状態を確認する
```
$sudo systemctl status nginx
● 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 Sat 2022-02-19 08:05:43 UTC; 6min ago
 Main PID: 3511 (nginx)
   CGroup: /system.slice/nginx.service
           tq3511 nginx: master process /usr/sbin/nginx
           mq3512 nginx: worker process

Feb 19 08:05:43 ip-172-31-81-136.ec2.internal systemd[1]: Starting The nginx ...
Feb 19 08:05:43 ip-172-31-81-136.ec2.internal nginx[3505]: nginx: the configu...
Feb 19 08:05:43 ip-172-31-81-136.ec2.internal nginx[3505]: nginx: configurati...
Feb 19 08:05:43 ip-172-31-81-136.ec2.internal systemd[1]: Started The nginx H...
Hint: Some lines were ellipsized, use -l to show in full.
```
##マシン起動時のnginx起動を有効化する
```
$sudo systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
```

##ブラウザでアクセスする
ブラウザのアドレスバーにEC2のパブリックIPアドレスを入力します。
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/321513/c509706b-c4b7-2580-b190-28382464e062.png)

##EC2のインバウンドルールにHTTPを追加して再度アクセスする。
ブラウザのアドレスバーにEC2のパブリックIPアドレスを入力します。
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/321513/def9c589-9e69-190b-6b7e-170dbbdac71c.png)

#おわりに
EC2にnginxをインストールしたときのメモは以上です。
では、また。