Install vagrant&virtualBox php environment under windows

VirtualBox  is an open source virtual machine software, the most important one is free, although there are others, but enough is enough

Introduction to Vagrant: 

1. Used to create and deploy a virtualized development environment

2. By managing the virtual machine through commands and configuration files, the deployment of a development environment can be completed quickly, and it can be packaged and disseminated quickly, unifying the development environment and solving the trouble of repeatedly configuring the environment.

(one)

Download virtualbox first, and install it step by step after downloading

Address: https://www.virtualbox.org/wiki/Downloads (official website download)

Then download vagrang, click Next until the installation is complete

Address: Downloads | Vagrant by HashiCorp

The order can not be wrong, otherwise the installation of vagrang will report an error

 (two)

Configure the Vagrant environment variable
    wins system:
        add after the system variable PATH
        ;E:\Program Files\Oracle\VirtualBox;E:\HashiCorp\Vagrant\bin
        (the above PATH is filled in according to the path of your own installation software, do not copy and use directly)

(3) Download the box system template 

 Box resources:  A list of base boxes for Vagrant - Vagrantbox.es

(4) Add a virtual machine

1. Add box

#title是自己起的名字
#url是自己在第三步下载的box文件路径
vagrant box add {title} {url}

2. Initialize Vagrant

#新建目录(作用:用来放虚拟机文件,避免与项目文件搞混)
mkdir vagrant_project
#打开此目录
cd vagrant_project
#初始化,title为你上方设置的title
vagrant init {title}

3. Start the VirtualBox virtual machine from Vagrant

vagrant up

Successfully displayed styles

==> default: Forwarding ports...
    default: 22 (guest) => 2222 (host) (adapter 1)
==> 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
    default: Warning: Connection reset. Retrying...
==> default: Machine booted and ready!
[default] GuestAdditions 6.1.4 running --- OK.

4. Enter the configuration step ssh login problem

The virtual machine has just been started. At this time, the above ip and port number cannot be used to log in to the host with the ssh account and password.
You need to change the ssh login permission (you need to switch to the root user here)

#使用Vagrant的ssh命令登陆
vagrant ssh
#先更改root账户密码,根据提示需要输入两次密码
sudo passwd root
#切换为root账户(需要输入上一步中修改的root密码)
sudo -i 
#使用vi编辑器打开 /etc/ssh/sshd_config 配置文件进行修改。
vi /etc/ssh/sshd_config

change configuration

PasswordAuthentication yes
PermitRootLogin yes(这个不需要修改,备注:开启关注root直接登录的功能)
#这里简单讲解vi编辑器的一些操作
#先按键盘的 i 键进入编辑模式,修改完成以后需要按 esc 按键退出编辑模式
#然后输入 :wq   就可以退出并保存了
# 需要重新载入配置
service sshd restart
提示:Redirecting to /bin/systemctl restart sshd.service 表示载入失败
centos7以上换成
1. 查看 sshd 服务是否启动:

看到上述信息就可以确定是启动了。

2. 如果没有启动,则需要启动该服务:

systemctl start sshd.service
3. 重启 sshd 服务:

systemctl restart sshd.service
4. 设置服务开启自启:

systemctl enable sshd.service
启动成功后,如果ssh工具连接不上,换一个ip重启后重试

Then you need to use the exit command to exit the root account, and then exit (logout command) ssh mode

You can use the account set above: root password: (the root password set above) use the ssh tool to log in;

2. Network problem (need to modify the Vagrantfile configuration file in the folder selected in the fifth step)

Introduction to several modes

Forwarded port (port mapping)
refers to mapping the port of the host computer to a port on the virtual machine. When accessing the port of the host computer, the request will actually be forwarded to the specified port on the virtual machine. The configuration file setting syntax is

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

Advantages: Simple, easy to understand, and easy to access virtual machines from external networks.
Disadvantages: It is cumbersome to map many ports, and does not support port forwarding on the host machine with a port smaller than 1024 (for example, SSL port 443 cannot be used for https connections).

  • Under the public network (public network)
    configuration, the virtual machine enjoys the same treatment as the physical machine and the same network configuration. After vagrant version 1.3, this configuration also supports setting a fixed IP.
config.vm.network "private_network", ip: "192.168.50.4" # 固定IP

You can also set a dynamic IP, the configuration syntax is as follows:

config.vm.network "private_network", type: "dhcp"

Pros: Safe, only accessible by yourself.
Disadvantages: Due to private ownership, other team members cannot collaborate with you.


  • Under the public network (public network) configuration, the virtual machine enjoys the same treatment as the physical machine and the same network configuration. After version 1.3 of vagrant, this configuration also supports setting a fixed IP. The configuration syntax is as follows:
config.vm.network "public_network", ip: "192.168.50.4"

You can also set a bridge network card, the configuration syntax is as follows:

config.vm.network "public_network", bridge: "en1: Wi-Fi (AirPort)"

Advantages: It is convenient for team collaboration, and others can access your virtual machine.
Disadvantages: need to have a network, a router to assign IP

  • 3. Shared directory
    Sometimes, we hope that the virtual machine can share some folders with our host. At this time, configure it in the vagrant configuration file to achieve the purpose of sharing the directory.
    The shared directory types of vagrant are:

  • Configuration syntax: (specifically here you can Baidu. There is a more detailed introduction)
    config.vm.synced_folder "/hostPath", "/www/wwwroot", owner:"www", group:"www", create:true  

Vagrant official document address: https://www.vagrantup.com/docs/

Reference address: Use Vagrant+VirtualBox to build a cross-platform development environment under Mac - short book

Attach the vagrant command

#--列出本地环境中所有的box
vagrant box list
#--添加box到本地vagrant环境
vagrant box add box-name(box-url)
#--更新本地环境中指定的box
vagrant box update box-name
#--删除本地环境中指定的box
vagrant box remove box-name
#--重新打包本地环境中指定的box
vagrant box repackage box-name
#--启动虚拟机
vagrant up
#--关机
vagrant halt
#--销毁虚拟机
vagrant destroy
#--虚拟机重启
vagrant reload

Guess you like

Origin blog.csdn.net/qq_39436397/article/details/83859212