search
HomeBackend DevelopmentPHP TutorialHow to install vagrant under windows?
How to install vagrant under windows?Jun 23, 2017 am 11:40 AM
vagrantwindowsuseInstall

vagrant is a software that facilitates virtual machine operation, and using a virtual machine has several benefits:

1. In order to keep the development environment consistent with the production environment (many development environments are windows and the production environment is linux), Various problems will not occur when the development environment is normal and moved to the formal production environment. Vagrant can write code in the IDE under the host (windows) by sharing files, and run the display directly in the virtual machine (linux). produce effect.

2. In vagrant, you only need to configure the development environment once, and then you can package and send the matched environment system image to other colleagues. Other colleagues only need to download vagrant and virtualBox, and then configure the sharing You can develop after adding the directory, and you no longer need to worry about configuring the environment.

3. Currently, many extensions such as swoole, redis, etc. have better support for Linux, and some even only support Linux. Therefore, using a virtual machine, you no longer need to make excuses for learning new technologies.

Related environment machine software versions: Host: win7; Virtual machine: CentOS 6.5 x86_64; vagrant: 1.9.5; virtualBox: 5.2

1. The installation steps are as follows:

1. Download virtualBox, click here, install

2. Download vagrant, click here, install

3. Download the image, there You can download it from several places: http://www.vagrantbox.es/ and http://www.vagrantbox.es/. Here I just found centos65-x86_64-20140116

4. Load the downloaded image. By the way, Chapter Step 3 is not necessary. Vagrant supports online image installation. However, due to the Great Wall, it is best to download the image through other methods and then load it locally. Open cmd and enter the following command:

# 命令是这样的,title为自己起的名字,url为box地址,可以在线也可以本地
# vagrant box add {title} {url}
# 实际安装命令,本地下载下来的
vagrant box add CentOs6.5_64 E:\boxes\centos65-x86_64-20140116.box

5. Find/create a directory, such as vagrant_project, and then initialize the environment

#新建目录
mkdir vagrant_project
#打开此目录
cd vagrant_project
#初始化,CentOs6.5_64为你上方设置的title
vagrant init CentOs6.5_64

After successful initialization, a Vagrantfile will appear in this directory file, this is the vagrant configuration file, the subsequent configuration can be modified here

6. First use the default configuration, and then start it:

vagrant up

will display a Series startup information:

==> default: Clearing any previously set forwarded ports...==> default: Clearing any previously set network interfaces...==> 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: 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
    .......

If no error is reported, the startup is successful. If an error occurs, please Google or Baidu. The main error I encountered here is infinite stuck default: SSH auth method: private keyThe most likely reason is that the virtualization technology is not enabled on this machine. Restart the computer, press F2 or other keys to enter the bios settings, and find the words related to virtual to enable it.

7. Remotely connect to Linux. At this time, you can use putty or xshell to connect under Windows. The host address is as marked above: 127.0.0.1:2222 , the username and password are: vagrant

At this time, the default shared folder relationship is under windows The vagrant_project folder corresponds to the /vagrant## folder under Linux, you can do whatever you want Create several files on both sides to test whether the two parties are synchronized

#8. Close and other commands. Since there are still many configurations that need to be configured, you can close them temporarily and wait until the configuration is completed. Restart

#关闭
vagrant halt
#重启
vagrant reload

2. Network configuration, shared directory and other related configuration

The above is just for display It has a simple usage of installation and opening, but it requires additional configuration to be truly used for development. Open the Vagrantfile configuration file mentioned above to configure it. Many of them have been commented out, just select to remove them

1. Network configuration, Vagrant has three configuration methods: Click here to view the official website documentation

a. Port mapping, which means mapping the port of the virtual machine to the port of the host. You can access this port in the host LAN. Accessing things on your virtual machine

config.vm.forwarded_port 80, 8080
The above function is to map the 80 port of the virtual machine to the 8080 port of the host. Then my direct access to the 8080 port of the host is equivalent to accessing Port 80 of the virtual machine

