Vagrant is a software for simple virtual machine operations, and there are several benefits of using virtual machines:
1, in order to develop the environment and production environment consistent (many development environment for Windows and production environment for Linux), do not appear in the development environment normal and go to the formal production environment, there are various problems, and vagrant by sharing files, You can implement code operations in the IDE under the host (Windows) and run directly on the virtual machine (Linux) to show the effect.
2, in the vagrant only need to match a development environment, and then you can have a good environment system image package sent to other colleagues, other colleagues only need to download vagrant and VirtualBox, and then configure the shared directory can be developed, There is no need to worry about configuring the environment anymore.
3, there are many extensions such as Swoole, Redis and so on to Linux support better, and even some only support Linux, so the use of virtual machines, no longer need to learn new technology to find excuses.
Related environment Machine software version: Host: Win7; virtual machine: CentOS 6.5 x86_64;vagrant:1.9.5;virtualbox:5.2
First, the installation steps are as follows:
1, download VirtualBox, address point here, install
2, download vagrant, address point here, install
3, download the image, there are several places to download: http://www.vagrantbox.es/and all can, here I casually find a centos65-x86_64-20140116
4, will download the image load, by the way, the 3rd step can not use, Vagrant Support Online installation image, but because of the Great Wall, so it is best to download the image by other means, and then load locally, open cmd, enter the following command:
# command is this, title for their own name, URL for the box address, can be online or local # vagrant box Add {title} {url}# actual installation command, locally downloaded vagrant box add centos6.5_64 e:\b Oxes\centos65-x86_64-20140116.box
5, find A/new directory, such as Vagrant_project, and then initialize the environment
#新建目录mkdir vagrant_project# Open this directory CD vagrant_project# initialize, centos6.5_64 for the titlevagrant init centos6.5_64 you set above
After the initialization is successful, a file will appear Vagrantfile
in this directory, this is the vagrant configuration file, and the subsequent configuration is modified here.
6, first use the default configuration, to start the following:
Vagrant up
A series of boot messages are displayed:
==> default:clearing Any previously set forwarded ports...==> default:clearing any previously set network INTERFAC Es...==> default:preparing Network interfaces based on configuration ... Default:adapter 1:nat default:adapter 2:hostonly==> default:forwarding ports ... Default:22 (Guest) = 2222 (host) (Adapter 1) ==> default:running ' Pre-Boot ' VM customizations...==> Default:bo Oting vm...==> default:waiting to boot. This could take a few minutes ... Default:ssh address:127.0.0.1:2222//Note here default:ssh username:vagrant//here default:ssh auth method:private key
.......
If you do not give an error is the start of success, if there is a mistake Google or Baidu, my side of the main error for the infinite card in the default:ssh auth method:private key This place, a big possibility is that the machine does not turn on virtualization technology, Restart the computer, press F2 or other keys to enter the BIOS settings, to find the virtual related words can be opened.
7, remote connection to Linux, at this time windows can be used putty or Xshell to connect, host address such as the above logo Note: 127.0. 0.1:2222, user name password is:vagrant
At this point, the default shared folder for the relationship is the vagrant_project folder under Windows corresponding to the Linux /vagrant folder, you can set up a few files on both sides of the test if the two parties are synchronized
8, close orders, because there are many need to configure, temporarily can be closed, and so on after the configuration is completed before starting
#关闭vagrant halt# Restart vagrant Reload
Second, network configuration, shared directory and other related configuration
The above only shows the installation and open simple usage, but really can be used for development also need additional configuration, open the above mentioned Vagrantfile configuration file to configure, many are commented out, choose to remove
1, the network configuration, vagrant has three kinds of configuration methods: Official website document point here to view
A, port mapping, which means that the port of the virtual machine is mapped to the port of the host, the host LAN can access this port to visit your virtual machine on the thing
Config.vm.forwarded_port 80, 8080
The purpose of this is to map the 80 port of the virtual machine to the 8080 port of the host, then I directly access the host's 8080 port is equivalent to access the virtual machine 80 port
b, private network, only the host can access the virtual machine, the main clause LAN members can not access the contents of the virtual machine
Config.vm.network "Private_network", IP: "192.168.33.10"
When the above settings are complete, In this machine can be accessed through 192.168.33.10来 virtual machine, you need to note that 192.168.33.10 although can be arbitrarily set, but if the host IP is a 192.168.1.xxx field, then do not use this section, can 2.xxx or 3.xxx or other, so as not to cause conflict.
c, a common network, the host area Network members can access the contents of the virtual machine, the virtual machine equivalent to a member of the LAN
Config.vm.network "Public_network", IP: "192.168.1.120"
The above configuration in general, the development environment does not require access to local area network members, so most of the cases are selected with B, private network to develop, this can be selected according to the actual situation.
2, shared directory, users can customize the shared directory, in the Vagrantfile configuration file configuration:
#禁用原有的默认的共享目录config. Vm.synced_folder '. ', '/vagrant ', disabled:true# Add a new shared directory, the second parameter is the current folder as the benchmark Config.vm.synced_ Folder "abc", "/www/web/abc",
The/www/web/abc folder under Linux corresponds to the vagrant_project/abc folder under Windows
In addition to the ability to customize shared files, you can also choose how to share, vagrant there are four ways to share, official documents please click here to view
A, NFS sharing, the Windows platform cannot use this configuration, and in this way cannot modify the file owner and all groups
B, rsync sharing method, all platforms can be used, but this approach seems to have a disadvantage is that the host changes can be synchronized to the virtual machine, but the changes in the virtual machine cannot be synchronized to the host, if you want to achieve bidirectional synchronization may also need to do other settings, because no use, so not very clear.
C, SMB sharing method, only the Windows platform can be used, had to find some information to configure, but has not been successful, so the specific is not very clear, but it seems that the efficiency is very good.
D, VirtualBox sharing method, this is VirtualBox provided is also vagrant default sharing method. When the number of files in the hour is good, when the number of large files open the page will be stuck to timeout.
In short, if the host is a Mac, it is recommended to share with NFS, if Windows is recommended for SMB or rsync, if the file is very small, then the default is fine.
But in addition to said above, Windows actually also has the way to speed up the website access speed, that is to use Vagrant winnfsd This plug -in, plug-in address point here, this way is the easiest way to improve speed under windows:
Vagrant Plugin Install VAGRANT-WINNFSD
After the installation is complete, the Type is NFS, although the official website says that NFS cannot be applied to Windows, but with this plugin it is ready.
Config.vm.synced_folder "abc", "/www/web/abc", type: "NFS"
Under test, this plug-in is still very effective, the original open yii2 frame file will time out, or 10, more than 20 seconds, but with this after the response within 2s, or acceptable. Also provides a reference material: Allow vagrant to use NFS/SMB shared folders under Windwos to resolve the slow directory sharing IO
Third, domain name access and packaging distribution
1, domain name access, mainly virtual local domain name configuration problems
Here is still installed lanmp_v3.1, specific installation method can see the previous article: Linux under the installation of LANMP environment or to download the official website to find installation instructions
Create a new site, specify the directory to the shared directory, take a virtual domain name: www.abc.com
The test needs to be modified in the host, as follows: (As for the virtual machine in the/etc/hosts file, the test changes and no modification does not affect access)
192.168.33.10 www.abc.com192.168.33.10 abc.com
You can then display the contents of the virtual machine directly in the browser. The IDE modifies the file directly in Windows, which is displayed in real-time through this URL.
2. Packing and distributing
Vagrant Package
Wait a while, will find in the directory to generate a Package.box file, this is the image that already contains the installation environment, send this image to the colleague, let its installation above the steps to load the image vagrant box add {title} {URL} command, Then configure your own network and shared folders can be directly developed, away from the trouble of reconfiguring the environment.