Memo

メモ > サーバ > 各論: Vagrant > 起動

■起動
■準備 XAMPPなど何らかのローカル環境を使っている場合、念のためあらかじめ終了させておく ■VirtualBoxインストール ※環境によっては、最新版では動作しないことがあるので注意 ※2022年3月の時点でWindows11にインストールする際、 「VirtualBox 6.1.32 + Vagrant 2.2.19」なら動作した(この時点での最新版) Docker Toolbox は使用しないので、このまま進める ※2021年7月の時点でWindows10にインストールする際、 「VirtualBox 6.1.24 + Vagrant 2.2.17」なら動作した(この時点での最新版) Docker Toolbox は使用しないので、このまま進める ※2020年12月にWindows10をアップデートをした Windows10にインストール済みだったVagrantが起動しなくなった 「VirtualBox 6.1.16 + Vagrant 2.2.14」なら動作した(この時点での最新版) いったん Docker Toolbox の利用は急がないので、このまま進める ※2020年6月の時点でWindows10にインストールする際、 「VirtualBox 6.1 + Vagrant 2.2.9」なら動作した(この時点での最新版) ただし上記VirtualBoxではDockerが起動しなかった。詳細は Docker.txt を参照 ※2019年8月の時点でWindows10にインストールする際、 「VirtualBox 6.0.10 + Vagrant 2.2.5」なら動作した(この時点での最新版) ただし上記VirtualBoxではDockerが起動しなかったので、後から「VirtualBox-5.2.32-132073-Win.exe」をインストールしなおした ※2018年5月の時点でWindows10にインストールする際、 「VirtualBox 5.2.12 + Vagrant 2.1.1」なら動作した(最新版のVirtualBoxでは、CentOS起動時に「default: SSH auth method: private key」で止まってしまった) ※2018年2月の時点でWindows10にインストールする際、 「VirtualBox 5.1.32 + Vagrant 2.0.1」なら動作した(最新版のVirtualBoxでは、CentOS起動時に「default: SSH auth method: private key」で止まってしまった) https://www.virtualbox.org/ から VirtualBox-6.X.XX-XXXXXX-Win.exe をダウンロード 初期設定のまま C:\Program Files\Oracle\VirtualBox\ にインストール 途中「Warning Network Interfaces」と表示されるが、「Yes」を選択する 途中デバイスソフトウェアのインストールについて確認されるが、「インストール」を選択する Oracle VM VirtualBox マネージャが起動すれば完了 ■Vagrantインストール https://www.vagrantup.com/ から vagrant_2.X.X_x86_64.msi をダウンロード 初期設定のまま C:\HashiCorp\Vagrant\ にインストール PCを再起動 ■CentOS7用のボックスを取得 https://app.vagrantup.com/boxes/search でCentOS7用BoxのURLを確認 (Oracleだったりその他サイトだったり、ダウンロード先はたくさんあるので好みの場所から取得する) 以下にボックスがある Vagrant box centos/7 - Vagrant Cloud https://app.vagrantup.com/centos/boxes/7 以下、Windowsのコマンドプロンプトで操作
>vagrant box list There are no installed boxes! Use `vagrant box add` to add some.
はじめはボックスは無い 以下のようにしてボックスを追加できる
>vagrant box add centos/7
CentOSをダウンロードには時間がかかる 実行しても、反応があるまで少しラグがある 以下のようにして、追加されたボックスを確認できる
>vagrant box list
■参考: app.vagrantup.com 以外から取得する例 http://www.vagrantbox.es/ でCentOS7用BoxのURLを確認
>vagrant box add centos72-x64 https://github.com/CommanderK5/packer-centos-template/releases/download/0.7.2/vagrant-centos-7.2.box ==> box: Box file was not detected as metadata. Adding it directly... ==> box: Adding box 'centos72-x64' (v0) for provider: box: Downloading: https://github.com/CommanderK5/packer-centos-template/releases/download/0.7.2/vagrant-centos-7.2.box box: Download redirected to host: github-production-release-asset-2e65be.s3.amazonaws.com box: ==> box: Successfully added box 'centos72-x64' (v0) for 'virtualbox'! >vagrant box list centos72-x64 (virtualbox, 0)
■Vagrantfile作成
>mkdir C:\Users\refirio\Vagrant\centos7 >cd C:\Users\refirio\Vagrant\centos7
※CentOS7用フォルダを作成して移動。場所やフォルダ名は任意
>vagrant init --minimal centos/7 ==> vagrant: A new version of Vagrant is available: 2.2.13 (installed version: 2.2.9)! ==> vagrant: To upgrade visit: https://www.vagrantup.com/downloads.html A `Vagrantfile` has been placed in this directory. You are now ready to `vagrant up` your first virtual environment! Please read the comments in the Vagrantfile as well as documentation on `vagrantup.com` for more information on using Vagrant.
※Vagrantfile(VMの構成を書くためのファイル)を作成 ※--minimal を省略すると、コメント付きのファイルが作成される ※コマンドを使わずに、手動でVagrantfileを作成してもいい Vagrantfileの内容は以下のとおり。「centos/7」には先程入手したボックスの名前を指定する(任意の名前ではない)
Vagrant.configure("2") do |config| config.vm.box = "centos/7" end
一例だが、テキストエディタで以下のように編集。「192.168.33.10」はサーバにアクセスするときのIPアドレス
Vagrant.configure(2) do |config| config.vm.box = "centos/7" config.vm.box_check_update = false config.vm.network "private_network", ip: "192.168.33.10" # config.vm.network "forwarded_port", guest: 80, host: 80 # config.vm.synced_folder "./code", "/var/www/html/code" end
※上記のうち config.vm.box_check_update は true の方がいいかもしれない。要検証 【備忘録】Vagrant 基本の巻 - Qiita https://qiita.com/shoko_kb/items/1701247996f031d0cf4f vagrant box updateでboxファイルをアップグレードする https://hnakamur.github.io/blog/2015/04/25/vagrant-box-update/ ■仮想マシンを起動
>vagrant up Bringing machine 'default' up with 'virtualbox' provider... ==> default: Importing base box 'centos/7'... ==> default: Matching MAC address for NAT networking... ==> default: Setting the name of the VM: centos7_default_1565852593519_83202 Vagrant is currently configured to create VirtualBox synced folders with the `SharedFoldersEnableSymlinksCreate` option enabled. If the Vagrant guest is not trusted, you may want to disable this option. For more information on this option, please refer to the VirtualBox manual: https://www.virtualbox.org/manual/ch04.html#sharedfolders This option can be disabled globally with an environment variable: VAGRANT_DISABLE_VBOXSYMLINKCREATE=1 or on a per folder basis within the Vagrantfile: config.vm.synced_folder '/host/path', '/guest/path', SharedFoldersEnableSymlinksCreate: false ==> default: Clearing any previously set network interfaces... ==> default: Preparing network interfaces based on configuration... default: Adapter 1: nat default: Adapter 2: hostonly ==> default: Forwarding ports... default: 22 (guest) => 2222 (host) (adapter 1) ==> default: Booting VM... ==> default: Waiting for machine to boot. This may take a few minutes... default: SSH address: 127.0.0.1:2222 default: SSH username: vagrant default: SSH auth method: private key default: Warning: Connection reset. Retrying... default: Warning: Connection aborted. Retrying... default: Warning: Connection aborted. Retrying... default: Warning: Connection reset. Retrying... default: Warning: Connection aborted. Retrying... default: default: Vagrant insecure key detected. Vagrant will automatically replace default: this with a newly generated keypair for better security. default: default: Inserting generated public key within guest... default: Removing insecure key from the guest if it's present... default: Key inserted! Disconnecting and reconnecting using new SSH key... ==> default: Machine booted and ready! ==> default: Checking for guest additions in VM... default: The guest additions on this VM do not match the installed version of default: VirtualBox! In most cases this is fine, but in rare cases it can default: prevent things such as shared folders from working properly. If you see default: shared folder errors, please make sure the guest additions within the default: virtual machine match the version of VirtualBox you have installed on default: your host and reload your VM. default: default: Guest Additions Version: 4.3.30 default: VirtualBox Version: 6.0 ==> default: Configuring and enabling network interfaces... ==> default: Mounting shared folders... default: /vagrant => C:/Users/refirio/Vagrant/centos7
※仮想マシンを起動(初回はBoxのダウンロードとVM生成があるため時間がかかる) ※完了すると、Oracle VM VirtualBox マネージャで確認できる ■SSHでアクセス
>vagrant ssh `ssh` executable not found in any directories in the %PATH% variable. Is an SSH client installed? Try installing Cygwin, MinGW or Git, all of which contain an SSH client. Or use your favorite SSH client with the following authentication information shown below: Host: 127.0.0.1 Port: 2222 Username: vagrant Private key: C:/Users/refirio/Vagrant/centos7/.vagrant/machines/default/virtualbox/private_key
SSHでの接続情報が表示されるので、この情報でSSHログインできる (環境によっては、即座にSSHで操作できる状態になる) もしくは、以下のホスト名とポートでもアクセスできる (Vagrantfile で「config.vm.network "private_network", ip: "192.168.33.10"」と設定した場合) Host: 192.168.33.10 Port: 2222 Vagrantの持つ機能で「ホストOS(この場合はWindows)から127.0.0.1の2222番ポートへアクセスした場合、VagrantのSSHへ転送する」となっている 設定されているのはSSHへのアクセスのみ それとは別にVagrantはネットワークの設定機能を持っている デフォルトで「ホストOSから192.168.33.10の22番ポートへのアクセスは2222番ポートへ転送する」となっているため、SSHアクセスができる これを使えば80番ポートでHTTPアクセスさせることもできる FTP - vagrantのssh-addressとprivate-network-ipの違い(32537)|teratail https://teratail.com/questions/32537 Vagrantで立ち上げた仮想環境にIPアドレスを振ってsshでログインする - Qiita https://qiita.com/grachro/items/4d34a43a9a57946f3693 ■バーチャルマシンを終了する場合
>cd C:\Users\refirio\Vagrant\centos7 >vagrant halt
■バーチャルマシンを破棄する場合
>cd C:\Users\refirio\Vagrant\centos7 >vagrant destroy
■ボックスを削除する場合
>vagrant box remove centos/7
■2022年3月にWindows11にインストールした際の作業内容まとめ
>vagrant box list >vagrant box add centos/7 >vagrant box list >cd C:\Users\refirio\Vagrant\centos7 >vagrant init --minimal centos/7
作成されたVagrantfileを編集
Vagrant.configure(2) do |config| config.vm.box = "centos/7" config.vm.box_check_update = false config.vm.network "private_network", ip: "192.168.33.10" config.vm.network "forwarded_port", guest: 80, host: 80 config.vm.synced_folder "./code", "/var/www/html/code" end
同じ階層に code フォルダを作成
>vagrant up
「Vagrant was unable to mount VirtualBox shared folders.」のエラーが表示された 「トラブル」に記載の手順にあるように、以下を実行
>vagrant plugin install vagrant-vbguest >vagrant halt >vagrant up >vagrant ssh $ sudo yum install -y kernel kernel-devel gcc $ exit >vagrant reload
「環境構築」の手順をもとに、CentOS7環境を作成 引き続き、以下をもとに development 環境を作成 refirio/ansible-development https://github.com/refirio/ansible-development その後、WSL.txt の内容をもとに WSL2+Docker 環境を作成

Advertisement