PHP Laravel-Homestead開發環境安裝與配置

Laravel Homestead 是一個官方預載的 Vagrant「封裝包」,提供你一個美好的開發環境,不須要在你的本機端安裝 PHP、HHVM、網頁服務器或任何服務器軟件。 Homestead 能夠在任何 Windows、Mac 或 Linux 上面運行, 裏面包含了 Nginx 網頁服務器、PHP 5.六、MySQL、Postgres、Redis、Memcached等軟件, 還有全部你要開發精彩的 Laravel 應用程序所需的軟件。php

本機系統環境:

$ uname -mnprs
Darwin YONG-TEST.local 15.6.0 x86_64 i386

$ sw_vers
ProductName:  Mac OS X
ProductVersion: 10.11.6
BuildVersion: 15G31

不使用用Homestead,直接在本機安裝:

使用homebrew安裝PHP7:html

$ brew update
$ brew search php70
homebrew/php/php70               
homebrew/php/php70-gmagick       
homebrew/php/php70-maxminddb     
homebrew/php/php70-pdo-pgsql     
homebrew/php/php70-stats       
homebrew/php/php70-amqp

$ brew install homebrew/php/php70mysql

配置php-fpm端口爲9002, 由於本機已經安裝了php5.4, 佔用了9001端口。laravel

配置Nginx配置文件:git

重啓Nginx服務器。github

接下來,咱們主要看怎麼使用Homestead安裝Laravel開發環境?sql

安裝Homestead Vagrant Box:

下載 Homestead:數據庫

$ git clone https://github.com/laravel/homestead.git Homestead
$ cd Homestead/
$ ls
CHANGELOG.md Vagrantfile composer.lock init.bat readme.md /src
LICENSE.txt composer.json homestead init.sh /scripts
$ bash init.sh

下載 Homestead Vagrant Box:json

$ vagrant box add laravel/homesteadubuntu

執行示例:

//下載安裝laravel/homestead: 
$ vagrant box add laravel/homestead --force
==> box: Loading metadata for box 'laravel/homestead'
box: URL: https://atlas.hashicorp.com/laravel/homestead
==> box: Adding box 'laravel/homestead' (v0.5.0) for provider: virtualbox
box: Downloading: https://atlas.hashicorp.com/laravel/boxes/homestead/versions/0.5.0/providers/virtualbox.box
box: Progress: 5% (Rate: 572k/s, Estimated time remaining: 0:33:13)
...
//下載成功後顯示以下:
==> box: Successfully added box 'laravel/homestead' (v0.5.0) for 'virtualbox'!

啓動虛擬機

$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
There are errors in the configuration of this machine. Please fix the following errors and try again:
vm: * The host path of the shared folder is missing: ~/Code
//報錯:沒有「~/Code」目錄,那咱們就建立一個:
$ mkdir ~/Code
$ vagrant up

打開VirtualBox中,能夠看到有一個名爲"homestead-7"的虛擬機正在運行(狀態爲:Running)。

配置Hosts文件:

$ sudo vi /etc/hosts
//增長一條記錄:  
192.168.10.10  homestead.app

Make sure the IP address listed is the one set in your ~/.homestead/Homestead.yaml file.

使用laravel建立一個站點:

$ cd ~/Code $ composer require laravel/homestead --dev

配置文件「Homestead.yaml」裏包含一個示例站點配置. 注意默認的站點是"/home/vagrant/Code/Laravel/public", 因此咱們如今建立在~/Code下面建立一個名爲"Laravel"。

$ laravel new Laravel -bash: laravel: command not found

$ ~/.composer/vendor/bin/laravel new Laravel

/home/vagrant/Code/Laravel/public

打開瀏覽器,輸入"http://homestead.app/", 便可正常訪問。

配置 Homestead

配置文件路徑是: ~/.homestead/Homestead.yaml

配置文件內容以下:

ip: "192.168.10.10"
memory: 2048
cpus: 1
provider: virtualbox
authorize: ~/.ssh/id_rsa.pub
keys:
    - ~/.ssh/id_rsa
folders:
    - map: ~/Code
      to: /home/vagrant/Code
sites:
    - map: homestead.app
      to: /home/vagrant/Code/Laravel/public
databases:
    - homestead

配置示例: 新增一個網站"kaixin123.app":

(1) 新增網站目錄"kaixin123net": 在Code目錄中新增一個kaixin123net目錄,做爲新增網站的根目錄: 建立一個index.html文件,隨便寫寫內容,好比"hello,kaixin123.app": 路徑以下: ~/Code/kaixin123net/index.html

(2) 增長sites:

$ vim ~/.homestead/Homestead.yaml

//在sites中新增一條map記錄,修改後配置以下: 
//配置中的xunij  「/home/vagrant/Code/kaixin123net」 對應咱們本地的 "~/Code/kaixin123net"
sites:
    ### 默認
    - map: homestead.app
      to: /home/vagrant/Code/Laravel/public
    ### 新增長一個網站,域名爲kaixin123.app
    - map: kaixin123.app
      to: /home/vagrant/Code/kaixin123net

(3) 新增一條host記錄:

$ sudo vim /etc/hosts

//新增一條host記錄,IP是Homestead.yaml中「ip」,修改後以下:
... (略) ...
192.168.10.10 homestead.app
192.168.10.10 kaixin123.app

(4) 重啓vagrant虛擬機:
$ cd ~/Homestead/
$ vagrant reload --provision

(5) 打開瀏覽器,輸入"http://kaixin123.app/", 能夠正常訪問。

登陸虛擬機:

$ cd ~/Homestead/

$ vagrant ssh

/進入~/Homestead/目錄
YONGTEST:~ WangTom$ cd ~/Homestead/
YONGTEST:Homestead WangTom$ 

//登陸虛擬機
YONGTEST:Homestead WangTom$ vagrant ssh
Welcome to Ubuntu 16.04 LTS (GNU/Linux 4.4.0-22-generic x86_64)
* Documentation:  https://help.ubuntu.com/
   Last login: Fri Sep 23 06:42:33 2016 from 10.0.2.2

//有Code文件夾,這個/home/vagrant/Code與本機的~/Code目錄會保持同步
vagrant@homestead:~$ ls
Code

//退出虛擬機
vagrant@homestead:~$ exit
logout
Connection to 127.0.0.1 closed.

鏈接Homestead數據庫:

To connect to your MySQL or Postgres database from your host machine via Navicat or Sequel Pro: Connect to 127.0.0.1 and port 33060 (MySQL) or 54320 (Postgres). The username and password for both databases is homestead / secret.

地址: 192.168.10.10
端口: 3306

用戶名: homestead

密碼: secret

登陸虛擬機數據庫:

vagrant@homestead:~$ mysql -h192.168.10.10 -uhomestead -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 51
Server version: 5.7.13-0ubuntu0.16.04.2 (Ubuntu)
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| homestead          |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)
mysql>

擴展閱讀

(1) brew

brew(即Homebrew), 是macOS上的軟件包管理工具, 能在Mac中方便的安裝軟件或者卸載軟件,相似ubuntu系統下的apt-get的功能. Homebrew- The missing package manager for OS X http://brew.sh/

(2) Vagrant

Vagrant是一個基於Ruby的工具,用於建立和部署虛擬化開發環境。它使用Oracle的開源VirtualBox虛擬化系統,使用Chef建立自動化虛擬環境。 https://www.vagrantup.com/

(3) Laravel

Laravel是一套簡潔、優雅的PHP Web開發框架(PHP Web Framework)。 https://laravel.com/

參考資料

https://laravel.com/docs/5.3/homestead
http://laravelacademy.org/post/2749.html
http://laravel-china.org/docs/5.1/homestead
https://www.vagrantup.com/docs/getting-started/

更新記錄

  1. 20160825 新增此文檔 (wangyt)
  2. 20160829 更新文檔,改爲Homestead安裝與配置 (wangyt)
  3. 20160922 更新文檔,新增配置多站點 (wangyt)

[END]

相關文章
相關標籤/搜索