Windows上使用Vagrant打造Laravel Homestead可協同跨平臺開發環境

一、簡介

Laravel 致力於讓整個 PHP 開發過程變得讓人愉悅,包括本地開發環境,爲此官方爲咱們提供了一整套本地開發環境 —— Laravel Homesteadphp

Laravel Homestead 是一個打包好各類 Laravel 開發所須要的工具及環境的 Vagrant 盒子(Vagrant 提供了一個便捷的方式來管理和設置虛擬機),該盒子爲咱們提供了優秀的開發環境,有了它,咱們再也不須要在本地環境安裝 PHP、HHVM、Web服務器以及其它工具軟件,咱們也徹底不用再擔憂誤操做搞亂操做系統 —— 由於 Vagrant 盒子是一次性的,若是出現錯誤,能夠在數分鐘內銷燬並從新建立該 Vagrant 盒子!html

Homestead能夠運行在 Windows、Mac 以及 Linux 系統上,其中已經安裝好了Nginx、PHP7.0、MySQL、Postgres、Redis、Memcached、Node以及不少其它開發 Laravel 應用所須要的東西。java

注:若是你使用的是Windows,須要開啓系統的硬件虛擬化(VT-x),這一般能夠經過BIOS來開啓。node

預裝軟件

  • Ubuntu 14.04
  • Git
  • PHP 7.0
  • HHVM
  • Xdebug
  • Nginx
  • MySQL
  • SQLite 3
  • Postgres
  • Composer
  • Node(With PM2, Bower, Grunt, and Gulp)
  • Redis
  • Memcached
  • Beanstalkd
  • Blackfire Profiler

環境準備

  1. Git
  2. PHP
  3. Laravel
  4. Composer
  5. Vagrant
  6. VirtualBox

安裝git

1.下載GitHub for Windowsmysql

https://windows.github.com/linux

安裝php

建議你們儘可能安裝當前最新版本的 PHPlaravel

1.下載PHPgit

http://windows.php.net/download/github

2.解壓目錄web

個人路徑D:\php

3.添加環境變量

右鍵計算機->高級系統設置->環境變量->系統變量->PATH

C:\ProgramData\Oracle\Java\javapath;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;C:\nodejs\;D:\php;C:\ProgramData\ComposerSetup\bin

4.設置php.ini

進入 PHP 安裝目錄(例如 D:\php)。找到 php.ini-development 文件並複製一份到當前目錄,重命名爲 php.ini,修改如下配置
去掉extension=php_mbstring.dll 前面的分號(888 行左右)
去掉extension=php_openssl.dll前面的分號(893 行左右)
去掉extension_dir = "ext"前面的分號(736 行左右)

5.使環境變量生效

重啓explorer.exe

安裝Laravel

1.下載Laravel

http://www.golaravel.com/download/

2.解壓目錄

個人路徑D:\laravel-v5.1.4

3.啓動Laravel

d: 
cd laravel-v5.1.4 
D:\laravel-v5.1.4>php artisan serve 
Laravel development server started on http://localhost:8000/

在瀏覽器中訪問http://localhost:8000/

artisanserve 命令還支持兩個參數:

host 設置主機地址
port 設置 web server 監聽的端口號
例如:php artisan serve --port=8888

安裝Composer

1.下載Composer-Setup.exe

https://getcomposer.org/doc/00-intro.md#installation-windows

2.配置Composer

Loading composer repositories with package information 
Installing dependencies (including require-dev) 
SSL certificate problem, verify that the CA cert is OK. Details:  
error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed」

若是安裝過程提示報錯爲缺乏CA證書,下載cacert.pem到自定義路徑
http://curl.haxx.se/docs/caextract.html

而後修改php.ini文件(1983行左右)

openssl.cafile=D:\php\verify\cacert.pem

3.測試Composer

composer -V 
Composer version 1.0-dev (d79427f1a7b15e8f4d46ce8124a4d0c58ba1479c) 2016-01-27 13:01:22

windows安裝可參考

http://pkg.phpcomposer.com/

二、安裝 & 設置

準備工做

  • VirtualBox 虛擬機,基礎依賴
  • Vagrant 基於VirtualBox或vmware的虛擬化環境部署工具,能夠將一個虛擬機打包(簡稱vbox)給別人。
  • Homestead laravel官方vbox,集成環境以及一套特製的命令行工具。
  • xshell 這是一款windows下的ssh & shell工具,同類的有securecrt、putty等。

