PHP Laravel 开发的环境搭建 for Windows

PHP Laravel 开发的环境搭建 for Windows

搭建这个开发环境,不是必须的,倒是最好的。虽然麻烦点,倒是一劳永逸的。
开发环境用到三个软件是VirtualBox、Vagrant、Homestead,如今先科普概念。php

VirtualBox

VitrualBox 是一款很是强大的免费虚拟机软件,使用者能够在 VitrualBox 上安装并运行 Linux、Windows、Mac OS X 等操做系统,相似的软件还有 VMware 等linux

Homestead

Homestead 是 Laravel 官方预封装的一套开发环境。在Laravel 的开发中,强烈建议使用 Homestead,不管是一我的开发项目,仍是团队开发。laravel

附注: 若是你是 Windows 用户,你可能须要启用硬件虚拟化(VT-x)。这一般须要经过 BIOS 来启用它。git

Vagrant

每次开始一个新的项目,必然要先搭建开发环境,不一样的开发者可能习惯使用不一样的系统,有人用 windows,有人用 mac,有人用 linux,在搭建环境的过程当中又有可能会遇到各类 BUG 各类坑,代码还没开始写,搭环境就先用掉几天时间..非常头疼,因而,为了解决这个问题,Vagrant 应运而生...github

第一步 安装必须的软件

  • 安装vagrant
  • 安装Virtualbox
  • 安装Homestead :包括盒子安装和工具安装。

第二步 安装 Homestead 盒子(vagrant box add)

vagrant box add laravel/homestead

使用上述命令,会自动从国外网上下载box, 目前最新的box是5.0.1, 国外的比较慢。shell

建议下载过来,经过本地方式添加box:json

vagrant box add laravel/homestead file:///F:/BaiduNetdiskDownload/homestead-5.0.1.box

我作了个百度网盘连接 :ubuntu

homestead-5.0.1.box:https://pan.baidu.com/s/1eTHY88q 密码:qn3i

添加后,Vagrant会将所下载的box保存到 ~/.vagrant.d/boxes 目录下。
对windows系统,大体目录是:C:\Users\Administrator.vagrant.d\boxes。windows

安装好后,最好用list命令验证下。centos

  • 列出box: vagrant box list
E:\Homestead>vagrant box list
centos/7          (virtualbox, 1710.01)
laravel/homestead (virtualbox, 4.0.0)
  • 若是你不爽,或疯了傻了,能够删除用不到的box:
E:\Homestead>vagrant box remove laravel/homestead
Removing box 'laravel/homestead' (v4.0.0) with provider 'virtualbox'...

安装时可能出现版本号是0的坑:

E:\Homestead>vagrant box list
centos/7          (virtualbox, 1710.01)
laravel/homestead (virtualbox, 0)

从本地添加的box文件,估计是信息不全,须要经过配置文件来添加。在Homestead文件夹是新建一个json文件,如 homestead.json:

{
    "name": "laravel/homestead",
    "versions": [{
        "version": "5.0.1",
        "providers": [{
            "name": "virtualbox",
            "url": "file:///d:/php/homestead-5.0.1.box"
        }]
    }]
}

注意填入正确的版本号和路径名,而后从新添加:

vagrant box add homestead.json

再列出,发现原来的没有删除:

E:\Homestead>vagrant box list
centos/7          (virtualbox, 1710.01)
laravel/homestead (virtualbox, 0)
laravel/homestead (virtualbox, 5.0.1)

重复名字删除时,要加上版本号:

E:\Homestead>vagrant box remove laravel/homestead --box-version 0

再列出,已完美:

E:\Homestead>vagrant box list
centos/7          (virtualbox, 1710.01)
laravel/homestead (virtualbox, 5.0.1)

到目前为止。咱们作的工做,至关于建立了一个虚拟硬盘,虽然没有直接操做VirtualBox, 但Vagrant 经过 VirtualBox提供的接口,替咱们作了该作的一切。在这个虚拟硬盘中,已经装了PHP开发所须要的一切,列出(copy from laravel.com)以下:

Included Software

  • Ubuntu 16.04
  • Git
  • PHP 7.1
  • Nginx
  • MySQL
  • MariaDB
  • Sqlite3
  • Postgres
  • Composer
  • Node (With Yarn, PM2, Bower, Grunt, and Gulp)
  • Redis
  • Memcached
  • Beanstalkd

