2.2 vagrant&VirtualBox for Mac


Vagrant Download

  • Vagrant is essentially a command-line version of VirtualBox, which relies on VirtualBox
  • It uses the command line to perform various operations of VirtualBox to improve the efficiency of development
  • The results of vagrant implementation can also be presented seamlessly on VirtualBox.
  • Various tests can be done on various systems without polluting the system running on the physical host

Vagrant and docker have many similarities and differences. In a word, vagrant is used to manage virtual machines and docker is used to isolate application environments.https://www.vagrantup.com/downloads.html

vagrant -v
Vagrant 2.1.1

Install on CentOS

wget https://releases.hashicorp.com/vagrant/2.1.2/vagrant_2.1.2_x86_64.rpm
yum install -y vagrant_2.1.2_x86_64.rpm

Vagrant mirror

Vagrant box add only downloads images and does not immediately create a virtual machine

vagrant box list/add/remove

Vagrant virtual machine

CLI list


1. Create centos7 system

#A vagrantfile file is generated
vagrant init centos/7
#Create a startup centos7 based on the vagrantfile configuration file
vagrant up
#Enter CentOS 7 system after startup
vagrant ssh

If you need to create other systems using vagrant, you can accesshttps://app.vagrantup.com/box…, view a list of all systems

If you pursue a stable version, you can give priority to using ithttps://app.vagrantup.com/bento

2. View centos7 status

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`.

3. Shutdown

vagrant halt

4. Restart

vagrant reloadBy default, the vagrantfile file is updated exceptconfig.vm.provisionConfiguration other than configuration, and then restart the system.
use--provisionOption to force the updateconfig.vm.provisionto configure

vagrant reload

5. Change root to the default user entering the system

By default, we log in with an account with a user name of vagrant and a password of vagrant. But not before executing the commandsudoThis often leads to permission problems. Therefore, you can change the default account to root in the following ways:

Password login

First log in to the system with the vagrant account, and thensu -Switch to the root user. The default password of the root user isvagrant

modify/etc/ssh/sshd_configFile, modify to or add the following configuration, and thensystemctl reload sshdRestart sshd service

In case of password login:

PermitRootLogin yes
PasswordAuthentication yes

In case of certificate login:

PermitRootLogin yes
PubkeyAuthentication yes

Finally, edit the vagrantfile file and add the following configuration,vagrant reloadAfter revagrant sshthat will do

In case of password login:

config.ssh.username = 'root'
config.ssh.password = 'vagrant'

In case of certificate login:

config.ssh.username= "root"

private_ key_ If path does not display the declaration, vagrant will report an error:

Authentication failure. Retrying...

Phpstorm currently does not support syntax highlighting for vagrant files. Use IntelliJ to open the project, and then install a ruby plug-in to realize syntax highlighting

Vagrant actual combat

  • Create a custom vagrant
  • Configure lamp and LNMP environments
  • Ability to run yii2, laravel, ThinkPHP

1. Initialize and start the virtual machine

The default root password of vagrant is vagrant. In order to facilitate memory, we set the password of each software in the virtual machine to vagrant

2. Software installation

  • nginx
  • mysql
  • php

3. Advanced knowledge

  • Port forwarding realizes the mapping between the host and the virtual machine port. The port setting of the host must be greater than 1024

According to the following configuration, we can access the contents of port 80 in the virtual machine through port 8080 on the host machine

  config.vm.network "forwarded_port", guest: 80, host: 8080
  • Shared directory can realize the synchronization of code and resource files

4. Package distribution

Required before packagingvagrant haltVirtual machine, note that the output directory of the package file must not be placed in the directory of vagrant data synchronization. You know the reason…

vagrant package --output xxx.box
Vagrant package -- output xxx.box -- base virtual machine name

If we give others a 1.0 box and then distribute a 2.0 box, and old users want to upgrade, the publisher can write the upgrade script in the shell part of the vagrantfile file

If we passsudo vagrant upCreated a VM, but usedvagrant statusTo read vmzhuang Tai

The VirtualBox VM was created with a user that doesn't match the
current user running Vagrant. VirtualBox requires that the same user
be used to manage the VM that was created. Please re-run Vagrant with
that user. This is not a Vagrant issue.
The UID used to create the VM was: 502
Your UID is: 501

If someone else gets the packaged. Box file, use the following command to use it

vagrant init siguoya.box
vagrant up

5. Common configuration

Vagrant.configure("2") do |config|
  # https://docs.vagrantup.com.
  config.vm.box = "centos/7"
  config.vm.hostname = "centos"
  config.vm.box_check_update = false
  config.disksize.size = "60GB"
  # config.vm.network "forwarded_port", guest: 80, host: 8080
  #Configure private_ Network benefits:
  #- there is no need to configure port forwarding, which breaks through the problem that the port setting of port forwarding host cannot be less than 1024
  #- you can use NFS for file synchronization to avoid file type inconsistency
  #Remember not to conflict with the network segment where the host is located. For example, when my host is and the IP is set to, the virtual machine cannot be started
  #Set auto_ Config is true to avoid problems caused by private IP when packaging and distributing to others and ensure normal use
  config.vm.network "private_network", ip: "", auto_config: true
  #Due to the VM bug, you need to set the sendfile of nginx to false, otherwise the code may not take effect immediately
  #Config.vm.synced before 10.15_ folder "/usr/project/code", "/usr/project/code", :nfs => true
  config.vm.synced_folder "/System/Volumes/Data/usr/local/coding/code", "/usr/local/coding/code", type: "nfs" , nfs_version: 3, nfs_udp: false
  #Because the file systems inside and outside the virtual machine are inconsistent, the default folder synchronization of vagrant will report an error, so it is prohibited in the following ways
  config.vm.synced_folder ".", "/vagrant", :disabled => true
  config.vm.provider "virtualbox" do |vb|
    vb.name = "vagrant_siguoya"
    vb.memory = "2048"
    vb.cpus = "2"

After NFS synchronization is configured,vagrant upYou will be asked to enter a password. If you don’t want to enter a password, you cansudo vi /etc/sudoersAdd the following:

Cmnd_Alias VAGRANT_EXPORTS_ADD = /usr/bin/tee -a /etc/exports
Cmnd_Alias VAGRANT_NFSD = /sbin/nfsd restart
Cmnd_Alias VAGRANT_EXPORTS_REMOVE = /usr/bin/sed -E -e /*/ d -ibak /etc/exports

common problem

If/etc/exportsThe contents of the document are as follows:

# VAGRANT-BEGIN: 501 79c27c9a-9f3f-4ac7-b2b1-fb453217fb92
"/usr/project/code" -alldirs -mapall=501:20
# VAGRANT-END: 501 79c27c9a-9f3f-4ac7-b2b1-fb453217fb92
# VAGRANT-BEGIN: 501 0b26c233-7623-4acd-931a-670b8ecbfcd4
"/usr/project/code/zy108830/docker-demo/vagrant-network/labs" -alldirs -mapall=501:20
# VAGRANT-END: 501 0b26c233-7623-4acd-931a-670b8ecbfcd4

An error will be reported:

NFS is reporting that your exports file is invalid. Vagrant does this check before making any changes to the file. Please correct the issues below and execute "vagrant reload":
exports:5: /usr/project/code/zy108830/docker-demo/vagrant-network/labs conflicts with existing export /usr/project/code

If you had some previous images/boxes installed in your VirtualBox installation, so there were a few invalid entries in /etc/exports already. So you had to clean up that file and restart your Vagrant box.

sudo rm /etc/exports && sudo touch /etc/exports
vagrant halt && vagrant up --provision