在使用 Homestead 以前,須要先安裝 Virtual Box/VMWare 和 Vagrant,全部這些軟件包都爲經常使用操做系統提供了一個便於使用的可視化安裝器。

安裝完之後重啓一下,再打開cmd

vagrant --version

Vagrant 1.8.1

 

下載安裝 Homestead Vagrant 盒子

經過命令

VirtualBox/VMWare 和 Vagrant 安裝好了以後,在終端中使用能以下命令將 laravel/homesterad 添加到 Vagrant 中。下載該盒子將會花費一些時間,時間長短主要取決於你的網絡鏈接速度:

vagrant box add laravel/homestead

若是上述命令執行失敗,可使用 Vagrant 老版本的方式,這須要輸入完整的 URL:

vagrant box add laravel/homestead https://atlas.hashicorp.com/laravel/boxes/homestead

能夠自動下載homestead的vbox,然而國內網絡你們懂的,龜速。

建議本身有vps的同窗,在vps上經過

wget -O homestead.box https://atlas.hashicorp.com/laravel/boxes/homestead/versions/0.4.1/providers/virtualbox.box

手動下載後,再從vps上將box下載到本地,或者能夠試試用迅雷下載。總之,想辦法把這個box文件下載下來,而後經過如下命令添加到vagrant

vagrant box add laravel/homestead file:///d:/homestead.box

經過 GitHub 安裝 Homestead命令行工具

你還能夠經過簡單克隆倉庫代碼來實現 Homestead 安裝。將倉庫克隆到用戶目錄下的 Homestead 目錄,這樣 Homestead 盒子就能夠會做爲全部其餘 Laravel 項目的主機:

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

克隆完成後,在 Homestead 目錄下運行 bash init.sh 命令來建立 Homestead.yaml 配置文件,Homestead.yaml 配置文件文件位於 ~/.homestead 目錄:

bash init.sh

安裝依賴

composer install

在Homestead目錄執行

php homestead
Laravel Homestead version 3.0.1

 

配置 Homestead

設置 Provider

Homestead.yaml 文件中的 provider 鍵表示使用哪一個 Vagrant 提供者:virtualboxvmware_fushion 或者vmware_workstation,你能夠將其設置爲本身喜歡的提供者:

provider: virtualbox

配置共享文件夾

C:\Users\stone\.homestead\Homestead.yaml

Homestead.yaml 文件中的 folders 屬性列出了全部主機和 Homestead 虛擬機共享的文件夾,一旦這些目錄中的文件有了修改,將會在本地和 Homestead 虛擬機之間保持同步,若是有須要的話,你能夠配置多個共享文件夾(通常一個就夠了):

folders:
    - map: d:/app
      to: /home/vagrant/Code

若是要開啓 NFS,只需簡單添加一個標識到同步文件夾配置:

folders:
    - map: ~/Code
      to: /home/vagrant/Code
      type: "nfs"

配置 Nginx 站點

對 Nginx 不熟?沒問題,經過 sites 屬性你能夠方便地將「域名」映射到 Homestead 虛擬機的指定目錄,Homestead.yaml 中默認已經配置了一個示例站點。和共享文件夾同樣,你能夠配置多個站點:

sites:
    - map: homestead.app
      to: /home/vagrant/Code/Laravel/public

你還能夠經過設置 hhvm 爲 true 讓全部的 Homestead 站點使用 HHVM:

sites:
    - map: homestead.app
      to: /home/vagrant/Code/Laravel/public
      hhvm: true

默認狀況下,每一個站點均可以經過 HTTP(端口號:8000)和 HTTPS(端口號:44300)進行訪問。

修改Hosts文件|訪問網站

不要忘記把 Nginx 站點配置中的域名添加到本地機器上的 hosts 文件中,該文件會將對本地域名的請求重定向到 Homestead 虛擬機,在 Mac 或 Linux上,該文件位於 /etc/hosts,在 Windows 上,位於C:\Windows\System32\drivers\etc\hosts,添加方式以下:

域名須要在本機綁定hosts:192.168.10.10 homestead.app

確保 IP 地址和你的 Homestead.yaml 文件中列出的一致,一旦你將域名放置到 hosts 文件,就能夠在瀏覽器中經過該域名訪問站點了!

