The use of vagrant in Windows

Time:2021-1-25

The use of vagrant in Windows

Download and install VirtualBox:www.virtualbox.org/

Download and install vagrant:www.vagrantup.com/

Download the box you need to use:

Add box

Add a Debian box as follows

E:\ubuntu\vagrant-box-ngixn-php-fpm-mysql-redis-nodejs>vagrant box add  debian p
ackage.box
==> box: Adding box 'debian' (v0) for provider:
    box: Downloading: file://E:/ubuntu/vagrant-box-ngixn-php-fpm-mysql-redis-nod
ejs/package.box
    box: Progress: 100% (Rate: 11.0M/s, Estimated time remaining: --:--:--)
==> box: Successfully added box 'debian' (v0) for 'virtualbox'!

vagrantfile

Under any vagrant project, there is a vagrant file, just like a Makefile, which is used to configure the virtual machine information created by the pleasure of vagrant.

Using vagrant

Add image
Vagrant box add ABC boxpath [url | path] # ABC unknown name
Development to Development Directory
1. Vagrant init ABC # initialization

    D:\work\test>vagrant init debian
    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.

2. Vagrant up #

    D:\work\test>vagrant up
    Bringing machine 'default' up with 'virtualbox' provider...
    ==> default: Importing base box 'debian'...
    ==> default: Matching MAC address for NAT networking...
    ==> default: Setting the name of the VM: test_default_1413449093680_48484
    ==> default: Clearing any previously set network interfaces...
    ==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
    ==> default: Forwarding ports...
    default: 22 => 2222 (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 timeout. Retrying...
    default: Warning: Connection timeout. Retrying...
    ==> default: Machine booted and ready!
    ==> default: Checking for guest additions in VM...
    ==> default: Mounting shared folders...
    default: /vagrant => D:/work/test

3. Vagrant SSH # SSH login

    D:\work\test>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/zhangwei_f/.vagrant.d/insecure_private_key

Log in with xshell after opening SSH

network configuration

There are three modes of vagrant’s network

1. Port mapping is more commonly used, that is, mapping the ports in the virtual machine to the corresponding ports of the host computer for direct use. Configure in the vagrantfile:

config.vm.network :forwarded_port, guest: 80, host: 8080
Guest: 80 represents port 80 in the virtual machine, host: 8080 represents port 8080 mapped to the host.

    After this is enabled, if vagrant has been started, enter vagrant reload on the command line to restart the machine, and then it can be used again localhost:8080 To access the localhost:80  .

2. If you need to access the virtual machine freely, but others do not need to access the virtual machine, you can use private_ Network, and set IP for the virtual machine. Configure in the vagrantfile:

config.vm.network :private_network, ip: "192.168.1.104"
192.168.1.104 indicates the IP address of the virtual machine. If multiple virtual machines need to access each other, they can be set in the same network segment

3. If you need to use the virtual machine as a computer in the current local area network for DHCP, configure it in the vagrantfile

config.vm.network :public_network
Directory mapping:

Since it is a development environment, the development work must be completed locally, rather than in the virtual machine. The virtual machine will run the service well in the background, otherwise it will put the cart before the horse. Therefore, we need to use the directory mapping function to map the local directory to the corresponding directory of the virtual machine.

By default, the current working directory will be mapped to the / vagrant directory of the virtual machine. The files in the current directory can be accessed directly under / vagrant. Of course, you can also create a soft connection through LN, such as

ln -fs /vagrant/wwwroot /var/www

For directory mapping, of course, from the perspective of automatic configuration, you don’t need to enter the system without entering the system, so you can also perform directory mapping in vagrant

config.vm.synced_folder "wwwroot/", "/var/www"

The previous parameter “wwwroot /” indicates the local path. Here, the relative path to the working directory is used. Here, the absolute path can also be used, for example: “D / www /”

The following parameter “/ var / www” represents the corresponding mapped directory in the virtual machine.

Some orders
Vagrant up
Vacuum halt
Vagrant suspend (virtual machine is suspended only. Information such as virtual machine memory will be saved locally in the form of state file and can be used after recovery operation)
Vagrant resume (resume virtual machine corresponding to previous pause)
Vagrant destroy (after deleting the virtual machine, the configuration in the current virtual machine will not be retained except the configuration in the vagrant file)
Vagrant reload (restart)

This work adoptsCC agreementReprint must indicate the author and the link of this article

You’re far from it!