2
1

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

Last updated at Posted at 2021-12-15

本ブログは、オラクル・クラウドの個人ブログの1つです。

初めに

ロードバランサーを作成した後、HTTPサーバをバックエンドとしてセットアップし、ヘルスチェックを行うことは、一般的な作業です。このブログは、様々なOSにおいてHTTPサーバ(IISまたはApache)をインストールする手順をまとめたものです。少しお役立てれば幸いです。

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

目次

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

一行のPowerShellスクリプトで、マイクロソフトのIIS(インターネットインフォメーションサービス)をインストールできます。

管理者ユーザーとしてPowerShellを起動し、次のコマンドを実行します。
Command: 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>

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

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

次は「Oracle Linux」に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名を入力すると、次のようなテストページが表示されます。
image.png

新しいインデックスページを作成したい場合は、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 ~]$

ブラウザで確認したら、次のようなテストページが表示されます。
image.png

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:~$

ブラウザで確認したら、次のようなデフォルトのウェルカムページが表示されます。
image.png

付録 - 初期化スクリプト

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

Windows Server

Cloud-init Script

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

Oracle Linux

Cloud-init Script

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

CentOS

Cloud-init Script

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

Ubuntu

Cloud-init Script

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

以上です。


関連記事
オラクル・クラウドの個人ブログ一覧

2
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
2
1