OCI ComputeでNginxをインストールする方法(複数OS・Cloud-init対応)

Developer

初めに

今回は、様々なOracle Cloud Infrastructure (OCI) ComputeインスタンスへNginxをインストールする方法をご紹介します。記事の最後には、cloud-initスクリプトも用意しています。インスタンス作成時にこのスクリプトを追加していただければ、作成完了後にすぐにNginxが利用可能な状態になります。

検証環境
インスタンス OS: Oracle Linux 9, CentOS 8, Ubuntu 22, Windows Server 2022

1. Oracle Linux 9 に Nginx をインストール

この方法は、Oracle Linux 7 および 8 にも適用できます。

コマンドの実行

sudo yum update -y
sudo yum install nginx -y

バージョンの確認

[opc@oracle-linux-9 ~]$ sudo nginx -v
nginx version: nginx/1.20.1

サービスを有効にし、Nginxを起動する

[opc@oracle-linux-9 ~]$ sudo systemctl enable nginx
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service ¨ /usr/lib/systemd/system/nginx.service.
[opc@oracle-linux-9 ~]$ sudo systemctl start nginx
[opc@oracle-linux-9 ~]$

ステータスの確認
ステータスが、Active: active (running)になっていることを確認します。

[opc@oracle-linux-9 ~]$ sudo systemctl status nginx
œ nginx.service - The nginx HTTP and reverse proxy server
     Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
     Active: active (running) since Fri 2022-11-04 10:00:52 GMT; 6s ago
......

TCP ポートを開く
HTTP ポート (80) のコマンド例:sudo firewall-cmd --permanent --add-port=80/tcp

[opc@oracle-linux-9 ~]$ sudo firewall-cmd --permanent --add-port=80/tcp
success
[opc@oracle-linux-9 ~]$ sudo firewall-cmd --reload
success
[opc@oracle-linux-9 ~]$ sudo firewall-cmd --list-ports
80/tcp

接続テスト
Web ブラウザを開き、URL ボックスにhttp://<ip_address>と入力すると、次のようなテスト・ページが表示されます。
Nginx test page on Oracle Linux

2. CentOS 8 に Nginx をインストール

インストールおよび起動コマンドは、Oracle Linux と同じです。
コマンドの実行

sudo yum update -y
sudo yum install nginx -y
sudo nginx -v

サービスを有効にし、Nginxを起動する
ステータスが、Active: active (running)になっていることを確認します。

[opc@centos8 ~]$ sudo systemctl enable nginx
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service ¨ /usr/lib/systemd/system/nginx.service.
[opc@centos8 ~]$ sudo systemctl start nginx
[opc@centos8 ~]$ sudo systemctl status nginx
œ nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: active (running) since Fri 2022-11-04 12:10:55 GMT; 4s ago
......

HTTPインプットの許可
HTTP ポート (80) のコマンド例: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             ctstate RELATED,ESTABLISHED,DNAT
3    ACCEPT     all  --  anywhere             anywhere
4    INPUT_direct  all  --  anywhere             anywhere
5    INPUT_POLICIES_pre  all  --  anywhere             anywhere
6    INPUT_ZONES_SOURCE  all  --  anywhere             anywhere
7    INPUT_ZONES  all  --  anywhere             anywhere
8    INPUT_POLICIES_post  all  --  anywhere             anywhere
9    DROP       all  --  anywhere             anywhere             ctstate INVALID
10   REJECT     all  --  anywhere             anywhere             reject-with icmp-host-prohibited
11   ACCEPT     all  --  anywhere             anywhere             state RELATED,ESTABLISHED
12   ACCEPT     icmp --  anywhere             anywhere
13   ACCEPT     all  --  anywhere             anywhere
14   ACCEPT     tcp  --  anywhere             anywhere             state NEW tcp dpt:ssh
15   REJECT     all  --  anywhere             anywhere             reject-with icmp-host-prohibited
[opc@centos ~]$

接続テスト
Nginx welcome page on CentOS

3. Ubuntu 22 に Nginx をインストール

コマンドの実行

sudo apt update -y
sudo apt install nginx -y
sudo nginx -v

curl http://localhostでNginxが起動しているのを確認します。

ubuntu@ubuntu:~$ curl http://localhost
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
......
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
ubuntu@ubuntu:~$

ここまで、Nginx のインストールは成功しましたが、まだ外部からアクセスできません。 HTTP/HTTPSでのインプットを許可するには、次のコマンドを使用して iptables を編集する必要があります。

HTTPインプットの許可
HTTP ポート (80) のコマンド例:sudo iptables -I INPUT -p TCP --dport 80 -j ACCEPT

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

接続テスト
Web ブラウザを開き、URL ボックスにhttp://<ip_address>と入力すると、次のようなウェルカム・ページが表示されます。
Nginx welcome page on Ubuntu

4. Windows Server 2022 に Nginx をインストール

ここから Windows 版の Nginx をダウンロードしてください。
Download Nginx installer for Windows

ダウンロード後、nginx-1.23.3.zipを解凍します。解凍後、nginx.exeをダブルクリックして起動します。
Unzip Nginx installer

ローカルのWEBブラウザを開き、localhostを入力したら、次のようなウェルカム・ページが表示されます。
Nginx welcome page on Windows Server

外部から接続する場合、TCPポート80 (SSLの場合は443) の許可は必要です。この時点まだ実施していませんので、外部からアクセスできない状態です。

TCPポート80を許可するのに、Windows Defender Firewallより実施してもよいし、PowerShellで以下のコマンドを実施してもよいです。

New-NetFirewallRule -DisplayName "ALLOW TCP PORT 80" -Direction inbound -Profile Any -Action Allow -LocalPort 80 -Protocol TCP

実行結果:
empty

許可後、外部からパブリックIPか、DNS名で接続したら、ウェルカム・ページが表示されます(省略)。

付録 - Cloud-init スクリプト

Computeインスタンスを作成する時に、Cloud-init スクリプトを追加すれば、Nginx を自動的にインストールできます。
Cloud-init script

1) Oracle Linux の場合

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

2) CentOS の場合

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

3) Ubuntu の場合

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

4) Windows の場合

#ps1_sysnative
net user opc "<NewPasswd>"

${nginx_path} = "C:\Nginx\"
${nginx_ver} = "nginx-1.23.3"
${nginx_zip} = ${nginx_ver} + ".zip" # nginx-1.23.3.zip

${download_url} = "http://nginx.org/download/" + ${nginx_zip}

${nginx_full_path} = ${nginx_path} + ${nginx_ver} # "C:\Nginx\nginx-1.23.3"
${nginx_local_zip} = ${nginx_path} + ${nginx_zip} # "C:\Nginx\nginx-1.23.3.zip"

New-Item $nginx_path -itemType Directory
Invoke-WebRequest -Uri ${download_url} -OutFile ${nginx_local_zip}
Expand-Archive -Path ${nginx_local_zip} -DestinationPath ${nginx_path}
New-NetFirewallRule -DisplayName "ALLOW TCP PORT 80" -Direction inbound -Profile Any -Action Allow -LocalPort 80 -Protocol TCP
cd ${nginx_full_path}
start nginx

以上

改訂歴
2022年11月:初版作成
2023年03月:「Windows Server 2022 に Nginx をインストール」を追加

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