美文网首页
Mac配置Homestead

Mac配置Homestead

作者: 在牛魔角上狂码 | 来源:发表于2018-12-12 02:02 被阅读0次

    安装VirtualBox

    VirtualBox 是 Oracle 公司的开源虚拟机软件。VirtualBox 号称是最强的免费虚拟机软件,它不仅功能齐全,而且性能也很优异!VirtualBox 支持大部分流行的系统,如:Mac, Windows, Linux 等。

    官网:https://www.virtualbox.org/wiki/Downloads
    

    一步一步按照默认安装即可。


    安装Vargant

    Vagrant 是用来管理虚拟机的工具,支持当前主流的虚拟机系统如 VirtualBox、VMware、AWS 等。Vagrant 的主要作用是提供一个可配置、可移植和复用的软件环境。Vagrant 让你通过编写一个 Vagrantfile 文件来控制虚拟机的启动、虚拟机网络环境的配置、虚拟机与主机间的文件共享,以及启动后自动执行一些配置脚本,如自动执行一个 Shell Script 来安装一些必备的开发工具,如安装配置MySQL、PHP,甚至是自动配置 Nginx 站点。这意味着,在一个多人开发的项目中,你只需要同步 Vagrantfile 文件,就可以保证参与项目的每个人各自的机器上拥有一致的开发环境。

    官网:https://www.vagrantup.com/downloads.html
    

    一步一步按照默认安装即可。


    安装Homestead

    1、由于是下载的地址是外网,网络差、不稳定,所有下配置Composer加速

    安装composer

    下载:https://getcomposer.org/composer.phar
    

    得到一个 composer.phar 文件

    sudo mv composer.phar /usr/local/bin/composer
    

    输入密码

    mac密码
    

    修改权限

    sudo chmod a+x /usr/local/bin/composer
    

    输入

    composer
    

    显示

       ______
      / ____/___  ____ ___  ____  ____  ________  _____
     / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
    / /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
    \____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                        /_/
    Composer version 1.5.2 2017-09-11 16:59:25
    
    Usage:
      command [options] [arguments]
    
    Options:
      -h, --help                     Display this help message
      -q, --quiet                    Do not output any message
      -V, --version                  Display this application version
          --ansi                     Force ANSI output
          --no-ansi                  Disable ANSI output
      -n, --no-interaction           Do not ask any interactive question
          --profile                  Display timing and memory usage information
          --no-plugins               Whether to disable plugins.
      -d, --working-dir=WORKING-DIR  If specified, use the given directory as working directory.
      -v|vv|vvv, --verbose           Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
    
    ....
    ....
    
    

    composer安装成功

    配置composer中国镜像

    composer config -g repo.packagist composer https://packagist.laravel-china.org
    

    取消镜像

    composer config -g --unset repos.packagist
    

    2、安装Homestead虚拟盒子

    vagrant box add laravel/homestead
    

    下载速度有点慢,下载完成后

    vagrant box list
    

    显示

    
    

    至此完成 Homestead Vagrant 盒子的导入。

    3、安装Git

    官网:https://git-scm.com/download/mac
    

    一步一步按照默认安装即可

    git --version
    

    有显示则表示安装成功

    4、下载Homestead管理脚本

    cd ~
    
    git clone https://github.com/laravel/homestead.git Homestead
    

    进入Homestead目录,使用Git检出我们所需的版本。 查看最新稳定的Homestead版本

    cd ~/Homestead
    
    git checkout -b v6.5.0
    
    

    Homestead配置文件

    初始化Homestead

    bash init.sh
    

    1、虚拟机设置
    . 虚拟机设置#

    Homestead 支持我们对虚拟机的 IP,内存,CPU,虚拟机的默认提供者进行配置。这里我们基本不需要做任何配置,因此可以跳过

    ip: "192.168.10.10"
    memory: 2048
    cpus: 1
    provider: virtualbox
    

    2、SSH秘钥登录设置

    authorize 选项是指派登录虚拟机授权连接的公钥文件,此文件填写的是主机上的公钥文件地址,虚拟机初始化时,此文件里的内容会被复制存储到虚拟机的 /home/vagrant/.ssh/authorized_keys文件中,从而实现 SSH 免密码登录。在这里我们默认填写即可

    authorize: ~/.ssh/id_rsa.pub
    

    keys 是数组选项,填写的是本机的 SSH 私钥文件地址。虚拟机初始化时,会将此处填写的所有 SSH 私钥文件复制到虚拟机的 /home/vagrant/.ssh/ 文件夹中,从而使虚拟机能共享主机上的 SSH 私钥文件,使虚拟机具备等同于主机的身份认证。此功能为 SSH 授权提供了便利,例如在后面章节中,我们只需要在 GitHub 上配置一个 SSH 公钥,即可实现 GitHub 对虚拟机和主机共同认证。

    此处我们将公钥和私钥一起同步到虚拟机中:

    keys:
        - ~/.ssh/id_rsa
        - ~/.ssh/id_rsa.pub
    

    接下来我们来生成 SSH Key,开始之前,我们先使用以下命令来检查主机上是否已经生成过 SSH Key:

    ls -al ~/.ssh
    

    如果存在 id_rsa 和 id_rsa.pub的话,请跳过以下生成 SSH 的步骤继续阅读剩下内容。

    否则使用以下方法来生成 SSH Key,请将 czhd2552@163.com 替换为你的邮箱:

    ssh-keygen -t rsa -C "czhd2552@163.com"
    

    命令行会提示让你指定秘钥的名称,按回车键将 SSH Key 保存到默认文件名即可:

    Enter a file in which to save the key (/Users/you/.ssh/id_rsa): [Press enter]
    

    接下来会询问你为 SSH Key 设置密码,按回车键即可,默认为空密码:

    Enter passphrase (empty for no passphrase): [Type a passphrase]
    
    Enter same passphrase again: [Type passphrase again]
    

    可以都选择默认,也就是直接敲击几个 Enter 键即可。这时候我们再检查一下:

    ls -al ~/.ssh
    

    可以看到以下两个文件:

    • id_rsa —— SSH 秘钥的 私钥 (Private Key)
    • id_rsa.pub —— SSH 秘钥的 公钥 (Public Key)

    3、共享文件夹配置

    统一配置如下:

    folders:
        - map: ~/Code
          to: /home/vagrant/Code
    

    默认 Homestead 会将我们本机的 ~/Code 文件夹映射到 /home/vagrant/Code 文件夹上。现在我们本机还没有 ~/Code 文件夹,让我们来创建一个:

    cd ~
    
    mkdir Code
    

    4、站点配置

    sites:
        - map: homestead.app
          to: /home/vagrant/Code/Laravel/public
    

    另外,主机里直接访问虚拟机站点,必须通过绑定 hosts 来实现。接下来我们利用 hosts 文件绑定 homestead.app 到虚拟机 IP 192.168.10.10 上。hosts 文件的完整路径为 /etc/hosts,

    在 hosts 文件的最后面加入以下一行:

    192.168.10.10  homestead.app
    

    到此站点配置好了

    5、数据库配置

    我们可以为 Homestead 指定数据库名称,这里使用默认设置即可

    databases:
        - homestead
    
    1. 自定义变量

    最后,如果你需要自定义一些在虚拟机上可以使用的自定义变量,则可以在 variables 中进行定义。

    variables:
        - key: APP_ENV
          value: local
    

    安装遇到的问题

    问题一:

    The provider 'virtualbox' that was requested to back the machine
    'homestead-7' is reporting that it isn't usable on this system. The
    reason is shown below:
    
    Vagrant has detected that you have a version of VirtualBox installed
    that is not supported by this version of Vagrant. Please install one of
    the supported versions listed below to use Vagrant:
    
    4.0, 4.1, 4.2, 4.3, 5.0, 5.1
    
    A Vagrant update may also be available that adds support for the version
    you specified. Please check www.vagrantup.com/downloads.html to download
    the latest version.
    

    VirtualBox 的版本过高,安装个VirtualBox版本低点即可

    问题二:

    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: Checking if box 'laravel/homestead' is up to date...
    ==> homestead-7: There was a problem while downloading the metadata for your box
    ==> homestead-7: to check for updates. This is not an error, since it is usually due
    ==> homestead-7: to temporary network problems. This is just a warning. The problem
    ==> homestead-7: encountered was:
    ==> homestead-7: 
    ==> homestead-7: Couldn't resolve host 'vagrantcloud.com'
    ==> homestead-7: 
    ==> homestead-7: If you want to check for box updates, verify your network connection
    ==> homestead-7: is valid and try again.
    A VirtualBox machine with the name 'homestead-7' already exists.
    Please use another name or delete the machine with the existing
    name, and try again.
    
    

    之前安装步骤中出错,但VirtualBox中已经安装了hometead目录了,到~/VirtualBox VMs/中删除掉homestead目录,重启启动vagrant

    相关文章

      网友评论

          本文标题:Mac配置Homestead

          本文链接:https://www.haomeiwen.com/subject/swoyhqtx.html