centos7 官方镜像 https://cloud.centos.org/centos/7/vagrant/x86_64/images/CentOS-7-x86_64-Vagrant-1707_01.VirtualBox.box
建议使用迅雷下载,浏览器下载可能太慢。
实操笔记
mac homebrew 安装 vagrant
$ brew cask install vagrant
Cloning into '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-cask'...
remote: Enumerating objects: 4189, done.
remote: Counting objects: 100% (4189/4189), done.
remote: Compressing objects: 100% (4167/4167), done.
remote: Total 4189 (delta 26), reused 654 (delta 19), pack-reused 0
Receiving objects: 100% (4189/4189), 1.33 MiB | 975.00 KiB/s, done.
Resolving deltas: 100% (26/26), done.
Checking connectivity... done.
Tapped 1 command and 4092 casks (4,197 files, 4.3MB).
==> Satisfying dependencies
==> Downloading https://releases.hashicorp.com/vagrant/2.2.2/vagrant_2.2.2_x86_64.dmg
######################################################################## 100.0%
==> Verifying SHA-256 checksum for Cask 'vagrant'.
==> Installing Cask vagrant
==> Running installer for vagrant; your password may be necessary.
==> Package installers may write to any location; options such as --appdir are ignored.
Password:
installer: Package name is Vagrant
installer: Installing at base path /
installer: The install was successful.
vagrant was successfully installed!
把下载好的box镜像安装到本地
$ vagrant box add lnmp ~/Downloads/CentOS-7-x86_64-Vagrant-1707_01.VirtualBox.box
==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'lnmp' (v0) for provider:
box: Unpacking necessary files from: file:///Users/zhoumengkang/Downloads/CentOS-7-x86_64-Vagrant-1707_01.VirtualBox.box
==> box: Successfully added box 'lnmp' (v0) for 'virtualbox'!
查看已有的 vagrant box 列表
$ vagrant box list
lnmp (virtualbox, 0)
初始化该 box
$ vagrant init lnmp
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.
我们执行 vagrant init <本地 box 名称>
会在当前目录生成 Vagrantfile 文件
启动虚拟机
$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'lnmp'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: vagrant-lnmp_default_1546650046283_10632
==> default: Vagrant has detected a configuration issue which exposes a
==> default: vulnerability with the installed version of VirtualBox. The
==> default: current guest is configured to use an E1000 NIC type for a
==> default: network adapter which is vulnerable in this version of VirtualBox.
==> default: Ensure the guest is trusted to use this configuration or update
==> default: the NIC type using one of the methods below:
==> default:
==> default: https://www.vagrantup.com/docs/virtualbox/configuration.html#default-nic-type
==> default: https://www.vagrantup.com/docs/virtualbox/networking.html#virtualbox-nic-type
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
default: Adapter 1: nat
==> 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: Remote connection disconnect. 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: No guest additions were detected on the base box for this VM! Guest
default: additions are required for forwarded ports, shared folders, host only
default: networking, and more. If SSH fails on this machine, please install
default: the guest additions and repackage the box to continue.
default:
default: This is not an error message; everything may continue to work properly,
default: in which case you may ignore this message.
==> default: Rsyncing folder: /Users/zhoumengkang/vagrant-lnmp/ => /vagrant
登录虚拟机
$ vagrant ssh
Last login: Sat Jan 5 01:03:42 2019 from 10.0.2.2
[vagrant@bogon ~]$
修改虚拟机配置 Vagrantfile
我们修改两项即可,端口映射和目录同步,方便开发。
config.vm.network "forwarded_port", guest: 8080, host: 8080
config.vm.synced_folder "/Users/zhoumengkang/PhpstormProjects/yq-lib", "/home/vagrant/yq-lib"
这样宿主机的/Users/zhoumengkang/PhpstormProjects/yq-lib
就和虚拟机的/home/vagrant/yq-lib
可以同步了。
目录同步可能遇到的问题,解决办法参考 https://mengkang.net/1315.html
虚拟机重启后
vagrant halt && vagrant up
查看目录同步是否正常
$ vagrant ssh
Last login: Sat Jan 5 02:33:05 2019 from 10.0.2.2
[vagrant@localhost ~]$ ls
yq-lib
[vagrant@localhost ~]$ cd yq-lib/
[vagrant@localhost yq-lib]$ ls
boot.php composer.json composer.lock public vendor Yq
安装 lnmp 环境
我比较习惯lnmp.org一键安装包,所以参考官网的做法
yum install wget -y
wget http://soft.vpser.net/lnmp/lnmp1.5.tar.gz -cO lnmp1.5.tar.gz && tar zxf lnmp1.5.tar.gz && cd lnmp1.5
sudo ./install.sh lnmp
问题1 虚拟机内存不够
You will Install MySQL 5.6.40
Memory less than 1GB, can't install MySQL 5.6+ or MairaDB 10+!
修改 Vagrantfile 调整内存
config.vm.provider "virtualbox" do |vb|
vb.memory = 2048
end
问题2 目录没有权限 nginx 403
用lnmp工具设置web 目录为 /home/vagrant/yq-lib/public
,把vagrant
设置为755
打包分享
进入我们生成Vagrantfile
目录里,执行vagrant package
打包
不过最后生成的 box 文件怎么这么大 3.8G