在 windows10 環境下安裝 homestead 開發環境,網上有很多相關教程其中大多都是 mac 環境,很多大神都是使用者的 mac 。 但是還是有很多像我這樣的小弟還是用的 windows 系統,而在 win10 系統上面因為許可權問題安裝 homestead 還是有各種問題發生。
需求:
安裝完 VirtualBox 5.2 和 Vagrant 你可以在終端中執行下面的命令將 laravel/homestead box 新增到 Vagrant 中安裝(如果你出現下載很慢,或者無法下載的問題,可以開啟一個vpn ,這裡推薦一個免費的 setupVpn)
vagrant box add laravel/homestead
如果你的網路實在不好,我就碰到過公司網路超級慢的公司。可以使用本地安裝的方法。
https://www.jianshu.com/p/ae9d1261bbd8
注意:我們下載box的時候最好下載最新的版本,如果下載的box和後面的homestead 版本不相配可能會 出現各種奇怪的問題
安裝 Homestead
你可以通過克隆倉庫程式碼來實現 Homestead 安裝。將倉庫克隆到使用者目錄下的 Homestead 目錄,這樣 Homestead 盒子就可以作為所有其他 Laravel 專案的主機:
git clone https://github.com/laravel/homestead.git homestead
克隆完成後,我們切換到最新的分支版本 v7.14.2 ,我們用的最新homestead.box 6.3.0:
// Clone the desired release...
git checkout v7.14.2
接下來,在 Homestead 目錄下執行 bash init.sh 命令來建立 Homestead.yaml 配置檔案,生成的 Homestead.yaml 配置檔案檔案位於當前 Homestead 目錄:
bash init.sh
配置 Homestead.yaml
name: homestead
ip: 192.168.10.10
memory: 2048
cpus: 2
provider: virtualbox
authorize: ~/.ssh/id_rsa.pub
keys:
- ~/.ssh/id_rsa
folders:
-
map: 'D:\webroot\'
to: /home/vagrant
type: nfs
sites:
-
map: homestead.test
to: /home/vagrant/homestead/public
databases:
- homestead-test
nfs 模式就要是提升虛擬機器和本機之間的檔案讀取速度,可以不開啟 nfs, 如果要開啟可以參考這篇教程 解決 Windows 系統使用 Homestead 執行 Laravel 本地專案響應緩慢問題
不要忘記把 Nginx 站點配置中的域名新增到本地機器上的 hosts 檔案中,該檔案會將對本地域名的請求重定向到 Homestead 虛擬機器,在 Mac 或 Linux上,該檔案位於 /etc/hosts,在 Windows 上,位於 C:\Windows\System32\drivers\etc\hosts,新增方式如下:
192.168.10.10 homestead.test
啟動 Vagrant 盒子
配置好 Homestead.yaml 檔案後,在 Homestead 目錄下執行 vagrant up 命令,Vagrant 將會啟動虛擬機器並自動配置共享資料夾以及 Nginx 站點,初次啟動需要花費一點時間進行初始化:
Administrator@SC-201808141722 MINGW64 /d/homestead ((v7.14.2))
$ vagrant up --provision
==> vagrant: A new version of Vagrant is available: 2.1.4!
==> vagrant: To upgrade visit: https://www.vagrantup.com/downloads.html
Bringing machine 'homestead' up with 'virtualbox' provider...
==> homestead: Checking if box 'laravel/homestead' is up to date...
==> homestead: Clearing any previously set forwarded ports...
==> homestead: Clearing any previously set network interfaces...
==> homestead: Preparing network interfaces based on configuration...
homestead: Adapter 1: nat
homestead: Adapter 2: hostonly
==> homestead: Forwarding ports...
homestead: 80 (guest) => 8000 (host) (adapter 1)
homestead: 443 (guest) => 44300 (host) (adapter 1)
homestead: 3306 (guest) => 33060 (host) (adapter 1)
homestead: 4040 (guest) => 4040 (host) (adapter 1)
homestead: 5432 (guest) => 54320 (host) (adapter 1)
homestead: 8025 (guest) => 8025 (host) (adapter 1)
homestead: 27017 (guest) => 27017 (host) (adapter 1)
homestead: 22 (guest) => 2222 (host) (adapter 1)
==> homestead: Running 'pre-boot' VM customizations...
==> homestead: Booting VM...
==> homestead: Waiting for machine to boot. This may take a few minutes...
homestead: SSH address: 127.0.0.1:2222
homestead: SSH username: vagrant
homestead: SSH auth method: private key
homestead: Warning: Connection reset. Retrying...
homestead: Warning: Connection aborted. Retrying...
homestead: Warning: Connection reset. Retrying...
homestead: Warning: Connection aborted. Retrying...
homestead: Warning: Connection reset. Retrying...
homestead: Warning: Connection aborted. Retrying...
==> homestead: Machine booted and ready!
==> homestead: Checking for guest additions in VM...
==> homestead: Setting hostname...
==> homestead: Configuring and enabling network interfaces...
==> homestead: Running provisioner: shell...
homestead: Running: script: Creating Certificate: homestead.test
==> homestead: Running provisioner: shell...
homestead: Running: script: Creating Site: homestead.test
==> homestead: Running provisioner: shell...
homestead: Running: inline script
==> homestead: Running provisioner: shell...
homestead: Running: script: Checking for old Schedule
==> homestead: Running provisioner: shell...
homestead: Running: script: Clear Variables
homestead: sed: can't read /home/vagrant/.profile: No such file or directory
==> homestead: Running provisioner: shell...
homestead: Running: script: Restarting Cron
==> homestead: Running provisioner: shell...
homestead: Running: script: Restarting Nginx
==> homestead: Running provisioner: shell...
homestead: Running: script: Creating MySQL Database: homestead-test
==> homestead: Running provisioner: shell...
homestead: Running: script: Creating Postgres Database: homestead-test
==> homestead: Running provisioner: shell...
homestead: Running: script: Update Composer
homestead: You are already using composer version 1.7.2 (stable channel).
==> homestead: Running provisioner: shell...
homestead: Running: C:/Users/ADMINI~1/AppData/Local/Temp/vagrant-shell20180903-12352-ohdsb3.sh
裡面會顯示出建立的站點和資料庫等資訊 ,現在就可以通過 homestead.test 訪問你的專案了。
一個小經驗:
如果你用的是 win10 系統,而且用的是不正版系統。當你在 git bash 裡面 使用 vagrant up 初始化啟動 (第一次啟動)homestead 虛擬機器的時候,出現一些奇怪的錯誤。 你可以嘗試使用 以管理員身份執行 cmd 的方式啟動 homestead 有時候會有奇蹟發生(個人理解是要在windows的系統盤裡面建立和呼叫一些檔案,因為 win10 的許可權設定的很嚴格所以會導致 homestead 無法正常初始化)。