🔥

【AWS】【EC2】Nginxをインストールしよう

に公開

EC2 を WEB サーバーとして運用するためにNginx をインストールしてみます

ブラウザから EC2 へアクセスできるようになります

前提

EC2 インスタンスを作成していることが前提になります。

もし、作成していない場合は、下記を参考に作成してみてください

https://zenn.dev/kuuki/articles/aws-create-ec2-server/

Nginx のインストール

EC2 へ SSH 接続します(筆者は ec2-user を使用します) → sudo を使用して、いくつかのコマンドを実行しています

方法がわからない方は以下を参考にしてみてください

https://zenn.dev/kuuki/articles/aws-ssh-ec2-server/

yum ではなく、amazon-linux-extras コマンドを使用してインストールしていきます

amazon-linux-extras コマンドの確認

which コマンドで amazon-linux-extras コマンドのパスを表示します

(実行コマンド)

which amazon-linux-extras

(実行結果)

[ec2-user@ip-×××-××-××-×× ~]$ which amazon-linux-extras
/usr/bin/amazon-linux-extras

もし、エラーが出た場合は、amazon-linux-extras コマンドをインストールしましょう

(実行コマンド)

sudo yum install amazon-linux-extras

パッケージの確認

(実行コマンド)

amazon-linux-extras list | grep nginx

(実行結果)

[ec2-user@ip-×××-××-××-×× ~]$ amazon-linux-extras list | grep nginx
 38  nginx1                   available    [ =stable ]

nginx1という名前で登録されているので、これをインストールします

インストール

(実行コマンド)

sudo amazon-linux-extras install nginx1

Is this ok [y/d/N]: とでたら、yと入力してインストールします

(実行結果)

[ec2-user@ip-×××-××-××-×× ~]$ 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
(略)

===========================================================================================================================================================
 Package                               Arch                        Version                                    Repository                              Size
===========================================================================================================================================================
Installing:
 nginx                                 x86_64                      1:1.20.0-2.amzn2.0.5                       amzn2extra-nginx1                      580 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.5                       amzn2extra-nginx1                       23 k
 openssl11-libs                        x86_64                      1:1.1.1g-12.amzn2.0.9                      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): nginx-filesystem-1.20.0-2.amzn2.0.5.noarch.rpm                                                                               |  23 kB  00:00:00
(2/5): gperftools-libs-2.6.1-1.amzn2.x86_64.rpm                                                                                     | 274 kB  00:00:00
(3/5): nginx-1.20.0-2.amzn2.0.5.x86_64.rpm                                                                                          | 580 kB  00:00:00
(4/5): openssl11-pkcs11-0.4.10-6.amzn2.0.1.x86_64.rpm                                                                               |  61 kB  00:00:00
(5/5): openssl11-libs-1.1.1g-12.amzn2.0.9.x86_64.rpm                                                                                | 1.4 MB  00:00:00
-----------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                      8.1 MB/s | 2.3 MB  00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : 1:openssl11-libs-1.1.1g-12.amzn2.0.9.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.5.noarch                                                                                            4/5
  Installing : 1:nginx-1.20.0-2.amzn2.0.5.x86_64                                                                                                       5/5
  Verifying  : 1:nginx-1.20.0-2.amzn2.0.5.x86_64                                                                                                       1/5
  Verifying  : openssl11-pkcs11-0.4.10-6.amzn2.0.1.x86_64                                                                                              2/5
  Verifying  : 1:nginx-filesystem-1.20.0-2.amzn2.0.5.noarch                                                                                            3/5
  Verifying  : gperftools-libs-2.6.1-1.amzn2.x86_64                                                                                                    4/5
  Verifying  : 1:openssl11-libs-1.1.1g-12.amzn2.0.9.x86_64                                                                                             5/5

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

Dependency Installed:
  gperftools-libs.x86_64 0:2.6.1-1.amzn2             nginx-filesystem.noarch 1:1.20.0-2.amzn2.0.5       openssl11-libs.x86_64 1:1.1.1g-12.amzn2.0.9
  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 ]
(略)

インストールの確認

(実行コマンド)

nginx -v

(実行結果)

[ec2-user@ip-×××-××-××-×× ~]$ nginx -v
nginx version: nginx/1.20.0

Nginx のバージョンが表示されれば、インストールできています

起動&自動起動設定をする

  1. Nginx を起動
    sudo systemctl start nginx
    
  2. 起動確認
    sudo systemctl status nginx
    
    Active: active (running) と表示されれば OK!
  3. インスタンスの起動と一緒に Nginx が起動するよう設定
    sudo systemctl enable nginx
    
  4. 設定の確認
    sudo systemctl is-enabled nginx
    

(実行結果)

[ec2-user@ip-×××-××-××-×× ~]$ sudo systemctl start nginx
[ec2-user@ip-×××-××-××-×× ~]$ 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 Sat 2022-10-08 16:13:28 UTC; 9s ago
  Process: 3331 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
  Process: 3327 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
  Process: 3326 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
 Main PID: 3333 (nginx)
   CGroup: /system.slice/nginx.service
           tq3333 nginx: master process /usr/sbin/nginx
           mq3334 nginx: worker process

Oct 08 16:13:28 ip-×××-××-××-××.ap-northeast-1.compute.internal systemd[1]: Starting The nginx HTTP and reverse proxy server...
Oct 08 16:13:28 ip-×××-××-××-××.ap-northeast-1.compute.internal nginx[3327]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Oct 08 16:13:28 ip-×××-××-××-××.ap-northeast-1.compute.internal nginx[3327]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Oct 08 16:13:28 ip-×××-××-××-××.ap-northeast-1.compute.internal systemd[1]: Started The nginx HTTP and reverse proxy server.
[ec2-user@ip-×××-××-××-×× ~]$ sudo systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
[ec2-user@ip-×××-××-××-×× ~]$ sudo systemctl is-enabled nginx
enabled

ブラウザからアクセスしてみる

http://パブリック IP(または DNS)へブラウザからアクセス
  →
EC2 接続時に使用したパブリック IP(または DNS)

アクセスできない場合は、 セキュリティグループでポート:80 を許可しているか確認してください

参考記事

https://qiita.com/e-onm/items/0814b6c4db395e331df1

https://qiita.com/tamorieeeen/items/07743216a3662cfca890

https://qiita.com/miriwo/items/56f746e4f9b7eac75d6b

Discussion