zatsu na benkyou matome saito

勉強したことをまとめるだけのサイト、数学、プログラミング、機械学習とか

#2: インフラについて簡単に理解する[EC2]

ウェルノウンポート(TCP)一覧(抜粋)

ポート番号 プロトコル
20 FTP(データ転送)
21 FTP(コントロール)
22 SSH
23 Telnet
25 SMTP
53 DNS
80 HTTP
110 POP3
443 HTTPS

ウェルノウンポート(UDP)一覧(抜粋)

ポート番号 プロトコル
53 DNS
67 DHCP(サーバ)
68 DHCP(クライアント)

登録済ポート番号(自分で使うもの抜粋)

ポート番号 アプリケーション名
3000 Ruby on Rails
3306 MySQL
5432 PostgreSQL
9000 PHP-FPM

スクリーンショット 2020-11-03 1.19.18.png (536.2 kB)

sudo lsof -i -n -P

どのポートでどのサービスが待っているかを表示するコマンド

[ec2-user@ip-10-0-10-10 ~]$ sudo lsof -i -n -P
COMMAND   PID     USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
rpcbind  2693      rpc    6u  IPv4  17079      0t0  UDP *:111 
rpcbind  2693      rpc    7u  IPv4  17082      0t0  UDP *:735 
rpcbind  2693      rpc    8u  IPv4  17083      0t0  TCP *:111 (LISTEN)
rpcbind  2693      rpc    9u  IPv6  17084      0t0  UDP *:111 
rpcbind  2693      rpc   10u  IPv6  17085      0t0  UDP *:735 
rpcbind  2693      rpc   11u  IPv6  17086      0t0  TCP *:111 (LISTEN)
chronyd  2706   chrony    1u  IPv4  17295      0t0  UDP 127.0.0.1:323 
chronyd  2706   chrony    2u  IPv6  17296      0t0  UDP [::1]:323 
dhclient 2920     root    6u  IPv4  17857      0t0  UDP *:68 
dhclient 3020     root    5u  IPv6  18151      0t0  UDP [fe80::484:59ff:fe7c:3fcc]:546 
master   3163     root   13u  IPv4  19137      0t0  TCP 127.0.0.1:25 (LISTEN)
sshd     3389     root    3u  IPv4  20940      0t0  TCP *:22 (LISTEN)
sshd     3389     root    4u  IPv6  20951      0t0  TCP *:22 (LISTEN)
sshd     4801     root    3u  IPv4 110006      0t0  TCP 10.0.10.10:22->126.140.213.190:58552 (ESTABLISHED)
sshd     4819 ec2-user    3u  IPv4 110006      0t0  TCP 10.0.10.10:22->126.140.213.190:58552 (ESTABLISHED)

(LISTEN)

(LISTEN)と書かれているものが、他のPCから接続を待ち受けているポート(サービス)

(ESTABLISHED)

(ESTABLISHED)と書かれているものは、現在通信を行っているポート

Apacheのインストール

yum update コマンド(最初)

sudo yum update -y

Apache インストールコマンド

sudo yum -y install httpd

Apache起動コマンド

sudo systemctl start httpd.service systemctlはサービスを動作させる時に使用するコマンドで、今回はhttpdというapacheサービスを起動するのに使用している。

Apache status確認コマンド

sudo systemctl status httpd.service

[ec2-user@ip-10-0-10-10 ~]$ sudo systemctl status httpd.service
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
   Active: active (running) since 月 2020-11-02 16:31:30 UTC; 19s ago
     Docs: man:httpd.service(8)
 Main PID: 13864 (httpd)
   Status: "Total requests: 0; Idle/Busy workers 100/0;Requests/sec: 0; Bytes served/sec:   0 B/sec"
   CGroup: /system.slice/httpd.service
           ├─13864 /usr/sbin/httpd -DFOREGROUND
           ├─13865 /usr/sbin/httpd -DFOREGROUND
           ├─13866 /usr/sbin/httpd -DFOREGROUND
           ├─13867 /usr/sbin/httpd -DFOREGROUND
           ├─13868 /usr/sbin/httpd -DFOREGROUND
           └─13869 /usr/sbin/httpd -DFOREGROUND
  • active(running)となっていれば起動している

ps -axu これでも確認できる。

ps = Linuxで起動しているサービスを表示する -ax = すべてのプロセスを表示 u = メモリの使用率などをともに表示

[ec2-user@ip-10-0-10-10 ~]$ ps -axu
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.5 125584  5480 ?        Ss   11月01   0:02 /usr/lib/systemd/systemd --switched-root --system --deserialize 22
root         2  0.0  0.0      0     0 ?        S    11月01   0:00 [kthreadd]
root         4  0.0  0.0      0     0 ?        I<   11月01   0:00 [kworker/0:0H]
root         6  0.0  0.0      0     0 ?        I<   11月01   0:00 [mm_percpu_wq]
root         7  0.0  0.0      0     0 ?        S    11月01   0:00 [ksoftirqd/0]
root         8  0.0  0.0      0     0 ?        I    11月01   0:00 [rcu_sched]
root         9  0.0  0.0      0     0 ?        I    11月01   0:00 [rcu_bh]
root        10  0.0  0.0      0     0 ?        S    11月01   0:00 [migration/0]

以下のようにすると絞り込みができる。

[ec2-user@ip-10-0-10-10 ~]$ ps -axu | grep httpd
root     13864  0.0  0.9 257348  9540 ?        Ss   16:31   0:00 /usr/sbin/httpd -DFOREGROUND
apache   13865  0.0  0.6 298456  6448 ?        Sl   16:31   0:00 /usr/sbin/httpd -DFOREGROUND
apache   13866  0.0  0.6 298456  6448 ?        Sl   16:31   0:00 /usr/sbin/httpd -DFOREGROUND
apache   13867  0.0  0.6 298456  6448 ?        Sl   16:31   0:00 /usr/sbin/httpd -DFOREGROUND
apache   13868  0.0  0.6 495128  6448 ?        Sl   16:31   0:00 /usr/sbin/httpd -DFOREGROUND
apache   13869  0.0  0.6 298456  6448 ?        Sl   16:31   0:00 /usr/sbin/httpd -DFOREGROUND
ec2-user 13967  0.0  0.0 119436   952 pts/0    S+   16:34   0:00 grep --color=auto httpd

Apache自動起動コマンド

サーバーを停止や再起動した時にApacheが自動で起動するようにするコマンド

$ sudo systemctl enable httpd.service

[ec2-user@ip-10-0-10-10 ~]$ sudo systemctl enable httpd.service
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.

自動起動が設定されているか確認するコマンド

[ec2-user@ip-10-0-10-10 ~]$ sudo systemctl is-enabled httpd.service
enabled
  • enabledとなっていればOK

ファイアーウォールの設定

  • 現状ではファイアーウォールが通信をシャットダウンしてしまうのでサーバー上のアプリケーションを見ることができない。なので、ファイアーウォールの設定を変更して、アプリケーションを見れるようにする必要がある。 スクリーンショット 2020-11-03 1.40.45.png (278.8 kB)

セキュリティグループを設定してファイアーウォールを調整する。

スクリーンショット 2020-11-03 1.41.39.png (258.0 kB)

httpアクセスを許可するために80番ポートを開ける

HTTPを任意の場所から接続できるようにして、80番ポートを開ける設定。 スクリーンショット 2020-11-03 1.46.09.png (85.4 kB)

そうすると、Apacheが起動しているサーバーに接続することが可能。 スクリーンショット 2020-11-03 1.46.59.png (130.6 kB)