b. Private network, only the host can access the virtual machine, the contents of the virtual machine that members within the main LAN cannot access

config.vm.network "private_network", ip: "192.168.33.10"
The above settings After completion, the virtual machine can be accessed through 192.168.33.10 on this machine. It should be noted that although 192.168.33.10 can be set casually, if the IP of the host is in the 192.168.1.xxx field, then do not use this section again. It can be 2.xxx or 3.xxx or others to avoid conflicts.

c. Shared network. Members within the host LAN can access the content in the virtual machine. The virtual machine is equivalent to a member of the LAN

config.vm.network "public_network", ip: "192.168.1.120"

  以上配置一般来说开发环境也不需要局域网内成员访问,所以大部分情况下是选择用b、私有网络来开发,这个可根据实际情况来选择。

2、共享目录,用户可以自定义共享目录,在Vagrantfile配置文件中配置:

#禁用原有的默认的共享目录
config.vm.synced_folder '.', '/vagrant', disabled: true#增加新的共享目录,第二个参数以当前文件夹为基准
config.vm.synced_folder "abc", "/www/web/abc",

上方相当与windows下的vagrant_project/abc文件夹对应linux下的/www/web/abc文件夹

除了可以自定义共享文件外,还可以选择共享的方式,vagrant有四种共享的方式,官网文档请点这里查看

  a、NFS共享方式,windows平台无法使用此配置,且用此方式时不能修改文件的所有者和所有组

  b、RSync共享方式,所有平台都可以使用,但这种方式貌似有个缺点就是主机的改动可以同步到虚拟机中,但虚拟机中的改动无法同步到主机中,如果要实现双向同步可能还需做其它设置,由于没有用过,所以不太清楚。

  c、SMB共享方式,只有windows平台可以使用,曾有找一些资料来配置,但一直没有成功,所以具体的并不太清楚,但貌似效率是挺不错的。

  d、VirtualBox共享方式,这是VirtualBox提供的也是vagrant默认的共享方式。当文件数量小时用着不错,当文件数量大的时候打开网页会卡到超时。

总之以上,如果主机是mac,则推荐用NFS共享方式,如果是windows则推荐用smb或者rsync方式,如果文件很少,那用默认的就可以了。

但除了上方说的,windows下其实还有方式可以加快网站访问速度,那就是用Vagrant WinNFSd这款插件,插件地址点这里,这种方式是windows下提高速度最简单的方式了:

vagrant plugin install vagrant-winnfsd

安装完成后配置type为nfs,虽然官网上说nfs不能应用于windows,但用这个插件后就可以了。

config.vm.synced_folder "abc", "/www/web/abc",type:"nfs"

测试了下,此插件还是非常有效的,原先打开yii2框架的文件会超时,或10、20多秒,但用了这个后响应在2s以内,还是可以接受的。另提供一个参考资料:让Vagrant在Windwos下支持使用NFS/SMB共享文件夹从而解决目录共享IO缓慢的问题

 三、域名访问及打包分发

1、域名访问,主要是虚拟的本地域名配置问题

这里还是安装了lanmp_v3.1,具体的安装方法可以看以前的文章:Linux下安装LANMP环境或者去下载官网上去找安装说明

新建一个站点,指定目录到共享的目录,随便取个虚拟的域名:www.abc.com

测试需要在主机中修改host,如下:(至于虚拟机中/etc/hosts文件中,测试修改与不修改都不影响访问)

192.168.33.10    www.abc.com192.168.33.10    abc.com

然后直接在浏览器中用既可显示虚拟机中的内容了。而直接在windows中的的IDE修改文件,会实时通过这个网址展示出来。

2、打包分发

vagrant package

等待一段时间,会发现在目录生成一个package.box的文件,这个就是已经包含安装环境的镜像了,将此镜像发给同事,让其安装上方所属的步骤加载镜像vagrant box add {title} {url}命令,然后配置自己的网络及共享文件夹就可以直接开发了,远离重新配置环境的烦恼。

以上就是vagrant常用的一些操作,挺不错的,试试吧!

The above is the detailed content of How to install 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
c盘的users是什么文件夹?可以删除吗?c盘的users是什么文件夹?可以删除吗?Nov 10, 2022 pm 06:20 PM

c盘的users是用户文件夹,主要存放用户的各项配置文件。users文件夹是windows系统的重要文件夹,不能随意删除;它保存了很多用户信息,一旦删除会造成数据丢失,严重的话会导致系统无法启动。

启动任务管理器的三个快捷键是什么启动任务管理器的三个快捷键是什么Sep 21, 2022 pm 02:47 PM

启动任务管理器的三个快捷键是:1、“Ctrl+Shift+Esc”,可直接打开任务管理器;2、“Ctrl+Alt+Delete”,会进入“安全选项”的锁定界面,选择“任务管理器”,即可以打开任务管理器;3、“Win+R”,会打开“运行”窗口,输入“taskmgr”命令,点击“确定”即可调出任务管理器。

微软的pin码是什么微软的pin码是什么Oct 14, 2022 pm 03:16 PM

PIN码是Windows系统为了方便用户本地登录而独立于window账户密码的快捷登录密码,是Windows系统新添加的一套本地密码策略;在用户登陆了Microsoft账户后就可以设置PIN来代替账户密码,不仅提高安全性,而且也可以让很多和账户相关的操作变得更加方便。PIN码只能通过本机登录,无法远程使用,所以不用担心PIN码被盗。

window下报错“php不是内部或外部命令”怎么解决window下报错“php不是内部或外部命令”怎么解决Mar 23, 2023 pm 02:11 PM

对于刚刚开始使用PHP的用户来说,如果在Windows操作系统中遇到了“php不是内部或外部命令”的问题,可能会感到困惑。这个错误通常是由于系统无法识别PHP的路径导致的。在本文中,我将为您提供一些可能会导致这个问题的原因和解决方法,以帮助您快速解决这个问题。

win10自带的onenote是啥版本win10自带的onenote是啥版本Sep 09, 2022 am 10:56 AM

win10自带的onenote是UWP版本;onenote是一套用于自由形式的信息获取以及多用户协作工具,而UWP版本是“Universal Windows Platform”的简称,表示windows通用应用平台,不是为特定的终端设计的,而是针对使用windows系统的各种平台。

win10为什么没有“扫雷”游戏了win10为什么没有“扫雷”游戏了Aug 17, 2022 pm 03:37 PM

因为win10系统是不自带扫雷游戏的,需要用户自行手动安装。安装步骤:1、点击打开“开始菜单”;2、在打开的菜单中,找到“Microsoft Store”应用商店,并点击进入;3、在应用商店主页的搜索框中,搜索“minesweeper”;4、在搜索结果中,点击选择需要下载的“扫雷”游戏;5、点击“获取”按钮,等待获取完毕后自动完成安装游戏即可。

windows操作系统的特点包括什么windows操作系统的特点包括什么Sep 28, 2020 pm 12:02 PM

windows操作系统的特点包括:1、图形界面;直观高效的面向对象的图形用户界面,易学易用。2、多任务;允许用户同时运行多个应用程序,或在一个程序中同时做几件事情。3、即插即用。4、出色的多媒体功能。5、对内存的自动化管理。

在windows中鼠标指针呈四箭头时一般表示什么在windows中鼠标指针呈四箭头时一般表示什么Dec 17, 2020 am 11:39 AM

在windows中鼠标指针呈四箭头时一般表示选中对象可以上、下、左、右移动。在Windows中鼠标指针首次用不同的指针来表示不同的状态,如系统忙、移动中、拖放中;在Windows中使用的鼠标指针文件还被称为“光标文件”或“动态光标文件”。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools