LoginSignup
0
0

Google CloudでVMインスタンスを立てて、HTTPサーバを設定する方法(CLI)

Last updated at Posted at 2023-08-08

Google CloudでVMインスタンスを立てて、HTTPサーバを設定する方法(CLI)

目次

  1. 導入
  2. gcloudの設定
  3. VMインスタンスの作成
  4. HTTPサーバの設定
  5. 余談

導入

Google CloudでHTTPサーバを立ててみました。

注意: 今回はdefault VPCを利用します。あと筆者は基本的にマネコンに疎いので、GCloudでの操作が基本です。

gcloudの設定

まず、Google Cloud Shellを起動します。ここでは、gcloudの設定を行います。

Cloud Shellをアクティブにするをクリックし、CloudShellを開きましょう。何か確認メッセージが出てきたら適当に許可しましょう()

image.png

gcloud config set project my-project-id
gcloud config set compute/zone asia-northeast1-b
  • my-project-id:GCPプロジェクトIDを設定します。
  • asia-northeast1-b:zoneを設定します。

以下で確認ができます。

gcloud config list compute/zone

下みたいな結果が返ってきます。

[compute]
zone = asia-northeast1-b

Your active configuration is: [cloudshell-xxxxx]

VMインスタンスの作成

次に、Google CloudのCompute Engine上にVMインスタンスを作成します。以下のコマンドを実行してください:

gcloud compute instances create my-http-server --machine-type=e2-medium --image-family=debian-10 --image-project=debian-cloud

このコマンドのパラメータは以下の通りです:

  • my-http-server:作成するインスタンスの名前
  • e2-medium:インスタンスのマシンタイプ
  • debian-10:使用するOSのイメージファミリー
  • debian-cloud:使用するOSのイメージプロジェクト

結果は以下のような感じです。

Created [https://www.googleapis.com/compute/v1/projects/deductive-state-xxxxxx/zones/asia-northeast1-b/instances/my-http-server].
NAME: my-http-server
ZONE: asia-northeast1-b
MACHINE_TYPE: e2-medium
PREEMPTIBLE: 
INTERNAL_IP: YOUR_PRIVATE_IP
EXTERNAL_IP: YOUR_PUBLIC_IP
STATUS: RUNNING

また念のため、作成されたインスタンスを確認しましょう。

gcloud compute instances list --filter="name:my-http-server"
NAME: my-http-server
ZONE: asia-northeast1-b
MACHINE_TYPE: e2-medium
PREEMPTIBLE: 
INTERNAL_IP: YOUR_PRIVATE_IP
EXTERNAL_IP: YOUR_PUBLIC_IP
STATUS: RUNNING

が返ってくるはずです。

HTTPサーバの設定

次に、作成したVMインスタンス上でHTTPサーバを設定します。以下のコマンドを実行して、VMインスタンスにSSH接続します:

gcloud compute ssh my-http-server

次に、以下のコマンドを実行してApache HTTPサーバをインストールします:

sudo apt-get update
sudo apt-get install apache2 -y

Apache HTTPサーバが正常にインストールされたはずです。

curl localhost:80

これでApacheのデフォルトのhtmlが返ってきたら成功です。

##後片付け
最後に作成したインスタンスを削除しましょう。

gcloud compute instances delete my-http-server

念のため確認。

gcloud compute instances list --filter="name:my-http-server"

以下結果。

Listed 0 items.

余談

以下のコマンドを使用することでstartup-script(インスタンス起動時に実行されるスクリプト)でApacheサーバを起動できます。

gcloud compute instances create my-http-server \
--machine-type=e2-medium \
--image-family=debian-10 \
--image-project=debian-cloud \
--metadata=startup-script='#! /bin/bash
sudo apt-get update
sudo apt-get install apache2 -y'
0
0
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
0
0