PHP Laravel 開發的環境搭建 for Windows

PHP Laravel 開發的環境搭建 for Windows

搭建這個開發環境,不是必須的,倒是最好的。雖然麻煩點,倒是一勞永逸的。
開發環境用到三個軟件是VirtualBox、Vagrant、Homestead,如今先科普概念。php

VirtualBox

VitrualBox 是一款很是強大的免費虛擬機軟件,使用者能夠在 VitrualBox 上安裝並運行 Linux、Windows、Mac OS X 等操做系統,相似的軟件還有 VMware 等linux

Homestead

Homestead 是 Laravel 官方預封裝的一套開發環境。在Laravel 的開發中,強烈建議使用 Homestead,不管是一我的開發項目,仍是團隊開發。laravel

附註: 若是你是 Windows 用戶,你可能須要啓用硬件虛擬化(VT-x)。這一般須要經過 BIOS 來啓用它。git

Vagrant

每次開始一個新的項目,必然要先搭建開發環境,不一樣的開發者可能習慣使用不一樣的系統,有人用 windows,有人用 mac,有人用 linux,在搭建環境的過程當中又有可能會遇到各類 BUG 各類坑,代碼還沒開始寫,搭環境就先用掉幾天時間..非常頭疼,因而,爲了解決這個問題,Vagrant 應運而生...github

第一步 安裝必須的軟件

  • 安裝vagrant
  • 安裝Virtualbox
  • 安裝Homestead :包括盒子安裝和工具安裝。

第二步 安裝 Homestead 盒子(vagrant box add)

vagrant box add laravel/homestead

使用上述命令,會自動從國外網上下載box, 目前最新的box是5.0.1, 國外的比較慢。shell

建議下載過來,經過本地方式添加box:json

vagrant box add laravel/homestead file:///F:/BaiduNetdiskDownload/homestead-5.0.1.box

我作了個百度網盤連接 :ubuntu

homestead-5.0.1.box:https://pan.baidu.com/s/1eTHY88q 密碼:qn3i

添加後,Vagrant會將所下載的box保存到 ~/.vagrant.d/boxes 目錄下。
對windows系統,大體目錄是:C:\Users\Administrator.vagrant.d\boxes。windows

安裝好後,最好用list命令驗證下。centos

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

相關文章
相關標籤/搜索