Laravel之homestead安裝法
Laravel據(jù)說(shuō)是目前最優(yōu)雅的php web框架,雖然它沒(méi)有durpal速蕊、wordpress那么豐富份名,但正因?yàn)樗?jiǎn)潔、快速的開(kāi)發(fā)方式,使得大家都非常容易入門,并且作為未來(lái)的吃飯工具,也是可以勝任的训唱。
(注: 這是關(guān)于laravel最基礎(chǔ)的部分褥蚯,用它來(lái)自學(xué)或 者教學(xué)都非常棒挚冤。)
開(kāi)始--安裝
服務(wù)器要求
Laravel對(duì)服務(wù)器的要求不高,一般情況下用一些php平臺(tái)赞庶,如phpstudy等训挡,但是它有一個(gè)專門針對(duì)的本地開(kāi)發(fā)環(huán)境--Larval homestead,可以滿足所有的要求歧强。
1澜薄、準(zhǔn)備
安裝virtualbox和vagrant
我的系統(tǒng)在搭建linux和drupal時(shí)已經(jīng)安裝過(guò)了(建議下載最新的版本,以保證適配)摊册,通過(guò)powershell可以用命令:
Get WmiObject Class Win32_Product | Select Object Property Name
查看肤京,我的版本是Oracle VM VirtualBox 4.1.44、 Vagrant 1.7.2。
2忘分、安裝和配置
新建項(xiàng)目所在路徑棋枕,我的d:\data\vargant_box
用poweshell進(jìn)入項(xiàng)目路徑,敲入命令下載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: --:--:--)
上面的下載會(huì)很慢重斑,也可以使用工具從上面提供的下載的地址中直接下載。
可以用迅雷下載后肯骇,確定好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'!
接下來(lái),大家就可以在虛擬機(jī)環(huán)境中運(yùn)行l(wèi)aravel了笛丙!
2.1 把box添加到vagrant box列表中
1漾脂、打開(kāi)命令行,進(jìn)入box下載的路徑(我是通過(guò)迅雷下載的)
2胚鸯、 vagrant box add laravel/homestead hc-download.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項(xiàng)目
新建laravel目錄蠢琳,從github把homestead項(xiàng)目克隆下來(lái)啊终,并命令為Homestead
PS D:\data\laravel> git clone https://github.com/laravel/homestead.git Homestead
克隆完畢,我們可以在laravel文件夾下看到克隆的目錄:
運(yùn)行init.bat(windows),linux運(yùn)行init.sh
成功后傲须,我們會(huì)在c盤的如下目錄下看到:
5蓝牲、打開(kāi)Homestead.yaml文件,配置homestead
---
ip: "192.168.10.10"
memory: 2048
cpus: 1
provider: virtualbox
authorize: ~/.ssh/id_rsa.pub
keys:
- ~/.ssh/id_rsa
folders:
# 修改為本地主機(jī)laravel的源碼位置
- map: D:\data\laravel\code
to: /home/vagrant/Code
sites:
- map: homestead.app
#注意本地主機(jī)的public的路徑泰讽,它并沒(méi)有l(wèi)aravel這個(gè)根例衍。
#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版本已卸,避免無(wú)法運(yùn)行headstead的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版本舊了佛玄,直接從官網(wǎng)下載最新的vagrant安裝覆蓋即可。
如果出現(xiàn)
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文件出錯(cuò)累澡。這里指第17行有語(yǔ)法錯(cuò)誤梦抢。
###### 最后終于成功運(yùn)行,
運(yùn)行成功提示:
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.
###### 進(jìn)一步測(cè)試:
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愧哟,則可訪問(wèn)到laravel站點(diǎn)奥吩。

###### 總結(jié)經(jīng)驗(yàn):
* 64位系統(tǒng)中,運(yùn)行64位的虛擬機(jī)要確保配置正確

* windows的Hyper V要關(guān)閉蕊梧,否則起沖突霞赫,而且360安裝中心的晶核防護(hù)也會(huì)占用虛擬技術(shù)。
* 本地機(jī)的地址也要正確分配肥矢,一般要能連上網(wǎng)或者自己設(shè)定固定IP端衰。
##### 3、開(kāi)發(fā)方式:
* 通過(guò)ssh在命令行里進(jìn)行操作
* 因?yàn)槲覀児蚕砹吮镜匚募A,因此可以直接在本地windows上進(jìn)行開(kāi)發(fā)旅东。