Homestead翻译成中文,至关于家园,宅地之类,在这块咱们的地上,装满了开发可用的东西,省了心,没必要让每位开发者 劳神安装配置,并且保证每一个企业开发者的开发环境高度统一。
下一步要作的事,就是配置和启动。

第三步 安装 Homestead工具(经过git)

git clone https://github.com/laravel/homestead.git Homestead

上述安装,在你选择的工做目录进行,我选择是e: 若是你的机器上没装 git,自已安装。这里略去。

第四步 Homestead盒子的初始化(vagrant init)

vagrant init 运行以前 ,先运行 init.bat

E:\>cd homestead

E:\Homestead>vagrant box list
centos/7          (virtualbox, 1710.01)
laravel/homestead (virtualbox, 5.0.1)

E:\Homestead>vagrant init laravel/homestead
Homestead settings file not found in E:/Homestead

E:\Homestead>init
已复制         1 个文件。
已复制         1 个文件。
已复制         1 个文件。
Homestead initialized!

E:\Homestead>vagrant init laravel/homestead
`Vagrantfile` already exists in this directory. Remove it before
running `vagrant init`.

Vagrantfile 文件已经存在,不能初始化,是由于安装Homestead时已经有一个Vagrantfile 文件了,这个原来的文件,是正确的,先备份一下,等vagrant init laravel/homestead 运行后,再还回去:

E:\Homestead>copy Vagrantfile Vagrantfile_bak
已复制         1 个文件。

E:\Homestead>del vagrantfile

E:\Homestead>vagrant init laravel/homestead
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.

E:\Homestead>copy Vagrantfile_bak Vagrantfile
覆盖 Vagrantfile 吗? (Yes/No/All): y
已复制         1 个文件。

走到这一步,Homestead虚拟机已建立成功,下一步是启动它:

第五步 Homestead盒子的启动(vagrant up)

E:\Homestead>vagrant up
Bringing machine 'homestead-7' up with 'virtualbox' provider...
==> homestead-7: Importing base box 'laravel/homestead'...
==> homestead-7: Matching MAC address for NAT networking...
.....(省略显示)
 homestead-7: ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDddrcmIDNVhDJTNir5xNowJLvuTjboUjyxax2fjd/QD/ac/QwiunArtqAE134JWQxGSljank8XDfCJOTpyWXFQlqpi/MAzXBcT7XVVMZB2HUjubcVvIPRhv2Ryz/xUhueR18K55+RgoYi2Qhg+0j/jxMK9qmlLmXEIhdyYBXORCGbVmn8EeorZmExBLbyT2oEurizQiRGUl0jbRiQ4+g+v/BJYvn45GAlcXIWQ5Dv7c9jx0aScYoC/PXLt+cdfvmYStKZVXimU41j5jfeFnpAY8v2GK4dBSvvMKci21q0xkSr8WiY4EUOg+h+x29od9HTYk7e5s8NKpINQr8Hm2I+X ntcat888@qq.com
==> homestead-7: Running provisioner: shell...
    homestead-7: Running: inline script
.....(省略显示)

注意到在up时,系统自动产生了一个私钥,因此看上去不用运行 ssh-keygen -t rsa -C "ntcat888@qq.com"
,直接运行 ssh协议登陆虚拟机:

