1
1

More than 3 years have passed since last update.

AWSで基盤お試し構築~nginxで遊ぶ~

Last updated at Posted at 2019-10-11
1 / 32
前回の話

AWSで基盤お試し構築~サーバでコマンドを叩ける環境を作る~

前回までにやったこと

サーバでコマンドを叩けるように環境を作った。


今回目指すところ

Nginxをインストールしてコンテンツが見えるようにする

「Webサーバを作って、外から叩いたらなんか見えるようにしたい」が当初の目標。
その為には何かしらのミドルウェアを入れないといけませぬ。

ということで、何となくNginxをChoice.


好きなコンテンツをサーバにアップロードして、ブラウザから見えるようにする

まあ、hello worldを出すだけで終わりますよね。
センスのいい文字列とか浮かばないし。


準備

  • Session ManagerからEC2に接続する。
  • rootユーザのパスワード変更
    • sudo passwd root
    • お好きなパスワードを入力(2回)
    • パスワード変更完了
  • rootユーザにスイッチ
    • su - root
sh-4.2$
sh-4.2$
sh-4.2$ sudo passwd root
Changing password for user root.
New password:
BAD PASSWORD: The password fails the dictionary check - it is based on a dictionary word
Retype new password:
passwd: all authentication tokens updated successfully.
sh-4.2$
sh-4.2$ su - root
Password:
Last login: Mon Aug  5 07:04:11 UTC 2019 on pts/0
[root@ip-10-0-1-99 ~]#

リポジトリを追加する

とりあえず何も考えずにこれ叩いてみる。

amazon-linux-extras install epel

Is this ok [y/d/N]:とかそんな感じのが出てきたら、ひたすらy返してください。


Nginxをインストールする

yumコマンドでインストール

yum installを叩きましょう。
yum install nginx


こんな感じの文字列が出てくるはず。

[root@ip-10-0-1-99 ~]# yum install nginx
Loaded plugins: priorities, update-motd, upgrade-helper
amzn-main                                                                                                                  | 2.1 kB  00:00:00
amzn-updates                                                                                                               | 2.5 kB  00:00:00
Resolving Dependencies
--> Running transaction check
---> Package nginx.x86_64 1:1.14.1-2.34.amzn1 will be installed
--> Processing Dependency: libprofiler.so.0()(64bit) for package: 1:nginx-1.14.1-2.34.amzn1.x86_64
--> Running transaction check
---> Package gperftools-libs.x86_64 0:2.0-11.5.amzn1 will be installed
--> Processing Dependency: libunwind.so.8()(64bit) for package: gperftools-libs-2.0-11.5.amzn1.x86_64
--> Running transaction check
---> Package libunwind.x86_64 0:1.1-10.8.amzn1 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

==================================================================================================================================================
 Package                             Arch                       Version                                    Repository                        Size
==================================================================================================================================================
Installing:
 nginx                               x86_64                     1:1.14.1-2.34.amzn1                        amzn-updates                     585 k
Installing for dependencies:
 gperftools-libs                     x86_64                     2.0-11.5.amzn1                             amzn-main                        570 k
 libunwind                           x86_64                     1.1-10.8.amzn1                             amzn-main                         72 k

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

Total download size: 1.2 M
Installed size: 2.9 M
Is this ok [y/d/N]: 

Is this ok [y/d/N]:にyを返してあげる。


なんかダウンロードが始まって勝手にインストールされてCompleteする。

Is this ok [y/d/N]: y
Downloading packages:
(1/3): libunwind-1.1-10.8.amzn1.x86_64.rpm                                                                                 |  72 kB  00:00:00
(2/3): gperftools-libs-2.0-11.5.amzn1.x86_64.rpm                                                                           | 570 kB  00:00:00
(3/3): nginx-1.14.1-2.34.amzn1.x86_64.rpm                                                                                  | 585 kB  00:00:00
--------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                             2.4 MB/s | 1.2 MB  00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : libunwind-1.1-10.8.amzn1.x86_64                                                                                                1/3
  Installing : gperftools-libs-2.0-11.5.amzn1.x86_64                                                                                          2/3
  Installing : 1:nginx-1.14.1-2.34.amzn1.x86_64                                                                                               3/3
  Verifying  : libunwind-1.1-10.8.amzn1.x86_64                                                                                                1/3
  Verifying  : gperftools-libs-2.0-11.5.amzn1.x86_64                                                                                          2/3
  Verifying  : 1:nginx-1.14.1-2.34.amzn1.x86_64                                                                                               3/3

Installed:
  nginx.x86_64 1:1.14.1-2.34.amzn1

Dependency Installed:
  gperftools-libs.x86_64 0:2.0-11.5.amzn1                                    libunwind.x86_64 0:1.1-10.8.amzn1

Complete!
[root@ip-10-0-1-99 ~]#

  • 本当にインストールされたか不安な人向け
    rpm -q nginx

Nginxを起動してみる

なんだか起動してみたくなったので起動する。

まずは今のステータスを確認する
service nginx status

[root@ip-10-0-1-99 ~]# service nginx status
nginx is stopped

止まってるっぽいので、起動してみる。
service nginx start

[root@ip-10-0-1-99 ~]# service nginx start
Starting nginx:                                            [  OK  ]

もう1回ステータスを確認してみる

[root@ip-10-0-1-99 ~]# service nginx status
nginx (pid  23346) is running...

runningって言ってるので、起動したっぽい。


  • 本当に起動しているのか心配な人向け
    ps -ef | grep nginx

試しにブラウザからWebサーバへアクセスしてみる

  • ブラウザにサーバのIPアドレスを入力して、Enterを叩く。
    • IPアドレスは、EC2の画面で確認できる
    • IPv4パブリックIPを参照 image.png

  • ブラウザから叩いてみたらこんな感じ。 image.png

なんだかよくわからないけど、ただのEC2だったのがWebサーバっぽくなりました。


せっかくなので、nginxを止めてみる

いきなり停止コマンド叩いてる風に見せてますが、ステータス確認はちゃんとやりましょう。
めんどくさいから書くのやめたとかじゃないです。

それじゃ止めましょう。
service nginx stop

[root@ip-10-0-1-99 ~]# service nginx stop
Stopping nginx:                                            [  OK  ]

ステータス確認してみる

[root@ip-10-0-1-99 ~]# service nginx status
nginx is stopped

stoppedって言ってるので止まったんでしょう。


  • 本当に止まったのか心配な人向け
    ps -ef | grep nginx

この後nginxを使うので、もう1回起動する。
コマンドは端折ります。
めんどくさいわけじゃなくて時短です


コンテンツをアップロードして公開する

"Welcome to nginx"だけ公開してるWebサーバとか存在価値ないので、何かコンテンツを上げてみる。
といっても、センスがないので"hello world"を表示するだけ。。


コンテンツはどこにあるのか

ブラウザでサーバのIPを叩くと"Welcome to nginx"が表示される。
ということは、"Welcome to nginx"を表示するためのコンテンツがどこかにある。

image.png


This is the default index.html page that is distributed with nginx on the Amazon Linux AMI. It is located in /usr/share/nginx/html.

You should now put your content in a location of your choice and edit the root configuration directive in the nginx configuration file /etc/nginx/nginx.conf.

と書いてあるので、/usr/share/nginx/htmlに何かあるはず。


  • 実機で確認
    /usr/share/nginx/htmlに移動
    ② 移動できたか確認
    /usr/share/nginx/htmlに何があるか確認

こんな感じならいい感じ。

[root@ip-10-0-1-99 html]# ll
total 16
-rw-r--r-- 1 root root 3696 Dec 13  2018 404.html
-rw-r--r-- 1 root root 3738 Dec 13  2018 50x.html
-rw-r--r-- 1 root root 3770 Dec 13  2018 index.html
-rw-r--r-- 1 root root  368 Dec 13  2018 nginx-logo.png
lrwxrwxrwx 1 root root   32 Jul 25 06:55 poweredby.png -> /usr/share/pixmaps/poweredby.png

本当にここあるのが画面で出てるアレなのか

四の五の言わないで確認する。

  • 実機で確認
    htmlファイルの中身を見てみる。
    どのファイルを見るべきかは、"Welcome to nginx"に書いてありますね。

  • ということで、、、
    /usr/share/nginx/htmlに自作のhtmlファイルを置く
    →そうすると、ブラウザで自作のhelloworldを見ることができる

なんだかそんな気がしてきましたね。


hello worldを作る

  • 作りましょう作りましょう。
    ① 空のhtmlファイルを作成しつつ、エディタを起動(Linux上で)
    ② hello worldって書く。
    ③ 保存する。
    ④ 作成できたか確認する

こんな感じならいい感じ。

[root@ip-10-0-1-99 html]# ll
total 20
-rw-r--r-- 1 root root 3696 Dec 13  2018 404.html
-rw-r--r-- 1 root root 3738 Dec 13  2018 50x.html
-rw-r--r-- 1 root root   15 Jul 26 02:30 helloworld.html
-rw-r--r-- 1 root root 3770 Dec 13  2018 index.html
-rw-r--r-- 1 root root  368 Dec 13  2018 nginx-logo.png
lrwxrwxrwx 1 root root   32 Jul 25 06:55 poweredby.png -> /usr/share/pixmaps/poweredby.png

ブラウザからhello worldを見る。

ブラウザから↓こんなかんじで叩いてみる。
http://サーバのIP/さっき作ったファイル.html

こんな風に見えてくればいいです。
image.png


今回やったこと

  • nginxをインストール
  • nginxを起動して停止して起動した
  • ブラウザからサーバを突っついて画面を確認した
  • hello worldをアップロードして、ブラウザから確認した

次へ。

AWSで基盤お試し構築~RDS~

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1