Laravel之homestead安装法
Laravel据说是目前最优雅的php web框架,虽然它没有durpal、wordpress那么丰富,但正因为它简洁、快速的开发方式,使得大家都非常容易入门,并且作为未来的吃饭工具,也是可以胜任的。
(注: 这是关于laravel最基础的部分,用它来自学或 者教学都非常棒。)
开始--安装
服务器要求
Laravel对服务器的要求不高,一般情况下用一些php平台,如phpstudy等,但是它有一个专门针对的本地开发环境--Larval homestead,可以满足所有的要求。
1、准备
安装virtualbox和vagrant
我的系统在搭建linux和drupal时已经安装过了(建议下载最新的版本,以保证适配),通过powershell可以用命令:
Get WmiObject Class Win32_Product | Select Object Property Name
查看,我的版本是Oracle VM VirtualBox 4.1.44、 Vagrant 1.7.2。
2、安装和配置
新建项目所在路径,我的d:\data\vargant_box
用poweshell进入项目路径,敲入命令下载box,
PS D:\data\vagrant_box> vagrant box add laravel/homestead
==> box: Loading metadata for box 'laravel/homestead'
box: URL: https://atlas.hashicorp.com/laravel/homestead
This box can work with multiple providers! The providers that it
can work with are listed below. Please review the list and choose
the provider you will be working with.
1) virtualbox
2) vmware_desktop
Enter your choice: 1
==> box: Adding box 'laravel/homestead' (v0.4.4) for provider: virtualbox
box: Downloading: https://atlas.hashicorp.com/laravel/boxes/homestead/versions/0.4.4/providers/virtualbox.box
box: Progress: 0% (Rate: 0curl:/s, Estimated time remaining: --:--:--)
上面的下载会很慢,也可以使用工具从上面提供的下载的地址中直接下载。
可以用迅雷下载后,确定好box的后缀,
PS D:\data\vagrant_box> vagrant box add -n laravel d:/data/vagrant_box/hc-download.box
==> box: Adding box 'laravel' (v0) for provider:
box: Downloading: file://d:/data/vagrant_box/hc-download.box
box: Progress: 100% (Rate: 213M/s, Estimated time remaining: --:--:--)
==> box: Successfully added box 'laravel' (v0) for 'virtualbox'!
接下来,大家就可以在虚拟机环境中运行laravel了!
2.1 把box添加到vagrant box列表中
1、打开命令行,进入box下载的路径(我是通过迅雷下载的)
box存放的路径2、 vagrant box add laravel/homestead hc-download.box
添加box
3、查看一下是否添加成功
PS D:\data\vagrant_box> vagrant box add laravel/homestead hc-download.box
==> box: Adding box 'laravel/homestead' (v0) for provider:
box: Downloading: file://D:/data/vagrant_box/hc-download.box
box: Progress: 100% (Rate: 14.7M/s, Estimated time remaining: --:--:--)
==> box: Successfully added box 'laravel/homestead' (v0) for 'virtualbox'!
PS D:\data\vagrant_box> vagrant box list
drupalbox (virtualbox, 0)
laravel/homestead (virtualbox, 0)
4、克隆laravel在github上的homestead项目
新建laravel目录,从github把homestead项目克隆下来,并命令为Homestead
PS D:\data\laravel> git clone https://github.com/laravel/homestead.git Homestead
克隆完毕,我们可以在laravel文件夹下看到克隆的目录:
运行init.bat(windows),linux运行init.sh
成功后,我们会在c盘的如下目录下看到:
5、打开Homestead.yaml文件,配置homestead
---
ip: "192.168.10.10"
memory: 2048
cpus: 1
provider: virtualbox
authorize: ~/.ssh/id_rsa.pub
keys:
- ~/.ssh/id_rsa
folders:
# 修改为本地主机laravel的源码位置
- map: D:\data\laravel\code
to: /home/vagrant/Code
sites:
- map: homestead.app
#注意本地主机的public的路径,它并没有laravel这个根。
#to: /home/vagrant/Code/Laravel/public
#修改为:
to: /home/vagrant/Code/public
databases:
- homestead
# blackfire:
# - id: foo
# token: bar
# client-id: foo
# client-token: bar
# ports:
# - send: 50000
# to: 5000
# - send: 7777
# to: 777
# protocol: udp
6、修改box版本,避免无法运行headstead的box
修改box版本号如果提示:
PS D:\data\laravel\homestead> vagrant up
This Vagrant environment has specified that it requires the Vagrant
version to satisfy the following version requirements:
>= 1.8.4
You are running Vagrant 1.7.2, which does not satisfy
these requirements. Please change your Vagrant version or update
the Vagrantfile to allow this Vagrant version. However, be warned
that if the Vagrantfile has specified another version, it probably has
good reason to do so, and changing that may cause the environment to
not function properly.
则是vagrant版本旧了,直接从官网下载最新的vagrant安装覆盖即可。
如果出现
D:/HashiCorp/Vagrant/embedded/lib/ruby/2.2.0/psych.rb:370:in `parse': (<unknown>): found a tab character that violate intendation while scanning a plain scalar at line 17 column 12 (Psych::SyntaxError)
from D:/HashiCorp/Vagrant/embedded/lib/ruby/2.2.0/psych.rb:370:in `parse_stream'
from D:/HashiCorp/Vagrant/embedded/lib/ruby/2.2.0/psych.rb:318:in `parse'
from D:/HashiCorp/Vagrant/embedded/lib/ruby/2.2.0/psych.rb:245:in `load'
from D:/data/laravel/Homestead/Vagrantfile:25:in `block in <top (required)>'
from D:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.5/lib/vagrant/config/v2/loader.rb:37:in `call'
from D:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.5/lib/vagrant/config/v2/loader.rb:37:in `load'
from D:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.5/lib/vagrant/config/loader.rb:113:in `block (2 levels) in load'
from D:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.5/lib/vagrant/config/loader.rb:107:in `each'
from D:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.5/lib/vagrant/config/loader.rb:107:in `block in load'
from D:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.5/lib/vagrant/config/loader.rb:104:in `each'
from D:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.5/lib/vagrant/config/loader.rb:104:in `load'
from D:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.5/lib/vagrant/vagrantfile.rb:28:in `initialize'
from D:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.5/lib/vagrant/environment.rb:740:in `new'
from D:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.5/lib/vagrant/environment.rb:740:in `vagrantfile'
from D:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.5/lib/vagrant/environment.rb:486:in `host'
from D:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.5/lib/vagrant/environment.rb:208:in `block in action_runner'
from D:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.5/lib/vagrant/action/runner.rb:33:in `call'
from D:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.5/lib/vagrant/action/runner.rb:33:in `run'
from D:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.5/lib/vagrant/environment.rb:473:in `hook'
from D:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.5/lib/vagrant/environment.rb:722:in `unload'
from D:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.5/bin/vagrant:177:in `ensure in <main>'
from D:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.8.5/bin/vagrant:177:in `<main>'
```
则考虑yaml文件出错。这里指第17行有语法错误。
###### 最后终于成功运行,
运行成功提示:
PS D:\data\laravel\homestead> vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Checking if box 'laravel/homestead' is up to date...
==> default: A newer version of the box 'laravel/homestead' is available! You currently
==> default: have version '0'. The latest is version '0.4.0'. Run
==> default: vagrant box update
to update.
==> 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: 80 (guest) => 8000 (host) (adapter 1)
default: 443 (guest) => 44300 (host) (adapter 1)
default: 3306 (guest) => 33060 (host) (adapter 1)
default: 5432 (guest) => 54320 (host) (adapter 1)
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
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
default: The guest additions on this VM do not match the installed version of
default: VirtualBox! In most cases this is fine, but in rare cases it can
default: prevent things such as shared folders from working properly. If you see
default: shared folder errors, please make sure the guest additions within the
default: virtual machine match the version of VirtualBox you have installed on
default: your host and reload your VM.
default:
default: Guest Additions Version: 5.0.12
default: VirtualBox Version: 5.1
==> default: Setting hostname...
==> default: Configuring and enabling network interfaces...
==> default: Mounting shared folders...
default: /vagrant => D:/data/laravel/Homestead
default: /home/vagrant/Code => D:/data/laravel/code
==> default: Machine already provisioned. Run vagrant provision
or use the --provision
==> default: flag to force provisioning. Provisioners marked to run always will still run.
###### 进一步测试:
PS D:\data\laravel\homestead> vagrant ssh
Welcome to Ubuntu 14.04.4 LTS (GNU/Linux 3.19.0-25-generic x86_64)
- Documentation: https://help.ubuntu.com/
vagrant@homestead:~$
在本地浏览器输入192.168.10.10,则可访问到laravel站点。
![预览laravel](http:https://img.haomeiwen.com/i2675266/e729171a7c19e7d0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
###### 总结经验:
* 64位系统中,运行64位的虚拟机要确保配置正确
![配置virtualbox](http:https://img.haomeiwen.com/i2675266/3f3f14614cf22d3b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
* windows的Hyper V要关闭,否则起冲突,而且360安装中心的晶核防护也会占用虚拟技术。
* 本地机的地址也要正确分配,一般要能连上网或者自己设定固定IP。
##### 3、开发方式:
* 通过ssh在命令行里进行操作
* 因为我们共享了本地文件夹,因此可以直接在本地windows上进行开发。
网友评论