http://homestead.app

啓動 Vagrant Box

配置好 Homestead.yaml 文件後,在 Homestead 目錄下運行 vagrant up 命令,Vagrant 將會啓動虛擬機並自動配置共享文件夾以及 Nginx 站點。
銷燬該機器,可使用 vagrant destroy –force

vagrant up Box 'laravel/homestead' could not be found 問題解決辦法

Box 'laravel/homestead' could not be found

收到在homestead目錄下建立metadata.json文件,內容爲
{
    "name": "laravel/homestead",
    "versions": [{
        "version": "0.4.1",
        "providers": [{
            "name": "virtualbox",
            "url": "file:///F:/VirtualBox/homestead.box"
        }]
    }]
}

Then run vagrant box add metadata.json

This will install the box with a version and can be confirmed by:

$ vagrant box list
laravel/homestead               (virtualbox, 0.4.0)

You will now be able to perform vagrant up using your local box.

Default login and password is just vagrant.

參考:

http://stackoverflow.com/questions/34946837/box-laravel-homestead-could-not-be-found

GuestAdditions versions on your host (5.0.14) and guest (5.0.6) do not match.問題解決辦法

 

This works for me in linux, I think this will solve the problem in windows as well.

 

Quick Solution for Failed to mount folders in Linux guest issue.

 

Add the following line to your Homestead/Vagrantfile:

config.vbguest.auto_update = false

Your d:\VirtualBox/Homestead/Homestead/Vagrantfile should looks like this:

/...

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

    # To avoid install and uninstall VBoxGuessAdditions during vagrant provisioning.
    config.vbguest.auto_update = false

.../

Save it and execute

$ vagrant destroy --force
$ vagrant up

 

參考:

http://stackoverflow.com/questions/30175290/laravel-homestead-vagrant-vboxsf-not-available-issue

爲指定項目安裝 Homestead

全局安裝 Homestead 將會使每一個項目共享同一個 Homestead 盒子,你還能夠爲每一個項目單獨安裝 Homestead,這樣就會在該項目下建立 Vagrantfile,容許其餘人在該項目中執行 vagrant up 命令,在指定項目根目錄下使用 Composer 執行安裝命令以下:

composer require laravel/homestead --dev

這樣就在項目中安裝了 Homestead。Homestead 安裝完成後,使用 make 命令生成 Vagrantfile 和Homestead.yaml 文件,make 命令將會自動配置 Homestead.yaml 中的 sites 和 folders 屬性。

Mac/Linux:

php vendor/bin/homestead make

Windows:

vendor\bin\homestead make

接下來,在終端中運行 vagrant up 命令而後在瀏覽器中經過 http://homestead.app 訪問站點。不要忘記在/etc/hosts 文件中添加域名 homestead.app

三、平常使用

全局訪問 Homestead

有時候你想要在文件系統的任意位置運行 vagrant up 啓動 Homestead 虛擬機,要實現這一目的須要將 Homestead 安裝目錄添加到系統路徑。這樣你就能夠在系統的任意位置運行 homestead 或 homestead ssh 來啓動/登陸虛擬機。

經過 SSH 鏈接虛擬機

你能夠在 Homestead 目錄下經過運行 vagrant ssh 以 SSH 方式鏈接到虛擬機

vagrant ssh

可是若是你須要以更平滑的方式鏈接到 Homestead,能夠爲主機添加一個別名來快速鏈接到 Homestead 盒子,建立完別名後,可使用 vm 命令從任何地方以 SSH 方式鏈接到 Homestead 虛擬機:

alias vm="ssh vagrant@127.0.0.1 -p 2222"

能夠經過virtualbox打開虛擬機

用戶名和密碼: vagrant.

鏈接到數據庫

默認已經在 Homestead 虛擬機中爲 MySQL 和 Postgres 數據庫作好了配置,更方便的是,Laravel的 .env 還爲鏈接 Homestead 數據庫作好了配置。

想要經過本地的 Navicat 或 Sequel Pro 鏈接到 Homestead 上的 MySQL 或 Postgres 數據庫,能夠經過新建鏈接來實現,主機 IP 都是 127.0.0.1,對於 MySQL 而言,端口號是33060,對 Postgres 而言,端口號是54320,用戶名/密碼是 homestead/secret

