Laravel學習之Windows下Homestead環境搭建

來源:互聯網
上載者:User

標籤:rate   postgre   virtual   解決   machine   圖形化介面   交流   sse   私密金鑰   

一、簡介

1、Homestead
Laravel Homestead是官方預封裝的Vagrant box,提供給我們學習和開發人員一個環境,我們在自己的電腦上不要裝PHP,HHVM,WEB伺服器等其他軟體。也不用擔心環境搭建帶來的問題,Vagrant box全部都會給解決好,我們只需要使用就好。如果有問題,我們可以重新銷毀vagrant box重新安裝一個即可。
2、內建軟體
    • Ubuntu 16.04
    • Git
    • PHP 7.0
    • HHVM
    • Nginx
    • MySQL
    • MariaDB
    • Sqlite3
    • Postgres
    • Composer
    • Node (附帶了 PM2, Bower, Grunt, and Gulp)
    • Redis
    • Memcached
    • Beanstalkd
2、Vagrant
在這裡會有疑問vagrant是什麼,其實vagrant是一個工具,用於建立和部署虛擬化開發環境的。拿VirtualBox舉例,VirtualBox會開放一個建立虛擬機器的介面,Vagrant會利用這個介面建立虛擬機器,並且通過Vagrant來管理,配置和自動安裝虛擬機器。

 

二、安裝
1、安裝虛擬機器軟體VirtaulBox或VM(推薦用VirtaulBox),然後安裝vagrant軟體。這裡都是圖形化介面安裝,直接附上軟體的現在地址VirtualBox,Vagrant2、安裝Homestead盒子      在windows控制台中執行如下命令:
  1. vagrant box add laravel/homestead
這個命令會直接從網上下載Homestead盒子大概1G,又是國外的網,所以速度很慢,可以嘗試先用下載工具把Homestead下載下來附上連結:
  1. https://atlas.hashicorp.com/hashicorp/boxes/precise64/versions/1.1.0/providers/virtualbox.box
然後在windows控制台中執行如下的命令,將本地的box與laravel/homestead進行綁定:其中path/to/virtualbox.box是下載後的virtualbox.box的路徑
  1. vagrant box add laravel/homestead /path/to/virtualbox.box
3、安裝Homestead
  a.    如果電腦上安裝了Git(沒有的化就先安裝一下),Git有window用戶端直接安裝),直接將homestead項目複製下來,即如下的命令
  1. git clone https://github.com/laravel/homestead.git Homestead
   b.    進入Git的Git Bash終端執行如下命令,產生秘鑰和公開金鑰
  1. ssh-keygen -t rsa -C "[email protected]"
    c.    從window控制台中進入Homestead檔案夾,執行如下初始化命令
  1. bash init.sh
   執行完命令後在C:\User\UserName\.homestead檔案夾下會有Homestead.yaml設定檔,可以在這裡按自己的需求進行配置,內容如下
  1. ---
  2. #虛擬機器IP
  3. ip:"192.168.10.10"
  4. #虛擬機器記憶體大小
  5. memory:2048
  6. #虛擬機器cpu數量
  7. cpus:1
  8. #提供虛擬機器類型
  9. provider: virtualbox
  10. #公開金鑰SSH KEY
  11. authorize:~/.ssh/id_rsa.pub
  12. #私密金鑰SSH KEY
  13. keys:
  14. -~/.ssh/id_rsa
  15. #共用資料夾
  16. folders:
  17. - map: D:\Xampp\htdocs\blog #自己電腦上的項目路徑
  18. to:/home/vagrant/Code#映射到虛擬機器中的路徑
  19. #網站配置web伺服器配置
  20. sites:
  21. - map: homestead.app #網域名稱
  22. to:/home/vagrant/Code/blog/public #網域名稱的映射地址
  23. #資料庫配置
  24. databases:
  25. - homestead
  26. # blackfire:
  27. # - id: foo
  28. # token: bar
  29. # client-id: foo
  30. # client-token: bar
  31. # ports:
  32. # - send: 50000
  33. # to: 5000
  34. # - send: 7777
  35. # to: 777
  36. # protocol: udp
d.    啟動測試
①若沒有修改配置項執行如下命令
  1. vagrant up
可以看到終端上顯示,如下結果就說明啟動完成
 ②若修改了配置項需要執行下面的命令
  1. vagrant up --provision
 
e.    測試
①SSH測試:現在windows控制台輸入
  1. vagrant ssh
進行SSH串連,執行ls命令查看是否有映射的檔案夾
②瀏覽器訪問虛擬機器網站測試
  1. http://localhost:8000/
在C:\Windows\System32\drivers\etc\hosts添加如下代碼
  1. # Copyright (c) 1993-2009 Microsoft Corp.
  2. #
  3. # This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
  4. #
  5. # This file contains the mappings of IP addresses to host names. Each
  6. # entry should be kept on an individual line. The IP address should
  7. # be placed in the first column followed by the corresponding host name.
  8. # The IP address and the host name should be separated by at least one
  9. # space.
  10. #
  11. # Additionally, comments (such as these) may be inserted on individual
  12. # lines or following the machine name denoted by a ‘#‘ symbol.
  13. #
  14. # For example:
  15. #
  16. # 102.54.94.97 rhino.acme.com # source server
  17. # 38.25.63.10 x.acme.com # x client host
  18. # localhost name resolution is handled within DNS itself.
  19. #127.0.0.1 localhost
  20. #::1 localhost
  21. 192.168.10.10 homestead.app
在瀏覽器中訪問即可
③、資料連線測試通過本地的串連工具進行串連(在這裡用的是Navicat進行串連),使用者名稱是homestead密碼是secret 
三、總結Homestead是個很好用的虛擬環境,只是對於沒有接觸過這些東西的小夥伴們可能會感覺陌生,也會踩很多坑,不過也會越玩越有興趣。感覺Laravel的生態圈越來越完善,Laravel越來越讓人著迷,希望和小夥伴們一起學習交流,後面會更新紀錄自己遇到的坑和一些解決方案。                     

Laravel學習之Windows下Homestead環境搭建

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.