環境
- Windows10
- PowerShell
- VirtalBox:手元のPCにServerを複数作成できる。
- Vargrnt:サーバ設定、各種インストールが得意。
内容
お手元のPC(Win10)にLocalSeverを複数作成。手順
1.VritalBox、Vagrantをインストール
Virtualbox:https://www.virtualbox.org/
Vagrant:https://www.vagrantup.com/
2.複数のLocalSeverをまとめるファイルを作成
PowerShell起動後、入力
PS C:\>mkdir LocalSeversGroup PS C:\>cd LocalSeversGroup
3.各LocalSeverを各ディレクトリに作成
※各サーバー毎にディレクトリ名を変更して作成する
PS C:\LocalSeversGroup>mkdir SetCentOS PS C:\LocalSeversGroup>cd SetCentOS
4.各LocalSeverの設定をする
A.初期化及びVagrantfile作成
PS C:\LocalSeversGroup\SetCentOS>vagrant init centos/7
B.Vagrantfileの編集
作成した「\LocalSeversGroup\SetCentOS\Vagrantfile」をエディタで開き下記修正。
※各サーバー毎にプライベートIPを変更する<下記参照>
# Create a forwarded port mapping which allows access to a specific port # within the machine from a port on the host machine and only allow access # via 127.0.0.1 to disable public access ←「#」削除・・・30行目 config.vm.network "forwarded_port", guest: 80, host: 8080, auto_correct: true # config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1" : # Create a private network, which allows host-only access to the machine # using a specific IP. config.vm.network "private_network", ip: "192.168.33.10" ←「#」削除・・・35行目 : ※各サーバー毎にIP変更 ↑を"192.168.33.20"とか config.vm.provider "virtualbox" do |vb| ←「#」削除・・・52行目 # # Display the VirtualBox GUI when booting the machine # vb.gui = true vb.cpus = 2 ←追記 # # Customize the amount of memory on the VM: vb.memory = "1024" ←「#」削除 "1024"を”2048”に変更 end ←「#」削除
C.プラグイン追加
PS C:\LocalSeversGroup\SetCentOS> vagrant plugin install vagrant-hostmanager PS C:\LocalSeversGroup\SetCentOS> vagrant plugin install vagrant-vbguest
ちょこっとメモ
D.Vagrant起動
※virtalBox Intefaceによる変更確認が別窓で聞かれるので、はいを選択。
PS C:\LocalSeversGroup\SetCentOS>vagrant up : default: SSH address: 127.0.0.1:2200 ←ここの「IPアドレス:ポート番号」を覚えておく default: SSH username: vagrant default: SSH auth method: private key :
ちょこっとメモ
エラー:Failed to open/create the internal network
'HostInterfaceNetworking-VirtualBox Host-Only Ethernet Adapter' の対処法
ネットワークアダプターの設定を変更する
- 「設定(歯車アイコン)」をクリック
- 「ネットワークとインターネット」を選択
- 右側の状態から下方「ネットワークと共有センター」を選択
- 左側「アダプター設定の変更」を選択
- 各アダプターの中から「VirtualBox Host-Only Network #N」を右クリック、「プロパティ」選択
- 項目から「VirtualBox NDIS6 Bridged Networking Driver」をチェックON
- 項目から「インターネットプロトコル バージョン6(TCP/IPv6)」をチェックOFF
- 「OK」を選択、閉じる
- 先ほどの「VirtualBox Host-Only Network #N」を右クリック
- 一度「無効」にしてから再度「有効」にする
- 「vagrant up」で再起動
E.状態確認
PS C:\LocalSeversGroup\SetCentOS>vagrant status ←状態を確認 Current machine states: default running (virtualbox) ←で成功!! The VM is running. To stop this VM, you can run `vagrant halt` to shut it down forcefully, or you can run `vagrant suspend` to simply suspend the virtual machine. In either case, to restart it again, simply run `vagrant up`.
5.puttyで接続
下記の項目を記入、最後に「開く」を押して接続
login as: vagrant ←初期ID:vagrant 入力 vagrant@127.0.0.1's password: ←初期パスワード:vagrant 入力 Last login: Thu May 30 10:16:56 2019 from 10.0.2.2 [vagrant@localhost ~]$
ちょこっとメモ
エラー「No supported authentication methods available (server sent: publickey,gssapi-with-mic)」の対処法
・PuttyにVagrant 用の秘密鍵が登録がないなら作成しよう・・・Vagrant は公開鍵認証方式の為
- 「puttygen.exe」を起動
- 左上「File」>「load private key」選択
- ディレクトリを下記へ移動「作成したLocalSeverパス\.vagrant\machines\default\virtualbox」
- 左下ファイルの種類「All files(*.*) 」に変更
- 出現したファイル「private_key」を開く
- 中央右端「Save private key」ボタンをクリック
- 「パスフレーズなしの確認」窓が開くが「OK」ボタンをクリック
- 保存場所を指定し、「任意の名前.ppk」(秘密鍵)で保存
- puttyにて右側のセッション一覧から保存するセッションを選んで読込む
- 左側のカテゴリで「接続> SSH> 認証」選択
- 右下方「認証パラメータグループ」内「[認証のためのプライベートキーファイル」に先程作成した秘密鍵を設定する
- 左側のカテゴリで「セッション」選択し元に戻たら保存ボタン。
6.失敗したら
PS C:\LocalSeversGroup\SetCentOS> vagrant halt # vagrantの停止 PS C:\LocalSeversGroup\SetCentOS> vagrant destroy # Virturlbox上にあるVMを削除 PS C:\LocalSeversGroup\SetCentOS> vagrant box remove centos/7 # Vagrantのbox削除 PS C:\LocalSeversGroup\SetCentOS> vagrant box list