LoginSignup
1
0

More than 1 year has passed since last update.

VirtualBox+Vagrant+AnsibleでLaravel開発環境を構築その壱

Last updated at Posted at 2019-12-24

環境を作る度に色んなサイト見て調べてって・・・大変なので、コピペできるように纏めました。

マシンスペック

  • Mac mini 2018
  • macOS Catalina(10.15.x)
  • Intel Core-i7 3.2GHz 6コア
  • メモリ 32GB
  • SSD 512GB

VM環境

  • CentOS 7.x
  • Nginx 1.x
  • PHP(PHP-FPM)7.3.x
  • MariaDB 10.3.x
  • Laravel 6.x
  • リポジトリ
    • Bitbucket
  • IPアドレス
    • 192.168.56.105
  • ユーザー/パスワード
    • root/vagrant
    • vagrant/vagrant
  • VM名
    • CentOS7

やること

  • VirtualBoxでCentOSのインストール
  • VagrantのBox作成
  • Ansibleで自動構築

補足

各環境を行ったり来たりするので、下記のように記載します。

[VM]# VirtualBoxの中でroot権限で作業
[Cent]$ MacのターミナルからCentOSに接続して作業
[Cent]# MacのターミナルからCentOSに接続してroot権限で作業
[Mac]$ MacのターミナルでMac内の作業

前提

  • VirtualBoxはインストール済み
  • Vagrantはインストール済み

※参考
Vagrant+VirtualBoxのインストール(Mac)

VirtualBoxの設定

  • 名前はCentOS7で作成。
  • NATとホストオンリーアダプターを設定する

※参考
macでVirtualBoxにCentOS7をいれてssh接続する

CentOSのインストール

ISOは下記を使用
http://ftp.iij.ad.jp/pub/linux/centos/7.9.2009/isos/x86_64/CentOS-7-x86_64-Minimal-2009.iso

インストール手順
CentOS 7.6をインストール Linuxのインストールは難しくありません

ネットワーク設定変更

[VM]# cd /etc/sysconfig/network-scripts
[VM]# vi ifcfg-enp0s3
★下記の点線内を編集
---
ONBOOT=yes
---
:wq

[VM]# vi ifcfg-enp0s8
★下記の点線内を編集
---
ONBOOT=yes
BOOTPROTO=static
IPADDR=192.168.56.105
---
:wq

[VM]# systemctl restart network

ターミナルから接続

[Mac]$ ssh root@192.168.56.105
password:vagrant

NIC名を変更

※CentOS6みたいに、デバイス名をifcfg-eth0ifcfg-eth1にしたかっただけの設定なので、したくなければ不要です。

[Cent]# cd /etc/default
[Cent]# cp -p grub grub_yyyymmdd
[Cent]# vi grub

★下記の点線内をコピペ
---
GRUB_TIMEOUT=5
GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)"
GRUB_DEFAULT=saved
GRUB_DISABLE_SUBMENU=true
GRUB_TERMINAL_OUTPUT="console"
GRUB_CMDLINE_LINUX="rd.lvm.lv=centos/root rd.lvm.lv=centos/swap rhgb quiet biosdevname=0 net.ifnames=0"
GRUB_DISABLE_RECOVERY="true"
---
:wq

[Cent]# cp -p /boot/grub2/grub.cfg /boot/grub2/grub.cfg_yyyymmdd
[Cent]# grub2-mkconfig -o /boot/grub2/grub.cfg

[Cent]# cd /etc/sysconfig/network-scripts
[Cent]# mv ifcfg-enp0s3 ifcfg-eth0
[Cent]# mv ifcfg-enp0s8 ifcfg-eth1

[Cent]# vi ifcfg-eth0
★enp0s3をeth0に置換
:%s/enp0s3/eth0/g
:wq

[Cent]# vi ifcfg-eth1
★enp0s8をeth1に置換
:%s/enp0s8/eth1/g
:wq

[Cent]# shutdown -r now

※参考
【CentOS7】NICのデバイス名をeth0へ変更

再起動後に再接続

VirtualBox Guest Additionsのインストール

CDイメージを挿入
https://www.sejuku.net/blog/76014#VirtualBox_Guest_Additions-2

[Cent]# yum -y update
[Cent]# yum -y install kernel kernel-devel perl gcc
[Cent]# yum -y install bzip2
[Cent]# mkdir /media/cdrom
[Cent]# mount -r /dev/cdrom /media/cdrom
[Cent]# sh /media/cdrom/VBoxLinuxAdditions.run
[Cent]# umount /media/cdrom

