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 項目模板。隨后其它文章中細說。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM