フリーランス 技術調査ブログ

フリーランス/エンジニア Ruby Python Nodejs Vuejs React Dockerなどの調査技術調査の備忘録

AWS EC2にNginxをインストールする

パッケージの更新

  • yum updateでOSにインストールされているpackageを更新する
sudo yum -y update
  • 下記のメッセージが表示された場合、更新するパッケージがないということで放置する。
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
No packages marked for update

nginxをインストールする

  • Amazon Linux2の場合、Extras Libraryを利用してNignxをインストールする
  • 下記のコマンドを実行するとExtrasLibraryにどのようなpackageがインストールされているか確認することが出来る。nginxがnginx1というパッケージ名で存在することを確認できる。
$ amazon-linux-extras | grep nginx
 38  nginx1                   available    [ =stable ]

aws.amazon.com

  • nginxをインストールする
sudo amazon-linux-extras install nginx1
  • nginxをインストールするとステータスが avaiable から enabled に変更されていることを確認できる
 38  nginx1=latest            enabled      [ =stable ]
  • nginxをインストールした時点では起動していない
$ sudo systemctl status nginx.service
● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
   Active: inactive (dead)

Sep 14 23:07:15 ip-10-0-10-10.ap-northeast-1.compute.internal systemd[1]: Unit nginx.service cannot be reloaded because it is inactive.
  • nginxを起動する
$ sudo systemctl start nginx.service
  • nginxの起動していることを確認する
$ sudo systemctl status nginx.service
● 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-09-14 23:14:05 UTC; 1s ago
  Process: 32118 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
  Process: 32115 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
  Process: 32114 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
 Main PID: 32121 (nginx)
   CGroup: /system.slice/nginx.service
           tq32121 nginx: master process /usr/sbin/nginx
           mq32123 nginx: worker process
  • サーバーが再起動したときに自動起動するように設定する
$ sudo systemctl enable nginx.service
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.

$ sudo systemctl is-enabled nginx.service
enabled