E:\Homestead>vagrant ssh
Welcome to Ubuntu 16.04.3 LTS (GNU/Linux 4.4.0-101-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

0 packages can be updated.
0 updates are security updates.


Last login: Sun Jan 21 07:35:42 2018 from 10.0.2.2
vagrant@homestead:~$

退出虚拟机:

vagrant@homestead:~$ exit
logout
Connection to 127.0.0.1 closed.

目前为止,咱们只是尝试了盒子的启动和退出 ,在生产环境下,是须要配置虚拟机的,在配置前,首先关闭虚拟机:

E:\Homestead>vagrant halt
==> homestead-7: Attempting graceful shutdown of VM...

E:\Homestead>

查看系统状态:

E:\Homestead>vagrant global-status
id       name        provider   state    directory
-----------------------------------------------------------------------------
fb8b191  homestead-7 virtualbox poweroff C:/Users/Administrator/Homestead
7d8aa88  default     virtualbox running  E:/workspace/centos7
27fe266  default     virtualbox running  E:/Homestead
bd04938  homestead-7 virtualbox poweroff E:/Homestead

The above shows information about all known Vagrant environments
on this machine. This data is cached and may not be completely
up-to-date. To interact with any of the machines, you can go to
that directory and run Vagrant, or you can use the ID directly
with Vagrant commands from any directory. For example:
"vagrant destroy 1a2b3c4d"

第六步 Homestead盒子的配置(Homestead.yaml)

---
ip: "192.168.10.10"      <-----启动出现网络错误,随便改改这个IP
memory: 2048
cpus: 1
provider: virtualbox

authorize: ~/.ssh/id_rsa.pub

keys:
    - ~/.ssh/id_rsa

folders:                
    - map: ~/code        <------ 你的项目代码所在的共享文件夹,我填入的是e:/code
      to: /home/vagrant/code

sites:
    - map: homestead.test <------ 能够随便换你想要的名字
      to: /home/vagrant/code/public

databases:
    - homestead

配置后从新启动:

E:\Homestead>vagrant up
Bringing machine 'homestead-7' up with 'virtualbox' provider...
==> homestead-7: Checking if box 'laravel/homestead' is up to date...
==> homestead-7: Clearing any previously set forwarded ports...
==> homestead-7: Clearing any previously set network interfaces...
==> homestead-7: Preparing network interfaces based on configuration...
    homestead-7: Adapter 1: nat
    homestead-7: Adapter 2: hostonly
==> homestead-7: Forwarding ports...
    homestead-7: 80 (guest) => 8000 (host) (adapter 1)
    homestead-7: 443 (guest) => 44300 (host) (adapter 1)
    homestead-7: 3306 (guest) => 33060 (host) (adapter 1)
    homestead-7: 4040 (guest) => 4040 (host) (adapter 1)
    homestead-7: 5432 (guest) => 54320 (host) (adapter 1)
    homestead-7: 8025 (guest) => 8025 (host) (adapter 1)
    homestead-7: 27017 (guest) => 27017 (host) (adapter 1)
    homestead-7: 22 (guest) => 2222 (host) (adapter 1)
==> homestead-7: Running 'pre-boot' VM customizations...
==> homestead-7: Booting VM...
==> homestead-7: Waiting for machine to boot. This may take a few minutes...
    homestead-7: SSH address: 127.0.0.1:2222
    homestead-7: SSH username: vagrant  <---第一次运行没有出现这个用户登陆,运行putty,输入192.168.10.10
    homestead-7: SSH auth method: private key  |-----端口22,登陆时,它产生了个私钥,再次运行就这样正常了
    homestead-7: Warning: Connection reset. Retrying...
    homestead-7: Warning: Connection aborted. Retrying...
    homestead-7: Warning: Remote connection disconnect. Retrying...
==> homestead-7: Machine booted and ready!
==> homestead-7: Checking for guest additions in VM...
    homestead-7: The guest additions on this VM do not match the installed version of
    homestead-7: VirtualBox! In most cases this is fine, but in rare cases it can
    homestead-7: prevent things such as shared folders from working properly. If you see
    homestead-7: shared folder errors, please make sure the guest additions within the
    homestead-7: virtual machine match the version of VirtualBox you have installed on
    homestead-7: your host and reload your VM.
    homestead-7:
    homestead-7: Guest Additions Version: 5.0.18_Ubuntu r106667
    homestead-7: VirtualBox Version: 5.2
==> homestead-7: Setting hostname...
==> homestead-7: Configuring and enabling network interfaces...
==> homestead-7: Mounting shared folders...
    homestead-7: /vagrant => E:/Homestead         <---内置的共享
    homestead-7: /home/vagrant/code => E:/code    <---用户设置的共享
==> homestead-7: Machine already provisioned. Run `vagrant provision` or use the `--provision`
==> homestead-7: flag to force provisioning. Provisioners marked to run always will still run.

再次登陆BOX:

E:\Homestead>vagrant ssh
vagrant@homestead:~$ cd /home/vagrant/code
vagrant@homestead:~/code$ ls      <---看上去 直接打cd code也能进入,映射成家目录了。

若是这里的文件和e:/code能随时同步,就说明OK了。

第七步 安装 PHP 7及Composer

参考其它安装,安装后用Composer 生成laravel 项目模板。随后其它文章中细说。

相关文章
相关标签/搜索