コマンドで様々なOSにHTTPサーバをインストール

Developer

初めに

OCI (Oracle Cloud Infrastructure) でロードバランサーを構築した後、そのバックエンドサーバとしてHTTPサーバーを設定し、ヘルスチェックを行うことは、一般的な構成手順です。本記事では、主要なOS環境において、コマンドを用いてHTTPサーバー(Microsoft IIS または Apache HTTP Server)をインストールする具体的な手順をまとめ、各OSごとに解説します。環境構築作業の効率化にお役に立てれば幸いです。

動作確認済環境
OCIのプラットフォームイメージで作成されたVMインスタンス。

1. Windows Server 2019 に IIS をインストール

Windows環境では、PowerShellスクリプトを一行実行するだけで、Microsoft IIS(Internet Information Services)をインストールできます。

管理者ユーザーとしてPowerShellを起動し、次のコマンドを実行します:
Install-WindowsFeature -Name Web-Server -IncludeAllSubFeature -IncludeManagementTools

PS C:\Windows\system32> Install-WindowsFeature -Name Web-Server -IncludeAllSubFeature  -IncludeManagementTools

Success Restart Needed Exit Code      Feature Result
------- -------------- ---------      --------------
True    No             Success        {ASP.NET 4.7, .NET Framework 3.5 (includes...


PS C:\Windows\system32>


次のようなウェルカムページが表示されます。(VMの再起動は不要)
Microsoft IIS

2. Oracle Linux (7,8,9) に Apache HTTP サーバをインストール

次は「Oracle Linux 7」にApache HTTP サーバ (httpd) をインストールする方法の例です。

デフォルトでは、HTTPポート(80/443)がファイアウォールレベルで閉じられています。ポートを開くには、firewall-cmdを使用します。念のため、firewalldが実行中の状態にあることを確認してください。(OCIインスタンスが作成された後、firewalldはデフォルトで実行中の状態になります。)

コマンド: sudo systemctl status firewalld

[opc@linux7 ~]$ sudo systemctl status firewalld
firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
   Active: active (running) since Mon 2021-10-25 12:36:46 GMT; 9min ago
     Docs: man:firewalld(1)
 Main PID: 1297 (firewalld)
   Memory: 4.1M
   CGroup: /system.slice/firewalld.service
           mq1297 /usr/bin/python2 -Es /usr/sbin/firewalld --nofork --nopid

Oct 25 12:36:45 localhost.localdomain systemd[1]: Starting firewalld - dynamic firewall daemon...
Oct 25 12:36:46 localhost.localdomain systemd[1]: Started firewalld - dynamic firewall daemon.
Oct 25 12:36:46 localhost.localdomain firewalld[1297]: WARNING: AllowZoneDrifting is enabled. This is considered an insec... now.
Hint: Some lines were ellipsized, use -l to show in full.
[opc@linux7 ~]$

firewalldが停止している場合は、次のコマンドを使用して起動します。
コマンド: sudo systemctl start firewalld

次のコマンドを使用してApacheをインストールし、HTTPポートを開きます。

sudo yum -y install httpd
sudo firewall-cmd --permanent --add-port=80/tcp
sudo firewall-cmd --reload
sudo systemctl start httpd

sudo firewall-cmd --list-portsを使ってHTTPポートが開いていることを確認できます。

[opc@linux7 ~]$ sudo firewall-cmd --list-ports
80/tcp
[opc@linux7 ~]$

httpdがアクティブ状態になったら、ブラウザを開いてサーバのIPアドレスまたはDNS名を入力すると、次のようなテストページが表示されます。
Apache HTTP Server (on Oracle Linux)

新しいインデックスページを作成したい場合は、index.htmlを編集してください。

sudo su
echo 'AP-Server1' >> /var/www/html/index.html
exit

3. CentOS 8 に Apache HTTP サーバをインストール

インストールと起動のコマンドは「Oracle Linux」と同じです。

[opc@centos8 ~]$ sudo yum -y install httpd
......
Complete!
[opc@centos8 ~]$ sudo systemctl start httpd
[opc@centos8 ~]$ sudo systemctl status httpd
œ httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
   Active: active (running) since Fri 2022-11-04 12:51:14 GMT; 4s ago
......
[opc@centos8 ~]$ 

HTTPポートを許可
コマンド例:sudo iptables -I INPUT -p TCP --dport 80 -j ACCEPT

[opc@centos ~]$ sudo iptables -I INPUT -p TCP --dport 80 -j ACCEPT
[opc@centos ~]$ sudo iptables -L INPUT --line-numbers
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination
1    ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:http
2    ACCEPT     all  --  anywhere             anywhere             state RELATED,ESTABLISHED
3    ACCEPT     icmp --  anywhere             anywhere
4    ACCEPT     all  --  anywhere             anywhere
5    ACCEPT     tcp  --  anywhere             anywhere             state NEW tcp dpt:ssh
6    REJECT     all  --  anywhere             anywhere             reject-with icmp-host-prohibited
[opc@centos ~]$

ブラウザで確認したら、次のようなテストページが表示されます。
Apache HTTP Server (on Oracle CentOS)

4. Ubuntu 20 に Apache HTTP サーバをインストール

iptablesコマンドを使用して、入力のチェーン・ルールを作成します。(CentOSのケースと同じ)

ubuntu@ubuntu:~$ sudo iptables -I INPUT -p TCP --dport 80 -j ACCEPT
ubuntu@ubuntu:~$ sudo iptables -L INPUT --line-numbers
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination
1    ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:http
2    ACCEPT     all  --  anywhere             anywhere             state RELATED,ESTABLISHED
3    ACCEPT     icmp --  anywhere             anywhere
4    ACCEPT     all  --  anywhere             anywhere
5    ACCEPT     udp  --  anywhere             anywhere             udp spt:ntp
6    ACCEPT     tcp  --  anywhere             anywhere             state NEW tcp dpt:ssh
7    REJECT     all  --  anywhere             anywhere             reject-with icmp-host-prohibited
ubuntu@ubuntu:~$

aptを使用してApache HTTPサーバをインストールします。
コマンド: sudo apt install apache2

この時点では、デフォルトのウェルカムページが表示できますけど、インスタンス作成直後、ufwは有効になっていないことをご注意ください。有効化するには、次のコマンドをご利用ください。
コマンド: sudo ufw enable

次のように、Apacheのファイアウォール情報を確認できます。(HTTPポートが開いていることを確認)
コマンド: sudo ufw app info Apache

ubuntu@ubuntu:~$ sudo ufw status
Status: inactive
ubuntu@ubuntu:~$ sudo ufw enable
Command may disrupt existing ssh connections. Proceed with operation (y|n)? y
Firewall is active and enabled on system startup
ubuntu@ubuntu:~$ sudo ufw status
Status: active
ubuntu@ubuntu:~$ sudo ufw app list
Available applications:
  Apache
  Apache Full
  Apache Secure
  OpenSSH
ubuntu@ubuntu:~$ sudo ufw app info Apache
Profile: Apache
Title: Web Server
Description: Apache v2 is the next generation of the omnipresent Apache web
server.

Port:
  80/tcp
ubuntu@ubuntu:~$

ブラウザで確認したら、次のようなデフォルトのウェルカムページが表示されます。
Apache HTTP Server (on Oracle Ubuntu)

付録 - 初期化スクリプト (Cloud-init)

クラウドでVMを作成する際に、下記のコマンドをCloud-initスクリプトに配置すると、インスタンスの起動後にHTTPサーバが自動的にインストールされます。
Cloud-init script

1) Windows Server

#ps1_sysnative
Install-WindowsFeature -Name Web-Server -IncludeAllSubFeature -IncludeManagementTools

2) Oracle Linux

#!/bin/bash
yum update -y
yum install httpd -y
firewall-cmd --permanent --add-port=80/tcp
firewall-cmd --reload
systemctl start httpd

3) CentOS

#!/bin/bash
yum update -y
yum install httpd -y
systemctl start httpd
iptables -I INPUT -p TCP --dport 80 -j ACCEPT

4) Ubuntu

#!/bin/bash
apt update -y
apt install apache2 -y
iptables -I INPUT -p TCP --dport 80 -j ACCEPT

以上

タイトルとURLをコピーしました