Home >Operation and Maintenance >Windows Operation and Maintenance >Methods and precautions for installing Vagrant under Windows

Methods and precautions for installing Vagrant under Windows

巴扎黑
巴扎黑Original
2017-08-22 14:04:291678browse

To do web development, it is indispensable to set up a local development environment. Although various scripts currently have corresponding Windows versions, and even one-click installation packages, in many cases the compatibility with the Windows environment is not so good, and each has its own troubles. The problem is that the actual deployment environment is usually Linux, and the development and deployment environments are often inconsistent, and a lot of debugging is required before going online.

Playing Linux under Windows requires a virtual machine, but installing the system is still quite troublesome. Today, when everything is automated, Vagrant is such an artifact. It is developed based on Ruby and uses open source VirtualBox as virtualization support, which can be easily deployed across platforms.

Today I tried to change the environment of several machines to Vagrant. It was very comfortable and smooth. I personally found that running a centos only requires less than 60 MB of memory, which is quite powerful.

Let’s talk about the installation steps, and pay attention to it by the way!

Preparation work:

Download and install VirtualBox: https://www.virtualbox.org/

Download and install Vagrant: http://www.vagrantup.com/

Download the box you need to use:

Officially provided example: http://files.vagrantup.com/precise32.box

You can also download it at http://www. vagrantbox.es/ Here you can download more different systems and even boxes that have been configured and ready for use. Although you can use the URL directly in Vagrant and Vagrant will automatically download and install it, considering the network situation, it is recommended to download it yourself first.

Since I am used to using CentOS, I downloaded CentOS 6.3 x86_64 minimal. You can choose this Box based on your personal situation.

Set up the environment:

After the installation is completed and the system is restarted, you can start setting up the environment.

First create a working directory locally, and switch to the corresponding directory on the command line

vagrant box add base CentOS-6.3-x86_64-minimal.box

base means specifying the default box, or you can specify a name for the box, such as centos63, when using base , and then you can directly use vagrant init for initialization. If you specify the name yourself, you need to specify the name of the box during initialization.

CentOS-6.3-x86_64-minimal.box is the file name corresponding to the box. This can be the path to save the box locally, or the URL where the box can be downloaded. If it is a URL, Vagrant will automatically start the download. .

[vagrant] Downloading with Vagrant::Downloaders::File...
[vagrant] Copying box to temporary location...
[vagrant] Extracting box...
[vagrant] Verifying box...
[vagrant] Cleaning up downloaded box...

After setting the box, run

vagrant init

in the current working directory to generate the corresponding Vagrantfile. Open Vagrantfile through a text editor to perform some further common configurations:

Network configuration:

Vagrant’s network has three modes

1. The more commonly used one is port mapping, that is Map the port in the virtual machine to the port corresponding to the host for direct use. Configure in Vagrantfile:

config.vm.network :forwarded_port, guest: 80, host: 8080

guest: 80 means port 80 in the virtual machine, host: 8080 means mapped to port 8080 of the host. .

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

config.vm.network :private_network, ip: "192.168.1.104"

192.168. 1.104 represents the IP 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 LAN, the LAN will perform DHCP , then configure it in the Vagrantfile:

config.vm.network :public_network

Directory mapping:

Since it is a development environment, the development work must still be completed locally, rather than entering the virtual machine. The virtual machine can just run the service in the background, otherwise it will put the cart before the horse, so here you 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. Files in the current directory can be accessed directly under /vagrant. Of course, soft connections can also be created through ln. For example,

ln -fs /vagrant/wwwroot /var/www

is used to perform directory mapping. Of course, from the perspective of automated configuration, there is no need to enter the system without entering the system, so directory mapping operations can also be performed in Vagrant:

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

previous The parameter "wwwroot/" represents the local path. The relative path to the working directory is used here. The absolute path can also be used here, such as: "d:/www/"

The parameter after "/var" /www" represents the corresponding mapped directory in the virtual machine.

Run the script:

Although it is not necessary, if you need to run some scripts at startup (the installation of the environment or the startup of some services needs to be done after the directory mapping is completed), you can edit the script , similar to the following (extracted from Vagrant Document):

