1.1 Project Initialization
Vagrant uses the vagrant file as the configuration file, which defines the root directory of the project, and many of the vagrant configurations are based on the root directory of this configuration, and it also describes the type of machine and other resources (such as what software is installed) that the project will run on.
To initialize a vagrant project, use the command vagrant init command to create a configuration file named Vagrantfile in the current directory that modifies the behavior of the Vagrant project.
$ mkdir vagrant_getting_started
$ CD vagrant_getting_started
$ vagrant Init
Vagrant uses boxes (box) to create a development environment, the so-called box is a configured base of the virtual machine image, vagrant avoid every time to create a project to re-create the virtual machine, but according to the Vagrantfile file configuration from the base image creation of the box.
You can use the Vagrant init command on an already existing project, which creates only the configuration file and does not affect the original project. If you want, you can add the profile Vagrantfile to your project's version control so that other developers on your team can quickly set up the development environment to run and debug.
When creating a vagrant project, the first thing you need to do is to add a box, by using the command Vagrant box add to add a case, the vagrant site provides a lot of other developers to share the box, we can directly reuse, in vagrant Cloud can find the box you need and add it locally.
$ vagrant Box Add chef/centos-6.5
The added box can be used at the same time by multiple items, and the project does not modify the added base box when it initializes the box, but instead clones a copy of the box and configures it on the replica.
After adding the box, we need to configure the project to use this box as the basis for modifying the Vagrantfile file:
Vagrant.configure ("2") do |config|
Config.vm.box = "chef/centos-6.5"
End
Note: If the box specified here has not been added, vagrant will automatically download the box.
1.2 Start and run the project
After the configuration is complete, you can start the virtual environment and use the command vagrant up to start the development environment.
$ vagrant Up
After booting, we can connect to the virtual machine using SSH:
$ vagrant SSH
Now that you're connected to the virtual machine, you can do what you want to do as you would a normal Linux system.
Note that you should be cautious about RM-FR/operations on the virtual machine because, in the virtual machine, you mount the/vagrant directory, which is shared with your host's project, and deletes the item if deleted.
After the virtual machine is used, you can remove the virtual machine by using the vagrant Destroy command if you no longer need it.
Tip: Under the default configuration, the/vagrant directory in the virtual machine is the same directory as the project directory on the host, and all operations in that directory are synchronized automatically.
1.3 Creating a Project initialization script
It's a hassle to reinstall the software every time the system is initialized, so vagrant provides a simple way to help us complete this process by specifying that the script be executed automatically when the virtual machine is built.
Create a bootstrap.sh script file under the project directory:
#!/usr/bin/env Bash
Apt-get Update
Apt-get install-y apache2
Rm-rf/var/www
Ln-fs/vagrant/var/www
This script is used in the Ubuntu environment, if you are using the box is CentOS, you can not use the Apt-get command, but you should use the Yum command to complete the installation of the program.
Next, we need to configure the Vagrantfile file so that it automatically executes the script when the environment is created.
Vagrant.configure ("2") do |config|
Config.vm.box = "Hashicorp/precise32"
Config.vm.provision:shell, Path: "Bootstrap.sh"
End
After this configuration, the script executes automatically when you start the virtual machine with the command vagrant up, completing the initialization of the virtual machine.
If your virtual machine is already running, you can use the command vagrant Reload--provision quickly restart the virtual machine,--provision in the command tells Vagrant to run the Provisioners configuration at reboot, Typically, the provisioners configuration is performed only when the vagrant up command is executed.
1.4 Network Configuration
Vagrant provides port forwarding, by mapping the ports in the virtual machine to different ports on the host, we can use the mapped ports in the host to access the services in the virtual machine.
For example:
Vagrant.configure ("2") do |config|
Config.vm.box = "Hashicorp/precise32"
Config.vm.provision:shell, Path: "Bootstrap.sh"
Config.vm.network:forwarded_port, host:4567, guest:80
End
Once you have started the virtual machine as configured, you can use http://127.0.0.1:4567 on the host to access the Web services provided by Apache on the virtual machine.
1.5 teardown (Virtual machine Uninstall)
When you want to switch to other projects to develop or when you're going out for lunch, when you get off work, you need to clean up your development environment, Vagrant offers three ways, each with its own pros and cons, so you need to weigh the way you want to use it.
Suspending (suspend) Execute command vagrant suspend will save the current virtual machine running state and stop it, as the host hangs is the same, when you need to start again, use the vagrant Up command will restore the previous state of operation. The advantage of this approach is that it starts very quickly and can quickly enter the development environment, with the disadvantage that it takes up more disk space because the virtual machine stores the data in memory on disk.
Halting (stop) this way is normal to stop the virtual machine, using the virtual machine Shutdown command to complete, when you need to reuse the use of the command vagrant up restart the virtual machine, this method and computer cold boot, the disadvantage is that the startup time is more time-consuming, still occupy some of the disk space.
Destroying (Destroy) This method will destroy the virtual machine, no longer occupy disk space, the next time you start will re-establish the development environment, including downloading software and so on.
vagrant Use Tips
1.1 How to Debug
In the use of vagrant process, there will inevitably be some instability factors caused by some of the use of bugs, therefore, vagrant provides the debug log method for debugging, you can easily find the cause of the error in order to quickly solve the problem.
To allow logging enabled, you need to set the environment variable Vagrant_log to the required log level, including debug, info, warn, and error. Log level info is usually used when you need to find the cause of the error, in which case you can get a cleaner log that contains important information.
On Linux and Mac systems, you can set this up in the following ways:
$ vagrant_log=info vagrant Up
Under Windows, you need to write separately:
$ set Vagrant_log=info
$ vagrant Up
Of course, you can not use this way, just need to increase the parameters at the start of the--debug can be.
$ vagrant Up--debug
1.2 How to use Plug-ins
Vagrant provides a number of plug-ins that can be used directly, using these plug-ins can greatly reduce the workload of the Configuration development environment, vagrant most of the core functions are based on plug-ins.
Vagrant uses the command vagrant plugin install to install Plug-ins.
$ vagrant Plugin Install Vagrant-example-plugin
When the plug-in is installed, it will automatically load when the vagrant is started, and if there is an error in the plug-in loading process, it will not affect the start of the vagrant, but the error message that the plug-in failed to load is output.
After the plug-in installation is complete, you should view the plugin development document to obtain how to use the plug-in, generally can use the plug-in command via the Vagrant command, if the plug-in provides provision, can be configured by config.vm.provision.
Plug-in updates are simple, using the command Vagrant Plugin update to update all plug-ins, using vagrant plugin update name to update the specified plug-in.
The plug-in uninstall uses the command vagrant plugin uninstall.
$ vagrant Plugin Uninstall Vagrant-example-plugin
To see which plug-ins are installed:
Vagrant Plugin List
1.3 How to configure your network
In vagrant, all network configurations are in the Config.vm.network method in Vagrantfile.
Vagrant.configure ("2") do |config|
# Other Config here
Config.vm.network "Forwarded_port", guest:80, host:8080
End
Each network type has an identifier such as: Forwarded_port, followed by a string of configuration options. In the example of a port jump, two parameters are provided, the port number in the virtual machine, and the port number mapped in the host.
If you need more than one network configuration, you can use multiple config.vm.network.
How to make a port jump
The port Jump feature allows you to directly access the specified port on the host via TCP or UDP, and the port's request is forwarded to the specified port on the virtual machine.
Vagrant.configure ("2") do |config|
Config.vm.network "Forwarded_port", guest:80, host:8080
End
The configuration above will forward access to port 8080 on the host to port 80 on the virtual machine.
The following are the parameters supported by this configuration (Forwarded_port):
Guest (int) wants to leak to the host's virtual machine port
GUEST_IP (String) wants to bind the IP, the IP and guest specified port to the host, default is NULL, all network card interface.
Host port number (int) that you want to use to access the guest port of the virtual machine
The IP address of the HOST_IP (string) host, used to bind to the port of the jump, and all IP if not specified.
Protocol (string) optional TCP or UDP, TCP is the default.
Vagrant.configure ("2") do |config|
Config.vm.network "Forwarded_port", guest:2003, host:12003, protocol: ' TCP '
Config.vm.network "Forwarded_port", guest:2003, host:12003, protocol: ' UDP '
End
1.4 How to configure the Sync directory
Use the Config.vm.synced_folder method to configure the synchronization directory.
Vagrant.configure ("2") do |config|
# Other Config here
Config.vm.synced_folder "src/", "/srv/website"
End
In the above configuration, the first parameter of the Synced_folder method is the directory to synchronize with the virtual machine on the main machine, and the second parameter is the path to be mounted on the virtual machine.
You can configure parameter disabled:true to prevent directory synchronization.
Vagrant.configure ("2") do |config|
Config.vm.synced_folder "src/", "/srv/website", disabled:true
End
By default, vagrant sets the owner/group of the Sync folder as an SSH user and, if necessary, uses the following configuration:
Config.vm.synced_folder "src/", "/srv/website",
Owner: "Root", Group: "Root"