2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

OCI Compute インスタンスに Nginx をインストールする

Last updated at Posted at 2022-11-15

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

初めに

今回は、各種な OCI (Oracle Cloud Infrastruture) 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>と入力すると、次のようなテスト・ページが表示されます。
image.png

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

接続テスト
image.png

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>と入力すると、次のようなウェルカム・ページが表示されます。
image.png

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

ここから Windows 版の Nginx をダウンロードしてください。
image.png

ダウンロード後、nginx-1.23.3.zipを解凍します。解凍後、nginx.exeをダブルクリックして起動します。
image.png
ローカルのWEBブラウザを開き、localhostを入力したら、次のようなウェルカム・ページが表示されます。
image.png
外部から接続する場合、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

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

付録 - Cloud-init スクリプト

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

Oracle Linux の場合

Cloud-init スクリプト

#!/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

CentOS の場合

Cloud-init スクリプト

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

Ubuntu の場合

Cloud-init スクリプト

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

Windows の場合

Cloud-init スクリプト

#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 をインストール」を追加


関連記事
オラクル・クラウドの個人ブログ一覧
OCIでNginxのDockerイメージを利用する
OCI コンテナ・インスタンスでNginxコンテナを実行する
コマンドで各種のOSにHTTPサーバをインストール

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?