Vagrant is a tool for building a virtual development environment that is ideal for developing Web applications in languages such as Php/python/ruby/java, which is a history of "code running on my machine without problems".
We can package a Linux development environment through Vagrant and distribute it to team members. Members can develop programs on their favorite desktop system (Mac/windows/linux), but the code can be unified in a packaged environment to run, very domineering.
Install Step 1. Installing VirtualBox
Virtual machines still have to rely on VirtualBox to build, free small.
: Https://www.virtualbox.org/wiki/Downloads
* Although Vagrant also supports VMware, VMware is charged, and the corresponding Vagrant version is also charged
2. Installing Vagrant
: http://downloads.vagrantup.com/Follow the prompts step by step installation.
In addition, you have to download the official encapsulated base image:
Ubuntu Precise VirtualBox Http://files.vagrantup.com/precise32.box
Ubuntu Precise VirtualBox Http://files.vagrantup.com/precise64.box
If you want to mirror other systems, you can download them here: http://www.vagrantbox.es/
* In view of the domestic speed, we separate the steps to download the image separately.
3. Add image to Vagrant
Let's say we downloaded the image storage path is ~/box/precise64.box
, in the terminal input:
$ vagrant box add hahaha ~/box/precise64.box
hahaha
It's the name we give to this box, the path to the ~/box/precise64.box
box.
4. Initializing the development environment
Create a development directory (such as: ~/dev
), you can also use the existing directory, switch to the development directory, with hahaha
the image to initialize the current directory environment:
cd ~/dev # 切换目录$ vagrant init hahaha # 初始化$ vagrant up # 启动环境
You will see that the terminal shows the start-up process, and after the boot is complete, we can login to the virtual machine with SSH, and the rest of the steps are to configure the various environments and parameters you want to run in the virtual machine.
$ vagrant ssh # SSH 登录$ cd /vagrant # 切换到开发目录,也就是宿主机上的 `~/dev`
~/dev
directory corresponding to the directory in the virtual machine is/vagrant
Windows users Note : The Windows terminal does not support SSH, so you need to install a third-party SSH client, such as: Putty, Cygwin and so on.
5. Other Settings
After the Vagrant initialization succeeds, a configuration file is generated in the initialized directory Vagrantfile
, which can be customized by modifying the configuration file.
Vagrant By default is to use port mapping method to map the port of the virtual machine locally to achieve similar http://localhost:80
access, this way, it is troublesome to open and modify the port when you have to edit. In comparison, the host-only mode appears to be much more convenient. Open Vagrantfile
, remove and save the comments from the following line #
:
:private_network, ip: "192.168.33.10"
Restart the virtual machine so that we can 192.168.33.10
access the machine, you can change the IP to another address, as long as there is no conflict on the line.
6. Packaging and distribution
When you have configured your development environment, exit and shut down the virtual machine. To package the development environment in the terminal:
package
After the package is finished, a file is generated in the current directory package.box
, which is passed to other users, and other users can get an identical development environment simply by adding the box and initializing their own development directory.
7. Common commands
$ vagrant init # 初始化$ vagrant up # 启动虚拟机$ vagrant halt # 关闭虚拟机$ vagrant reload # 重启虚拟机$ vagrant ssh # SSH 至虚拟机$ vagrant status # 查看虚拟机运行状态$ vagrant destroy # 销毁当前虚拟机
For more information, please refer to the official documentation http://docs.vagrantup.com/v2/cli/index.html
8. Precautions
The use of Apache/nginx will appear after the changes in the film, but the page refresh is still an old file, due to the static file cache. The Apache/nginx configuration file in the virtual machine needs to be modified:
# Apache 配置添加:EnableSendfile off# Nginx 配置添加:sendfile off;
This is how the Segmentfault team unifies the development environment.
This article uses the program version VirtualBox 4.2.16,vagrant 1.2.7
Build a cross-platform development environment with Vagrant