※参考
VirtualBox Guest Additionsのインストール

sudoの設定

[Cent]# visudo

★下記の点線内を追記
---
vagrant ALL=(ALL)       NOPASSWD: ALL
---
:wq

Vagrantユーザーの作成と確認

[Cent]# useradd -m vagrant
[Cent]# passwd vagrant
[Cent]# su - vagrant
[Cent]$ sudo ls -l /root

vagrant up時の「Warning: Authentication failure. Retrying...」対応

[Cent]$ sudo yum -y install wget
[Cent]$ mkdir -p /home/vagrant/.ssh
[Cent]$ wget --no-check-certificate https://raw.github.com/mitchellh/vagrant/master/keys/vagrant.pub -O /home/vagrant/.ssh/authorized_keys
[Cent]$ chmod 0700 /home/vagrant/.ssh
[Cent]$ chmod 0600 /home/vagrant/.ssh/authorized_keys
[Cent]$ chown -R vagrant /home/vagrant/.ssh
[Cent]$ exit

※参考
vagrant upコマンド実行時にAuthentication failure.エラーが発生する

その他いろいろ

★udevのルールの削除
[Cent]# rm -f /etc/udev/rules.d/70-persistent-net.rules
[Cent]# rm -rf /dev/.udev/
[Cent]# rm -f /lib/udev/rules.d/75-persistent-net-generator.rules

★Box作成のための最適化
[Cent]# yum clean all

★フラグメンテーションの解消
[Cent]# rm -f /EMPTY

[Cent]# shutdown -h now

起動中のVirtualBoxも終了する

※参考
VagrantでオリジナルBoxを作成する

VagrantのBox作成

[Mac]$ cd <Boxを作成するディレクトリ>
[Mac]$ vagrant package --base CentOS7 --output CentOS7.box
[Mac]$ vagrant box add CentOS7 CentOS7.box
[Mac]$ vagrant init CentOS7

作成された Vagrantfile を開いて編集

下記を追加
config.ssh.insert_key = false

下記を編集
# config.vm.network "private_network", ip: "192.168.33.10"
↓
config.vm.network "private_network", ip: "192.168.56.105", auto_config:false

末尾のendの手前に挿入
config.vm.provision "ansible" do |ansible|
  ansible.playbook = "ansible/playbook.yml"
  ansible.inventory_path = "ansible/hosts"
  ansible.limit = 'all'
end

Ansibleのインストール&設定

設定ファイル作りました!
https://github.com/bobtabo/ansible

[Mac]$ sudo chown $USER /usr/local/lib/pkgconfig
[Mac]$ brew install ansible
[Mac]$ ansible --version
ansible 2.9.2

[Mac]$ cd <Boxを作成したディレクトリ>
[Mac]$ git clone https://github.com/bobtabo/ansible.git

※参考
仮想環境入門!Virtualbox, Vagrant, AnsibleでLAMP環境をお手軽構築
AnsibleでAmazonLinux2 + Laravel + mysql5.7 + nginx + SSL環境を構築する。

YAMLエディタにVSCodeを使用する場合
Visual Studio Code のユーザーインタフェイス(UI) 表示を日本語にする - Japanese Language Pack のインストール
[Ansible] Visual Studio Code で Playbook を書く時に便利な拡張たち

vagrant-vbguestのインストール

[Mac]$ vagrant plugin install vagrant-vbguest

※参考
Vagrantでマウントエラーが発生したときの解消方法

環境の構築

[Mac]$ vagrant up

※参考
仮想環境入門!Virtualbox, Vagrant, AnsibleでLAMP環境をお手軽構築

動作確認

[Mac]$ sudo vi /private/etc/hosts
★下記の点線内を追記
---
192.168.56.105 laravel6.local
---
:wq

ブラウザで確認
http://laravel6.local/
無題.png

SSHFSのインストール

https://qiita.com/ysk24ok/items/bb148530a55a4e55d99b
FUSE for macOS はコチラからダウンロード

マウントポイントを作成

[Mac]$ mkdir -p ~/MountPoint/mount

マウント

[Mac]$ sshfs vagrant@192.168.56.105:/home/vagrant ~/MountPoint/mount
password:vagrant

アンマウント

[Mac]$ umount -f ~/MountPoint/mount

PhpStormの設定

https://qiita.com/makies/items/e13b36c449905b2b46cb
http://www.1x1.jp/blog/2014/08/how-to-setup-php-remote-debug-with-vagrant-vm.html

その他

1
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
1
0