nginxベースの高速なWordPress環境をお名前.comのVPSで構築 2ページ

CentOS環境のアップデートと日時の確認

 CentOS環境にログインしたら、最初にOS環境の日時設定を正確なものとするため、ntpdateをインストールして時刻合わせを実施しておこう。さらにOSのアップグレードも実施する。標準環境はCentOS 6.2だが、アップデートを行うと6.3の環境となる。この際カーネル周辺のアップグレードも行われるので、念のため再起動も行っておく。

# yum install ntpdate
# ntpdate ntp.nict.jp
xx xxx xx:xx:xx ntpdate[xxxx]: step time server 133.243.238.244 offset 1.660451 sec
# yum update
# reboot

開放ポートの確認と80番ポート(HTTPポート)の開放

 次にポートの開放状況を確認しよう。標準環境のCentOSではファイアウォール(iptables)が設定されており、22番ポートを使用するSSHアクセスのみが通過できる状態になっている。設定確認は「iptables -L」コマンドを使う。

# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination
ACCEPT     all  --  anywhere             anywhere            state RELATED,ESTABLISHED
ACCEPT     icmp --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere
ACCEPT     tcp  --  anywhere             anywhere            state NEW tcp dpt:ssh
REJECT     all  --  anywhere             anywhere            reject-with icmp-host-prohibited

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination
REJECT     all  --  anywhere             anywhere            reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

 初期設定の作業中は、scpによるファイル転送などで22番ポートを使うことがあるかもしれないので、作業利便性を考えてひとまず標準のまま22番ポートを開けて作業を進めてもいいだろう。しかし、22番ポートを開けたままにしておくとアタックを受ける可能性が常にある。実際、数日OSを起動していただけで/var/log/secureファイルにはアタックの痕跡が記録されていた。なるべく早い段階で22番ポートを閉じることをお勧めする。

 iptablesの設定は、/etc/sysconfig/iptablesファイルで行う(リスト2)。

リスト2 標準の/etc/sysconfig/iptablesファイル

# Firewall configuration written by system-config-firewall
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT

 HTTPアクセスを有効にするために80番ポートを解放し22番ポートを閉じるには、ファイル中で「22」となっているところを「80」に変更すればOKだ。

# vi /etc/sysconfig/iptables

↓22となっているところを80に変更
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT

 ファイルを修正して保存したら、設定を有効にするためにiptablesを再起動する。

# /etc/init.d/iptables restart

 また、sshdは不要なので停止し、OS起動時にsshdが起動しないよう設定しておく。

# /etc/init.d/sshd stop
# chkconfig
sshd            0:off   1:off   2:on    3:on    4:on    5:on    6:off

# chkconfig sshd off
# chkconfig
sshd            0:off   1:off   2:off   3:off   4:off   5:off   6:off

 chkconfigコマンドで「sshd」の項目が「off」に変更されたのを確認できればOKだ。