<article><h1>Laravel 虚拟开发环境 Homestead</h1><ul><li><a href="#introduction">简介</a></li><li><a href="#installation-and-setup">安装与设置</a><ul><li><a href="#first-steps">第一步</a></li><li><a href="#configuring-homestead">配置 Homestead</a></li><li><a href="#launching-the-vagrant-box">启动 Vagrant Box</a></li><li><a href="#per-project-installation">根据项目分开安装</a></li><li><a href="#installing-mariadb">安装 MariaDB</a></li></ul></li><li><a href="#daily-usage">常见用法</a><ul><li><a href="#accessing-homestead-globally">全局可用的 Homestead</a></li><li><a href="#connecting-via-ssh">通过 SSH 连接</a></li><li><a href="#connecting-to-databases">连接数据库</a></li><li><a href="#adding-additional-sites">增加更多网站</a></li><li><a href="#configuring-cron-schedules">配置 Cron 调度器</a></li><li><a href="#ports">连接端口</a></li><li><a href="#sharing-your-environment">共享你的环境</a></li></ul></li><li><a href="#network-interfaces">网络接口</a></li><li><a href="#updating-homestead">更新 Homestead</a></li><li><a href="#old-versions">历史版本</a></li><li><a href="#provider-specific-settings">Provider 的特殊设置</a><ul><li><a href="#provider-specific-virtualbox">VirtualBox</a></li></ul></li></ul><p><a name="introduction"></a></p><h2><a href="#introduction">简介</a></h2><p>Laravel 致力于让 PHP 的开发过程更加轻松愉快,这其中也包括你的本地开发环境。 <a href="https://www.vagrantup.com">Vagrant</a> 提供了一种简单,优雅的方式来管理和调配虚拟机。</p><p>Laravel Homestead 是一个官方预封装的 Vagrant box,它为你提供了一个完美的开发环境,你无需在本地安装 PHP ,web 服务器,或其他服务软件。并且不用担心系统被搞乱! Vagrant box 是完全一次性的。如果有什么地方出错了,你也可以在几分钟内销毁并重建 box !</p><p>Homestead 可以运行在 Windows 、 Mac 或 Linux 系统上,并且里面包含了 Nginx Web 服务器、 PHP 7.1 、 MySQL 、 Postgres 、 Redis 、 Memcached 、 Node 、 以及所有利于你开发 laravel 应用的其他程序。</p><blockquote class="has-icon note"><p><div class="flag"><span class="svg"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:a="http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/" version="1.1" x="0px" y="0px" width="90px" height="90px" viewBox="0 0 90 90" enable-background="new 0 0 90 90" xml:space="preserve"><path fill="#FFFFFF" d="M45 0C20.1 0 0 20.1 0 45s20.1 45 45 45 45-20.1 45-45S69.9 0 45 0zM45 74.5c-3.6 0-6.5-2.9-6.5-6.5s2.9-6.5 6.5-6.5 6.5 2.9 6.5 6.5S48.6 74.5 45 74.5zM52.1 23.9l-2.5 29.6c0 2.5-2.1 4.6-4.6 4.6 -2.5 0-4.6-2.1-4.6-4.6l-2.5-29.6c-0.1-0.4-0.1-0.7-0.1-1.1 0-4 3.2-7.2 7.2-7.2 4 0 7.2 3.2 7.2 7.2C52.2 23.1 52.2 23.5 52.1 23.9z"></path></svg></span></div> 如果你是 Windows 用户,你可能需要启用硬件虚拟化(VT-x)。这通常需要通过 BIOS 来启用它。如果你在一个 UEFI 系统上使用的是 Hyper-V,你需要关闭 Hyper-V 才能启用 VT-x。</p></blockquote><p><a name="included-software"></a></p><h3>内置软件</h3><ul><li>Ubuntu 16.04</li><li>Git</li><li>PHP 7.1</li><li>Nginx</li><li>MySQL</li><li>MariaDB</li><li>Sqlite3</li><li>Postgres</li><li>Composer</li><li>Node (With Yarn, Bower, Grunt, and Gulp)</li><li>Redis</li><li>Memcached</li><li>Beanstalkd</li><li>Mailhog</li><li>ngrok</li></ul><p><a name="installation-and-setup"></a></p><h2><a href="#installation-and-setup">安装与设置</a></h2><p><a name="first-steps"></a></p><h3>第一步</h3><p>在你启动你的 Homestead 环境之前,你必须安装 <a href="https://www.virtualbox.org/wiki/Downloads">VirtualBox 5.1</a> 、 <a href="https://www.vmware.com">VMWare</a> 或 <a href="http://www.parallels.com/products/desktop/">Parallels</a> 的其中之一,以及 <a href="https://www.vagrantup.com/downloads.html">Vagrant</a> 。这些软件在各个常用的平台都有提供简单易用的界面安装包。</p><p>若要使用 VMware provider,你需要同时购买 VMware Fusion / Workstation 以及 <a href="https://www.vagrantup.com/vmware">VMware Vagrant plug-in</a>. 的软件授权。使用 VMware 可以在共享文件夹上获得较快的性能。</p><p>若要使用 Parallels provider,你需要安装 <a href="https://github.com/Parallels/vagrant-parallels">Parallels Vagrant plug-in</a> 。这是免费使用的。</p><h4>安装 Homestead Vagrant Box</h4><p>当 VirtualBox / VMware 以及 Vagrant 安装完成后,你使用以下命令将 laravel/homestead 这个 box 安装进你的 Vagrant 程序中。 box 的下载会花费你一点时间,具体的下载时长由网络速度决定:</p><pre class=" language-php"><code class=" language-php">vagrant box add laravel<span class="token operator">/</span>homestead</code></pre><p>如果上面的命令运行失败,代表你使用的可能是旧版的 Vagrant,请升级你的 Vagrant。</p><h4>安装 Homestead</h4><p>你可以通过手动克隆代码仓库的方式来安装 Homestead。建议将代码仓库克隆至「home」目录中的 Homestead 文件夹,如此一来 Homestead box 就能将主机服务提供给你所有的 Laravel 项目:</p><pre class=" language-php"><code class=" language-php">cd <span class="token operator">~</span>
git clone https<span class="token punctuation">:</span><span class="token operator">/</span><span class="token operator">/</span>github<span class="token punctuation">.</span>com<span class="token operator">/</span>laravel<span class="token operator">/</span>homestead<span class="token punctuation">.</span>git Homestead</code></pre><p>由于 Homestead 的 <code class=" language-php">master</code> 分支并不是稳定分支,你应该检出已经标签过的稳定版本。你可以在 <a href="https://github.com/laravel/homestead/releases">Github Release Page</a> 找到最新的稳定版本。</p><pre class=" language-php"><code class=" language-php">cd Homestead
<span class="token comment" spellcheck="true">
// 检出所需要的版本...
</span>git checkout v4<span class="token number">.0</span><span class="token punctuation">.</span><span class="token number">5</span></code></pre><p>一旦你克隆完 Homestead 的代码仓库,即可在 Homestead 目录中运行 <code class=" language-php">bash init<span class="token punctuation">.</span>sh</code> 命令 来创建 <code class=" language-php">Homesstead<span class="token punctuation">.</span>yaml</code> 配置文件。 <code class=" language-php">Homesstead<span class="token punctuation">.</span>yaml</code> 文件会被放置在你的 Homestead 目录中:</p><pre class=" language-php"><code class=" language-php"><span class="token comment" spellcheck="true">// Mac / Linux...
</span>bash init<span class="token punctuation">.</span>sh
<span class="token comment" spellcheck="true">
// Windows...
</span>init<span class="token punctuation">.</span>bat</code></pre><p><a name="configuring-homestead"></a></p><h3>配置 Homestead</h3><h4>配置你的提供者</h4><p><code class=" language-php">Homestead<span class="token punctuation">.</span>yaml</code> 中的 <code class=" language-php">provider</code> 参数设置取决于你用的是哪一个 Vagrant 提供者 <code class=" language-php">virtualbox</code> 、 <code class=" language-php">vmware_fusion</code> 、 <code class=" language-php">vmware_workstation</code> ,或者 <code class=" language-php">parallels</code> 。你可以根据自己的喜好来设置提供者:</p><pre class=" language-php"><code class=" language-php">provider<span class="token punctuation">:</span> virtualbox</code></pre><h4>配置共享文件夹</h4><p>你可以在 <code class=" language-php">Homestead<span class="token punctuation">.</span>yaml</code> 文件的 <code class=" language-php">folders</code> 属性里列出所有想与 Homestead 环境共享的文件夹。这些文件夹中的文件若有变更,它们将会在你的本机电脑与 Homestead 环境自动更新同步。你可以在这里设置多个共享文件夹:</p><pre class=" language-php"><code class=" language-php">folders<span class="token punctuation">:</span>
<span class="token operator">-</span> map<span class="token punctuation">:</span> <span class="token operator">~</span><span class="token operator">/</span>Code
to<span class="token punctuation">:</span> <span class="token operator">/</span>home<span class="token operator">/</span>vagrant<span class="token operator">/</span>Code</code></pre><p>若要启动 <a href="https://www.vagrantup.com/docs/synced-folders/nfs.html">NFS</a> ,只需要在共享文件夹的设置值中加入一个简单的参数:</p><pre class=" language-php"><code class=" language-php">folders<span class="token punctuation">:</span>
<span class="token operator">-</span> map<span class="token punctuation">:</span> <span class="token operator">~</span><span class="token operator">/</span>Code
to<span class="token punctuation">:</span> <span class="token operator">/</span>home<span class="token operator">/</span>vagrant<span class="token operator">/</span>Code
type<span class="token punctuation">:</span> <span class="token string">"nfs"</span></code></pre><p>你也可以在配置中传递任何 Vagrant 中的 <a href="https://www.vagrantup.com/docs/synced-folders/basic_usage.html">共享文件夹</a> 支持的参数,在 <code class=" language-php">options</code> 参数下列出它们:</p><pre class=" language-php"><code class=" language-php">folders<span class="token punctuation">:</span>
<span class="token operator">-</span> map<span class="token punctuation">:</span> <span class="token operator">~</span><span class="token operator">/</span>Code
to<span class="token punctuation">:</span> <span class="token operator">/</span>home<span class="token operator">/</span>vagrant<span class="token operator">/</span>Code
type<span class="token punctuation">:</span> <span class="token string">"rsync"</span>
options<span class="token punctuation">:</span>
rsync__args<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token string">"--verbose"</span><span class="token punctuation">,</span> <span class="token string">"--archive"</span><span class="token punctuation">,</span> <span class="token string">"--delete"</span><span class="token punctuation">,</span> <span class="token string">"-zz"</span><span class="token punctuation">]</span>
rsync__exclude<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token string">"node_modules"</span><span class="token punctuation">]</span></code></pre><h4>配置 Nginx 站点</h4><p>对 Nginx 不熟悉吗?没关系。<code class=" language-php">sites</code> 属性可以帮助你可以轻易指定一个 <code class=" language-php">域名</code> 来对应到 homestead 环境中的一个目录上。在 <code class=" language-php">Homestead<span class="token punctuation">.</span>yaml</code> 文件中已包含了一个网站设置范本。同样的,你也可以增加多个网站到你的 Homestead 环境中。 Homestead 可以同时为多个 Laravel 应用提供虚拟化环境:</p><pre class=" language-php"><code class=" language-php">sites<span class="token punctuation">:</span>
<span class="token operator">-</span> map<span class="token punctuation">:</span> homestead<span class="token punctuation">.</span>app
to<span class="token punctuation">:</span> <span class="token operator">/</span>home<span class="token operator">/</span>vagrant<span class="token operator">/</span>Code<span class="token operator">/</span>Laravel<span class="token operator">/</span><span class="token keyword">public</span></code></pre><p>如果你在 Homestead box 配置之后更改了 <code class=" language-php">sites</code> 属性,那么应该重新运行 <code class=" language-php">vagrant reload <span class="token operator">--</span>provision</code> 来更新 Nginx 配置到虚拟机上。</p><h4>关于 Hosts 文件</h4><p>你必须将在 Nginx sites 中所添加的「域名」也添加到你本机电脑的 <code class=" language-php">hosts</code> 上。 <code class=" language-php">hosts</code> 文件会将请求重定向至 Homestead 环境中设置的本地域名。在 Mac 或 Linux 上,该文件通常会存放在 <code class=" language-php"><span class="token operator">/</span>etc<span class="token operator">/</span>hosts</code> 。在 Windows 上,则存放于 <code class=" language-php">C<span class="token punctuation">:</span>\<span class="token package">Windows<span class="token punctuation">\</span>System32<span class="token punctuation">\</span>drivers<span class="token punctuation">\</span>etc<span class="token punctuation">\</span>hosts</span></code> 。设置内容如下所示:</p><pre class=" language-php"><code class=" language-php"><span class="token number">192.168</span><span class="token punctuation">.</span><span class="token number">10.10</span> homestead<span class="token punctuation">.</span>app</code></pre><p>务必确认 IP 地址与 <code class=" language-php">Homestead<span class="token punctuation">.</span>yaml</code> 文件中设置的相同。将域名设置在 <code class=" language-php">hosts</code> 文件之后,你就可以通过网页浏览器访问你的网站。</p><pre class=" language-php"><code class=" language-php">http<span class="token punctuation">:</span><span class="token operator">/</span><span class="token operator">/</span>homestead<span class="token punctuation">.</span>app</code></pre><p><a name="launching-the-vagrant-box"></a></p><h3>启动 Vagrant Box</h3><p>编辑完 <code class=" language-php">Homestead<span class="token punctuation">.</span>yaml</code> 后,进入你的 Homestead 目录并运行 <code class=" language-php">vagrant up</code> 命令。 Vagrant 就会根据 <code class=" language-php">Homestead<span class="token punctuation">.</span>yaml</code> 里的配置信息,为虚拟机设置共享文件夹和 Nginx 网站。</p><p>如果要移除虚拟机,你可以使用 <code class=" language-php">vagrant destroy <span class="token operator">--</span>force</code> 命令</p><p><a name="per-project-installation"></a></p><h3>为每个项目分开安装</h3><p>除了全局使用同一个 Homestead 环境, Homestead 还允许你为项目独立配置一个独占的 Homstead 。</p><p>通过传递 <code class=" language-php">Vagrantfile</code> ,可以实现为每个项目分别安装上 Homestead ,其他项目成员只需要通过简单的 <code class=" language-php">vagrant up</code> 即能跟你拥有一样的 Homestead 环境。</p><p>使用 Composer 将 Homestead 直接安装至项目中:</p><pre class=" language-php"><code class=" language-php"> composer <span class="token keyword">require</span> laravel<span class="token operator">/</span>homestead <span class="token operator">--</span>dev</code></pre><p>一旦 Homestead 安装完毕,可以使用 <code class=" language-php">make</code> 命令生成 <code class=" language-php">Vagrantfile</code> 与 <code class=" language-php">Homestead<span class="token punctuation">.</span>yaml</code> 文件,并存放于项目的根目录。</p><p><code class=" language-php">make</code> 命令将会自动在 Homestead.yaml 文件中配置 <code class=" language-php">sites</code> 及 <code class=" language-php">folders</code> :</p><p>Mac / Linux:</p><pre class=" language-php"><code class=" language-php">php vendor<span class="token operator">/</span>bin<span class="token operator">/</span>homestead make</code></pre><p>Windows:</p><pre class=" language-php"><code class=" language-php">vendor\<span class="token package"><span class="token punctuation">\</span>bin<span class="token punctuation">\</span><span class="token punctuation">\</span>homestead</span> make</code></pre><p>接下来,在命令行中运行 <code class=" language-php">vagrant up</code> 并通过网页浏览器访问 <code class=" language-php">http<span class="token punctuation">:</span><span class="token operator">/</span><span class="token operator">/</span>homestead<span class="token punctuation">.</span>app</code> 。再次提醒,你仍然需要在 <code class=" language-php"><span class="token operator">/</span>etc<span class="token operator">/</span>hosts</code> 里配置 <code class=" language-php">homestead<span class="token punctuation">.</span>app</code> 或其它想要使用的域名。</p><p><a name="installing-mariadb"></a></p><h3>安装 MariaDB</h3><p>如果你希望使用 MariaDB 来替换 MySQL,你可以在 <code class=" language-php">Homestead<span class="token punctuation">.</span>yaml</code> 文件中增加一个 <code class=" language-php">mariadb</code> 的选项,这个选项会移除 MySQL 并安装 MariaDB。因为 MariaDB 可用作 MySQL 的替代品,所以在你的数据库配置信息里,还是选用 <code class=" language-php">mysql</code> 配置项。</p><pre class=" language-php"><code class=" language-php">box<span class="token punctuation">:</span> laravel<span class="token operator">/</span>homestead
ip<span class="token punctuation">:</span> <span class="token string">"192.168.20.20"</span>
memory<span class="token punctuation">:</span> <span class="token number">2048</span>
cpus<span class="token punctuation">:</span> <span class="token number">4</span>
provider<span class="token punctuation">:</span> virtualbox
mariadb<span class="token punctuation">:</span> <span class="token boolean">true</span></code></pre><p><a name="daily-usage"></a></p><h2><a href="#daily-usage">常见用法</a></h2><p><a name="accessing-homestead-globally"></a></p><h3>全局使用</h3><p>如果你希望在文件系统的任何地方都可以 <code class=" language-php">vagrant up</code> 开启 Homestead 虚拟机,你可以把以下代码放到你的 Bash profile 里面,这个函数允许你在文件系统的任何位置都可以对 Homestead 运行 Vagrant 命令:</p><h4>Mac / Linux</h4><pre class=" language-php"><code class=" language-php"><span class="token keyword">function</span> <span class="token function">homestead<span class="token punctuation">(</span></span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token punctuation">(</span> cd <span class="token operator">~</span><span class="token operator">/</span>Homestead <span class="token operator">&&</span> vagrant $<span class="token operator">*</span> <span class="token punctuation">)</span>
<span class="token punctuation">}</span></code></pre><p>请将 <code class=" language-php"><span class="token operator">~</span><span class="token operator">/</span>Homestead</code> 这个路径修改为你的实际 Homestead 的安装路径,一旦这个函数安装成功,就可以在系统的任意位置运行 <code class=" language-php">homestead up</code> 或 <code class=" language-php">homestead ssh</code> 命令</p><h4>Windows</h4><p>在系统的任意位置创建一个批处理文件 <code class=" language-php">homestead<span class="token punctuation">.</span>bat</code> ,并添加如下内容:</p><pre class=" language-php"><code class=" language-php">@<span class="token keyword">echo</span> off
set cwd<span class="token operator">=</span><span class="token operator">%</span>cd<span class="token operator">%</span>
set homesteadVagrant<span class="token operator">=</span>C<span class="token punctuation">:</span>\<span class="token package">Homestead</span>
cd <span class="token operator">/</span>d <span class="token operator">%</span>homesteadVagrant<span class="token operator">%</span> <span class="token operator">&&</span> vagrant <span class="token operator">%</span><span class="token operator">*</span>
cd <span class="token operator">/</span>d <span class="token operator">%</span>cwd<span class="token operator">%</span>
set cwd<span class="token operator">=</span>
set homesteadVagrant<span class="token operator">=</span></code></pre><p>请将 <code class=" language-php">C<span class="token punctuation">:</span>\<span class="token package">Homestead</span></code> 这个路径修改为你的实际 Homestead 的安装路径,创建完这个文件后,将这个文件路径添加到 <code class=" language-php"><span class="token constant">PATH</span></code> 环境变量中,就可以在系统的任意位置运行 <code class=" language-php">homestead up</code> 或 <code class=" language-php">homestead ssh</code> 命令</p><p><a name="connecting-via-ssh"></a></p><h3>通过SSH连接</h3><p>在 Homestead 目录运行 <code class=" language-php">vagrant ssh</code> 命令来连接虚拟主机。</p><p>你可能会经常需要使用 SSH 来连接 Homestead 主机,你可以考虑将上述「function」添加到你的主机,以便快速的通过 SSH 进入你的 Homestead box</p><p><a name="connecting-to-databases"></a></p><h3>连接数据库</h3><p>在 box 中已经为 MySQL 和 Postgres 配置好了一个开箱即用的数据库 <code class=" language-php">homestead</code> ,为了更方便的使用它,Laravel 中的 <code class=" language-php"><span class="token punctuation">.</span>env</code> 文件将这个数据库设置成了框架默认使用的数据库。</p><p>如果想要从你主机上的数据库客户端连接 MySQL 或 Postgres,可以通过 <code class=" language-php"><span class="token number">127.0</span><span class="token punctuation">.</span><span class="token number">0.1</span></code> 来使用端口 <code class=" language-php"><span class="token number">33060</span></code>(MySQL) 或 <code class=" language-php"><span class="token number">54320</span></code>(Postgres) 连接。账号密码分别是 <code class=" language-php">homestead</code> / <code class=" language-php">secret</code></p><blockquote class="has-icon note"><p><div class="flag"><span class="svg"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:a="http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/" version="1.1" x="0px" y="0px" width="90px" height="90px" viewBox="0 0 90 90" enable-background="new 0 0 90 90" xml:space="preserve"><path fill="#FFFFFF" d="M45 0C20.1 0 0 20.1 0 45s20.1 45 45 45 45-20.1 45-45S69.9 0 45 0zM45 74.5c-3.6 0-6.5-2.9-6.5-6.5s2.9-6.5 6.5-6.5 6.5 2.9 6.5 6.5S48.6 74.5 45 74.5zM52.1 23.9l-2.5 29.6c0 2.5-2.1 4.6-4.6 4.6 -2.5 0-4.6-2.1-4.6-4.6l-2.5-29.6c-0.1-0.4-0.1-0.7-0.1-1.1 0-4 3.2-7.2 7.2-7.2 4 0 7.2 3.2 7.2 7.2C52.2 23.1 52.2 23.5 52.1 23.9z"></path></svg></span></div> 因为虚拟机做了端口转发,所以本机电脑上你应当只使用这些非标准的连接端口,虚拟机里依然使用默认的 3306 及 5432 连接端口。</p></blockquote><p><a name="adding-additional-sites"></a></p><h3>增加更多网站</h3><p>一旦 Homestead 环境配置完毕且成功运行后,你可能会想要为 Laravel 应用程序增加更多的 Nginx 网站。你可以在单个 Homestead 环境中运行多个 Laravel 程序。要添加额外的网站,只需将网站添加到您的 <code class=" language-php">Homestead<span class="token punctuation">.</span>yaml</code> 文件中:</p><pre class=" language-php"><code class=" language-php"> sites<span class="token punctuation">:</span>
<span class="token operator">-</span> map<span class="token punctuation">:</span> homestead<span class="token punctuation">.</span>app
to<span class="token punctuation">:</span> <span class="token operator">/</span>home<span class="token operator">/</span>vagrant<span class="token operator">/</span>Code<span class="token operator">/</span>Laravel<span class="token operator">/</span><span class="token keyword">public</span>
<span class="token operator">-</span> map<span class="token punctuation">:</span> another<span class="token punctuation">.</span>app
to<span class="token punctuation">:</span> <span class="token operator">/</span>home<span class="token operator">/</span>vagrant<span class="token operator">/</span>Code<span class="token operator">/</span>another<span class="token operator">/</span><span class="token keyword">public</span></code></pre><p>如果 Vagrant 没有自动管理你的「hosts」文件,你可能需要手动把新增的站点加入到「hosts」文件中:</p><pre class=" language-php"><code class=" language-php"><span class="token number">192.168</span><span class="token punctuation">.</span><span class="token number">10.10</span> homestead<span class="token punctuation">.</span>app
<span class="token number">192.168</span><span class="token punctuation">.</span><span class="token number">10.10</span> another<span class="token punctuation">.</span>app</code></pre><p>当你的网站添加完成,从你的 Homestead 目录运行 <code class=" language-php">vagrant reload <span class="token operator">--</span>provision</code> 命令就可以应用新的更改。</p><p><a name="site-types"></a></p><h4>Site Types</h4><p>Homestead supports several types of sites which allow you to easily run projects that are not based on Laravel. For example, we may easily add a Symfony application to Homestead using the <code class=" language-php">symfony2</code> site type:</p><pre class=" language-php"><code class=" language-php">sites<span class="token punctuation">:</span>
<span class="token operator">-</span> map<span class="token punctuation">:</span> symfony2<span class="token punctuation">.</span>app
to<span class="token punctuation">:</span> <span class="token operator">/</span>home<span class="token operator">/</span>vagrant<span class="token operator">/</span>Code<span class="token operator">/</span>Symfony<span class="token operator">/</span><span class="token keyword">public</span>
type<span class="token punctuation">:</span> symfony2</code></pre><p>The available site types are: <code class=" language-php">apache</code>, <code class=" language-php">laravel</code> (the default), <code class=" language-php">proxy</code>, <code class=" language-php">silverstripe</code>, <code class=" language-php">statamic</code>, and <code class=" language-php">symfony2</code>.</p><p><a name="configuring-cron-schedules"></a></p><h3>配置 Cron 调度器</h3><p>Laravel 提供了便利的方式来 <a href="/docs/5.4/scheduling">调度 Cron 任务</a> ,通过 <code class=" language-php">schedule<span class="token punctuation">:</span>run</code> Artisan 命令,调度便会在每分钟被运行。 <code class=" language-php">schedule<span class="token punctuation">:</span>run</code> 命令会检查定义在你 <code class=" language-php">App\<span class="token package">Console<span class="token punctuation">\</span>Kernel</span></code> 类中调度的任务,判断哪个任务该被运行。</p><p>如果你想为 Homestead 网站使用 <code class=" language-php">schedule<span class="token punctuation">:</span>run</code> 命令,你可以在定义网站时将 <code class=" language-php">schedule</code> 选项设置为 <code class=" language-php"><span class="token boolean">true</span></code></p><pre class=" language-php"><code class=" language-php">sites<span class="token punctuation">:</span>
<span class="token operator">-</span> map<span class="token punctuation">:</span> homestead<span class="token punctuation">.</span>app
to<span class="token punctuation">:</span> <span class="token operator">/</span>home<span class="token operator">/</span>vagrant<span class="token operator">/</span>Code<span class="token operator">/</span>Laravel<span class="token operator">/</span><span class="token keyword">public</span>
schedule<span class="token punctuation">:</span> <span class="token boolean">true</span></code></pre><p>该网站的 Cron 任务会被定义在虚拟机的 <code class=" language-php"><span class="token operator">/</span>etc<span class="token operator">/</span>cron<span class="token punctuation">.</span>d</code> 文件夹中。</p><p><a name="ports"></a></p><h3>连接端口</h3><p>以下本地电脑连接端口将会被转发至 Homestead 环境:</p><ul><li><strong>SSH:</strong> 2222 → Forwards To 22</li><li><strong>HTTP:</strong> 8000 → Forwards To 80</li><li><strong>HTTPS:</strong> 44300 → Forwards To 443</li><li><strong>MySQL:</strong> 33060 → Forwards To 3306</li><li><strong>Postgres:</strong> 54320 → Forwards To 5432</li><li><strong>Mailhog:</strong> 8025 → Forwards To 8025</li></ul><h4>转发更多的端口</h4><p>如果你需要的话,也可以借助指定连接端口的通信协议来转发更多额外的连接端口给 Vagrant box :</p><pre class=" language-php"><code class=" language-php">ports<span class="token punctuation">:</span>
<span class="token operator">-</span> send<span class="token punctuation">:</span> <span class="token number">93000</span>
to<span class="token punctuation">:</span> <span class="token number">9300</span>
<span class="token operator">-</span> send<span class="token punctuation">:</span> <span class="token number">7777</span>
to<span class="token punctuation">:</span> <span class="token number">777</span>
protocol<span class="token punctuation">:</span> udp</code></pre><p><a name="sharing-your-environment"></a></p><h3>共享你的环境</h3><p>有时候你想跟你的同事或者是客户共享你目前的工作进度。Vagrant 为此提供了一个内置方法 <code class=" language-php">vagrant share</code>;不过,如果你在你的 <code class=" language-php">Homestead<span class="token punctuation">.</span>yaml</code> 文件中配置了多个站点则这条命令将会变得没多大用处。</p><p>为了解决这个问题,Homestead 提供了自己的 <code class=" language-php">share</code> 命令。开始之前,通过 <code class=" language-php">vagrant ssh</code> 命令 SSH 进你的 Homestead 机器中,然后运行 <code class=" language-php">share homestead<span class="token punctuation">.</span>app</code>。这会从你的 <code class=" language-php">Homestead<span class="token punctuation">.</span>yaml</code> 配置文件中共享 <code class=" language-php">homestead<span class="token punctuation">.</span>app</code> 站点。当然,你也可以用其他已经配置的站点来代替 <code class=" language-php">homestead<span class="token punctuation">.</span>app</code>。</p><p>运行完命令之后,你可以看到一个包含活动日志和共享站点外网访问路径的 Ngrok 界面。</p><blockquote class="has-icon note"><p><div class="flag"><span class="svg"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:a="http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/" version="1.1" x="0px" y="0px" width="90px" height="90px" viewBox="0 0 90 90" enable-background="new 0 0 90 90" xml:space="preserve"><path fill="#FFFFFF" d="M45 0C20.1 0 0 20.1 0 45s20.1 45 45 45 45-20.1 45-45S69.9 0 45 0zM45 74.5c-3.6 0-6.5-2.9-6.5-6.5s2.9-6.5 6.5-6.5 6.5 2.9 6.5 6.5S48.6 74.5 45 74.5zM52.1 23.9l-2.5 29.6c0 2.5-2.1 4.6-4.6 4.6 -2.5 0-4.6-2.1-4.6-4.6l-2.5-29.6c-0.1-0.4-0.1-0.7-0.1-1.1 0-4 3.2-7.2 7.2-7.2 4 0 7.2 3.2 7.2 7.2C52.2 23.1 52.2 23.5 52.1 23.9z"></path></svg></span></div> 谨记,Vagrant 本质上是不安全的,当你运行 <code class=" language-php">share</code> 命令的时候,你会把你的虚拟机暴露在互联网中。</p></blockquote><p><a name="network-interfaces"></a></p><h2><a href="#network-interfaces">网络接口</a></h2><h2>Network Interfaces</h2><p><code class=" language-php">Homestead<span class="token punctuation">.</span>yaml</code> 文件里的 <code class=" language-php">networks</code> 配置项允许你为 Homestead 环境配置网络接口。你可以任意配置多个网络接口:</p><pre class=" language-php"><code class=" language-php">networks<span class="token punctuation">:</span>
<span class="token operator">-</span> type<span class="token punctuation">:</span> <span class="token string">"private_network"</span>
ip<span class="token punctuation">:</span> <span class="token string">"192.168.10.20"</span></code></pre><p>想要配置一个 <a href="https://www.vagrantup.com/docs/networking/public_network.html">桥接</a> 接口的话,增加 <code class=" language-php">bridge</code> 配置项,然后 <code class=" language-php">type</code> 填写为 <code class=" language-php">public_network</code> :</p><pre class=" language-php"><code class=" language-php">networks<span class="token punctuation">:</span>
<span class="token operator">-</span> type<span class="token punctuation">:</span> <span class="token string">"public_network"</span>
ip<span class="token punctuation">:</span> <span class="token string">"192.168.10.20"</span>
bridge<span class="token punctuation">:</span> <span class="token string">"en1: Wi-Fi (AirPort)"</span></code></pre><p>想要配置一个 <a href="https://www.vagrantup.com/docs/networking/public_network.html">DHCP</a> 接口的话,请从配置中移除 <code class=" language-php">ip</code> 选项:</p><pre class=" language-php"><code class=" language-php">networks<span class="token punctuation">:</span>
<span class="token operator">-</span> type<span class="token punctuation">:</span> <span class="token string">"public_network"</span>
bridge<span class="token punctuation">:</span> <span class="token string">"en1: Wi-Fi (AirPort)"</span></code></pre><p><a name="updating-homestead"></a></p><h2><a href="#updating-homestead">更新 Homestead</a></h2><p>你可以简单的用两个步骤来更新 Homestead ,第一步,使用 <code class=" language-php">vagrant box update</code> 命令更新 Vgrant box :</p><pre class=" language-php"><code class=" language-php">vagrant box update</code></pre><p>接下来。你需要更新 Homestead 的源代码,如果你是通过克隆仓库的方式安装的 Homestead ,你可以简单的运行 <code class=" language-php">git pull origin master</code> 命令在你最初克隆仓库的位置。</p><p>如果你已经通过你的项目中的 <code class=" language-php">composer<span class="token punctuation">.</span>json</code> 文件安装了 Homestead ,你应该确认你的 <code class=" language-php">composer<span class="token punctuation">.</span>json</code> 文件中是否包含 <code class=" language-php"><span class="token string">"laravel/homestead: "</span><span class="token operator">^</span><span class="token number">4</span>"</code> 并且更新你的依赖:</p><pre class=" language-php"><code class=" language-php">composer update</code></pre><p><a name="old-versions"></a></p><h2><a href="#old-versions">历史版本</a></h2><p>你可以通过添加以下配置到你的 <code class=" language-php">Homestead<span class="token punctuation">.</span>yaml</code> 文件来方便的覆盖 Homestead 使用的 box 版本</p><pre class=" language-php"><code class=" language-php">version<span class="token punctuation">:</span> <span class="token number">0.6</span><span class="token punctuation">.</span><span class="token number">0</span></code></pre><p>例如:</p><pre class=" language-php"><code class=" language-php">box<span class="token punctuation">:</span> laravel<span class="token operator">/</span>homestead
version<span class="token punctuation">:</span> <span class="token number">0.6</span><span class="token punctuation">.</span><span class="token number">0</span>
ip<span class="token punctuation">:</span> <span class="token string">"192.168.20.20"</span>
memory<span class="token punctuation">:</span> <span class="token number">2048</span>
cpus<span class="token punctuation">:</span> <span class="token number">4</span>
provider<span class="token punctuation">:</span> virtualbox</code></pre><p>当你使用旧版本的 box 时,你需要确保 Homestead 源代码的版本与之对应,下面的图表展示了支持的 box 版本,以及与之对应的 Homestead 的源代码版本和 box 所提供的 PHP 版本:</p><table><thead><tr><th></th><th>Homestead Version</th><th>Box Version</th></tr></thead><tbody><tr><td>PHP 7.0</td><td>3.1.0</td><td>0.6.0</td></tr><tr><td>PHP 7.1</td><td>4.0.0</td><td>1.0.0</td></tr></tbody></table><p><a name="provider-specific-settings"></a></p><h2><a href="#provider-specific-settings">Provider 的特殊设置</a></h2><p><a name="provider-specific-virtualbox"></a></p><h3>VirtualBox</h3><p>Homestead 默认将 <code class=" language-php">natdnshostresolver</code> 设置为 <code class=" language-php">on</code>。这允许 Homestead 使用你的主机系统中的 DNS 设置。如果你想重写这行为,你可以在你的 <code class=" language-php">Homestead<span class="token punctuation">.</span>yaml</code> 文件中添加下面这几行:</p><pre class=" language-php"><code class=" language-php">provider<span class="token punctuation">:</span> virtualbox
natdnshostresolver<span class="token punctuation">:</span> off</code></pre><h2>译者署名</h2><table><thead><tr><th>用户名</th><th>头像</th><th>职能</th><th>签名</th></tr></thead><tbody><tr><td><a href="https://laravel-china.org/users/1232">@lybc</a></td><td><img class="avatar-66 rm-style" src="https://dn-phphub.qbox.me/uploads/avatars/1232_1469434413.jpg?imageView2/1/w/100/h/100"></td><td>翻译</td><td>有用是有趣的前提</td></tr></tbody></table></article>
- 入门指南
- 安装
- 配置信息
- 文件夹结构
- 请求周期
- 开发环境部署
- Valet
- Homestead
- 核心概念
- 服务提供者
- Facades
- Contracts
- 服务容器
- HTTP 层
- 路由
- 中间件
- CSRF 保护
- 控制器
- 请求
- 响应
- 视图
- Session
- 表单验证
- 前端
- Blade 模板
- 本地化
- 前端指南
- 编辑资源 Mix
- 安全
- API 认证
- 用户认证
- 用户授权
- 加密解密
- 哈希
- 重置密码
- 数据库
- 快速入门
- 查询构造器
- 分页
- 数据库迁移
- Redis
- 数据填充
- Eloquent ORM
- Eloquent ORM快速入门
- 模型关联
- Eloquent 集合
- 修改器
- 序列化
- 综合话题
- Artisan 命令行
- 广播系统
- 缓存系统
- 集合
- 错误与日志
- 事件系统
- 文件存储
- 辅助函数
- 邮件发送
- 消息通知
- 扩展包开发
- 队列
- 任务调度