注意:只有從本地鏈接 Homestead 的數據庫時才能使用這些非標準的端口,在 Homestead 虛擬機中仍是應該使用默認的 3306 和 5432 端口進行數據庫鏈接配置。

添加更多站點

Homestead 虛擬機在運行時,可能須要添加額外 Laravel 應用到 Nginx 站點。若是是在單個 Homestead 環境中運行多個 Laravel 應用,添加站點很簡單,只需將站點添加到 Homestead.yaml 文件,而後在 Homestead 目錄中運行vagrant provision 命令便可。

  1. Homestead 中新增一個domain的步驟是
  2. 在homestead.ymal 文件裏面編輯好sites和folders這兩個項目
  3. 執行vagrant provision

配置 Cron 調度任務

Laravel 提供了很方便的方式來調度 Cron 任務:只需每分鐘調度運行一次 Artisan 命令 schedule:run 便可。schedule:run 會檢查定義在 App\Console\Kernel 類中定義的調度任務並判斷運行哪些任務。

若是想要爲某個 Homestead 站點運行 schedule:run 命令,須要在定義站點時設置 schedule 爲 true

sites:
    - map: homestead.app
      to: /home/vagrant/Code/Laravel/public
      schedule: true

該站點的 Cron 任務會被定義在虛擬機的 /etc/cron.d 目錄下。

端口轉發配置

默認狀況下,Homestead 端口轉發配置以下:

  • SSH: 2222 → Forwards To 22
  • HTTP: 8000 → Forwards To 80
  • HTTPS: 44300 → Forwards To 443
  • MySQL: 33060 → Forwards To 3306
  • Postgres: 54320 → Forwards To 5432

轉發更多端口

若是你想要爲 Vagrant 盒子添加更多端口轉發,作以下轉發協議設置便可:

ports:
    - send: 93000
      to: 9300
    - send: 7777
      to: 777
      protocol: udp

四、使用 Blackfire Profiler 進行性能分析

SensioLabs 開發的 Blackfire Profiler 能自動收集代碼執行數據,好比內存、CPU 時間、硬盤 I/O 等,Homestead 使得在應用中使用該性能分析器變得垂手可得。

Blackfire Profiler 須要的軟件包已經預安裝到 Homestead 盒子,你只須要在 Homestead.yaml 文件中設置 Blackfire Server ID 和 token:

blackfire:
    - id: your-server-id
      token: your-server-token
      client-id: your-client-id
      client-token: your-client-token

配置好 Blackfire 的憑證以後,在 Homestead 目錄下使用 vagrant provision 從新啓動 Homestead。在此以前,確保你已經查看過 Blackfire 文檔瞭解瞭如何在瀏覽器安裝相應的 Blackfire 擴展。

 

相關密碼

Ubuntu
用戶名:vagrant
密碼:vagrant

mysql密碼
homestead / secret
root / secret

虛擬機操做


在不進入虛擬機的狀況下,還可使用下面的命令對 虛擬機進行管理:

vagrant up (啓動虛擬機)
vagrant halt (關閉虛擬機——對應就是關機)
vagrant suspend (暫停虛擬機——只是暫停,虛擬機內存等信息將以狀態文件的方式保存在本地,能夠執行恢復操做後繼續使用)
vagrant resume (恢復虛擬機 —— 與前面的暫停相對應)
vagrant destroy (刪除虛擬機,刪除後在當前虛擬機所作進行的除開Vagrantfile中的配置都不會保留)

以上命令需在Homestead目錄下執行
參考:
http://stackoverflow.com/questions/26655516/how-to-install-manually-downloaded-box-for-vagrant
http://stackoverflow.com/questions/34946837/box-laravel-homestead-could-not-be-found
http://stackoverflow.com/questions/25981735/laravel-homestead-stuck-on-vm-login
http://os.51cto.com/art/201507/484834.htm
http://yaojinbu.com/post/2749.html
http://x-bird.qiubs.com/laravel-homestead-intro.html?utm_source=tuicool&utm_medium=referral
http://blog.csdn.net/small_rice_/article/details/45366299
http://blog.csdn.net/markely/article/details/49585887
http://www.ekan001.com/articles/40
http://www.tuicool.com/articles/rIVJRvm

相關文章
相關標籤/搜索