#!/usr/bin/env bash
apt-get update
apt-get install -y apache2
rm -rf /var/www
ln -fs /vagrant /var/www

Save it in the same directory as Vagrantfile, take the file name yourself (such as boot.sh), and then add it to Vagrantfile:

config.vm.provision :shell, :path => "boot.sh"

When used for the first time After the basic settings are completed, you can use vagrant up to start the virtual machine

Bringing machine 'default' up with 'virtualbox' provider...
[default] Setting the name of the VM...
[default] Clearing any previously set forwarded ports...
[default] Creating shared folders metadata...
[default] Clearing any previously set network interfaces...
[default] Preparing network interfaces based on configuration...
[default] You are trying to forward to privileged ports (ports < = 1024). Most operating systems restrict this to only privileged process (typicallyprocesses running as an administrative user). This is a warning in case
the port forwarding doesn&#39;t work. If any problems occur, please try a port higher than 1024.
[default] Forwarding ports...
[default] -- 22 => <strong>2222</strong> (adapter 1)
[default] -- 80 => 8080 (adapter 1)
[default] Booting VM...
[default] Waiting for VM to boot. This can take a few minutes.
[default] VM booted and ready for use!
[default] The guest additions on this VM do not match the installed version of VirtualBox! In most cases this is fine, but in rare cases it can cause things such as shared folders to not work properly. If you see shared folder errors, please update the guest additions within the virtual machine and reload your VM.
Guest Additions Version: 4.1.18
VirtualBox Version: 4.2
[default] Mounting shared folders...
[default] -- /var/www
[default] -- /vagrant
[default] Running provisioner: shell...

After the virtual machine is started, you can connect to the virtual machine through vagrant ssh for further environment configuration or software installation related work. Under Windows systems, you cannot directly connect to the virtual machine through vagrant ssh. You need to use third-party tools such as Putty and Xshell to connect.

The IP and port of the connection vary according to the network environment configuration. If port mapping is used, it is usually connected to the local port 2222.

登录的帐号密码均为 vagrant ,登录之后如果需要 su root ,密码也是 vagrant

注:使用 vagrant ssh 时,会提示可以使用密钥进行登录,如果需要使用putty进行密钥登录的话,需要下载 puttygen 将 ssh 的密钥转换为 ppk 文件才能使用。

登录ssh完成环境的配置,如果在开发环境中使用webserver(nginx/apache)为了避免一些静态文件处理的问题,可能还需要进行一些额外的配置:Vagrant下共享目录静态文件(js/jpg/png等)“缓存”问题

在不进入虚拟机的情况下,还可以使用下面的命令对 虚拟机进行管理:

vagrant up (启动虚拟机)
vagrant halt (关闭虚拟机——对应就是关机)
vagrant suspend (暂停虚拟机——只是暂停,虚拟机内存等信息将以状态文件的方式保存在本地,可以执行恢复操作后继续使用)
vagrant resume (恢复虚拟机 —— 与前面的暂停相对应)
vagrant destroy (删除虚拟机,删除后在当前虚拟机所做进行的除开Vagrantfile中的配置都不会保留)
当在启动Vagrant后,对于虚拟机有进行过安装环境相关的配置,如果并不希望写在Vagrant的启动shell里面每次都重新安装配置一遍,可以将当前配置好的虚拟机打包成box,
vagrant package --output NAME --vagrantfile FILE

可选参数:

--output NAME : (可选)设置通过NAME来指定输出的文件名

--vagrantfile FILE:(可选)可以将Vagrantfile直接封进box中

注:如果网络模式中使用 private_network 的话,在打包之前需要清除一下private_network的设置,避免不必要的错误:

sudo rm -f /etc/udev/rule.d/70-persistent-net.rules

制作完成之后直接将box文件拿到其他计算机上配置即可使用。

注意点: 1.如果在windows下无法启动,则可以开启box的gui端,这样就可以看到出现什么问题。

开启方法:

修改Vagrantfile文件,添加一下内容:

config.vm.provider :virtualbox do |vb|

  vb.gui = true

end

我就遇到了这个问题,虚拟机需要VT虚拟技术支持,所以要开启VT

The above is the detailed content of Methods and precautions for installing Vagrant under Windows. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn