PHP Laravel 开发的环境搭建 for Windows
PHP Laravel 开发的环境搭建 for Windows
搭建这个开发环境,不是必须的,却是最好的。虽然麻烦点,却是一劳永逸的。
开发环境用到三个软件是VirtualBox、Vagrant、Homestead,现在先科普概念。
VirtualBox
VitrualBox 是一款非常强大的免费虚拟机软件,使用者可以在 VitrualBox 上安装并运行 Linux、Windows、Mac OS X 等操作系统,类似的软件还有 VMware 等
Homestead
Homestead 是 Laravel 官方预封装的一套开发环境。在Laravel 的开发中,强烈建议使用 Homestead,不论是一个人开发项目,还是团队开发。
附注: 如果你是 Windows 用户,你可能需要启用硬件虚拟化(VT-x)。这通常需要通过 BIOS 来启用它。
Vagrant
每次开始一个新的项目,必然要先搭建开发环境,不同的开发者可能习惯使用不同的系统,有人用 windows,有人用 mac,有人用 linux,在搭建环境的过程中又有可能会遇到各种 BUG 各种坑,代码还没开始写,搭环境就先用掉几天时间..很是头疼,于是,为了解决这个问题,Vagrant 应运而生...
第一步 安装必须的软件
- 安装vagrant
- 安装Virtualbox
- 安装Homestead :包括盒子安装和工具安装。
第二步 安装 Homestead 盒子(vagrant box add)
vagrant box add laravel/homestead
使用上述命令,会自动从国外网上下载box, 目前最新的box是5.0.1, 国外的比较慢。
建议下载过来,通过本地方式添加box:
vagrant box add laravel/homestead file:///F:/BaiduNetdiskDownload/homestead-5.0.1.box
我做了个百度网盘链接 :
homestead-5.0.1.box:https://pan.baidu.com/s/1eTHY88q 密码:qn3i
添加后,Vagrant会将所下载的box保存到 ~/.vagrant.d/boxes 目录下。
对windows系统,大致目录是:C:\Users\Administrator.vagrant.d\boxes。
安装好后,最好用list命令验证下。
- 列出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 项目模板。随后其它文章中细说。