Vagrantのインストールと設定
Vagrantの操作はコンソール(Macならターミナル)で行うこととなる。
前提
VirtualBox
https://www.virtualbox.org/wiki/Downloads
Vagrant
https://www.vagrantup.com/downloads.html
上記がインストールされていること。
※ VirtualBox→Vagrantの順番でインストールしておくとトラブルが少ない。
Vagrantのインストール
ダウンロードしたVagrantのインストールファイルを叩けば完了する。
便利設定
仮想マシンGuest Addtionsの自動更新を有効化する。
1. $ vagrant plugin install vagrant-vbguest
Vagrantfileの用意
Vagrant用の設定ファイル。VirtualBoxへのOS導入も自動でやってくれる。
--Vagrantfileの内容--
Vagrant.configure("2") do |config|`
config.vm.box = "[任意のvm名称]"
config.vm.box_url = "[仮想マシンで利用するBOXのURL]" // 仮想イメージの作り方等は別記事で。
config.ssh.password = "[仮想マシン上でのVagratユーザのpassword]"
config.vm.network "forwarded_port", guest: 10080, host: 10080 // httpのポート設定
config.vm.network "forwarded_port", guest: 10443, host: 10443 // httpsのポート設定
---- ホストOSとゲストOSで共有するフォルダ(例としてWebアプリで使用するフォルダを記載)
config.vm.synced_folder "html", "/var/www/html/", create: true, mount_options:
['dmode=777','fmode=777']
config.vm.synced_folder "ssl", "/var/www/ssl/", create: true, mount_options:
['dmode=777','fmode=777']
config.vm.synced_folder "log", "/var/log/httpd/", create: true, mount_options:
['dmode=700','fmode=644'], owner: "root", group: "root"
# 共有フォルダ設定でログフォルダが作り直されてログ保存ができなくなるのでApacheを再起動してフォルダを認識させる
config.vm.provision "shell", run: "always", inline: "service httpd restart"
end
Vagrantの起動
Vagrantfileが設置されているディレクトリで以下のコマンドを実行する。
$ dir /b // Vagrantfileがあることを確認する。
Vagrantfile
$
$ vagrant up // Vagrantの実行。
Bringing machine 'default' up with 'virtualbox' provider...
~~
default: Running: inline script
default: Redirecting to /bin/systemctl restart httpd.service
$
※ 上記仮想マシンは10080,10443,2222番ポートを使用しますので、既に使用している場合は他ソフトを閉じるかVagrantfileの中身を書き換えてる必要がある。
vagrantが起動すると、ブラウザから http://localhost:10080 または https://localhost:10443 で自身の仮想マシンにアクセスすることができる。
※ Windowsの記事だが、Macでもほぼ変わらない操作で導入できる