How to setup Laravel Homestead in Windows
How to setup Laravel Homestead in
Windows
Developing
with PHP under Windows can be a real pain. Sure there are applications like WAMP
or XAMPP that include the stack you need, but in the end you are not emulating
the environment where your live application is very likely to run:
Linux.
Laravel
Homestead is a great tool to setup your PHP development environment, but it can
be a little confusing to configure and slow in Windows. But fear not, in this
tutorial you’ll learn how to setup Laravel homestead for your PHP
projects.
- Installing the software
 - Adjusting VirtualBox and Vagrant
 - Getting the Homestead image
 - Getting the Homestead repository
 - Creating a SSH key
 - Configuring folders and sites
 - Booting the virtual machine
 - Connecting to the virtual machine
 - Connecting to MySQL with HeidiSQL
 - Adding a new Laravel site
 - Speeding things up
 
Installing the software
The
first thing to do is download and install the following software:
- VirtualBox for Windows 5.1.14 –
this is what manages and helps emulate a guest OS (Linux, in our case) inside a
host OS (Windows). I recommend this particular version because newer ones would
have problems with folder synchronization. - Vagrant installer for Windows –
this is a command line tool that runs on top of virtualization software such as
VirtualBox or VMWare. It also gives us some other nice features like file sync
between our guest and host OS. - Git for
Windows – we will
need this so we can clone the Laravel Homestead repository from Github, but more
important, it comes with a terminal emulator which will come in
handy. 
Vagrant depends on it.
Adjusting VirtualBox and Vagrant
The
next step is to make some tweaks to VirtualBox and Vagrant. We need to do this
because, by default, both tools store data in the same drive where they were
installed (tipically the C drive). The thing with virtual machines is they can
take up a lot of space, so they can eat up your main drive’s storage very
quickly.
First
we’ll change the storage path in Virtual Box, since it’s pretty straightforward.
You now should have a program called Oracle
VM VirtualBox installed. Open it and then go
to File >
Preferences. A new dialog will open where you can change the folder
next to the option that reads Default
Machine Folder. Click the dropdown and choose Other…. Here you can choose the new
folder for VirtualBox to store data. For example, I have a partition called D
and I chose a path on that drive.

Now
we’ll do the same for Vagrant. By default the path where data related to virtual
machines is stored is C:\Users\YourUser\.vagrant.d so I
recommend changing it to a different drive. This can be done with environment
variables. We need to create a variable called VAGRANT_HOME and point it to our desired location.
Open
the control panel and search for the word environment.
From the results choose the one that says edit
environment variables for your account.

On
the new dialog that appears, click the New… button.

Here
you will create the new variable. Enter VAGRANT_HOME as the value for the field
named Variable
name. For the field named Variable
value enter the path of your choice. In my case I
used the path D:\VM\Vagrant

Click OK in
both New User
Variable and Environment
Variables dialogs to save changes.
Getting the Homestead image
Ok,
so now we have the basic blocks for our setup, so what role Homestead plays
then? Well, VirtualBox and Vagrant allows us to emulate the environment, but we
can say these tools only manage and run the virtual machine. We still need an
image of the machine and something that installs the required tools so that we
don’t have to it manually. That’s where Homestead comes in!
First
we need to download the OS for the guest machine. Laravel homestead already has
an image for this. The latest version at this time comes with Ubuntu 16.04. To
install it open Git Bash (the terminal emulator that comes with Git for Windows)
and execute the following command:
| 
 1 
 | 
 vagrant box add laravel/homestead 
 | 
This
will show the following message asking you to choose your virtualization
provider. Enter the number that corresponds to VirtualBox and press
Enter:

The
OS will start downloading. This can take some minutes depending on your internet
connection. Once it finishes downloading a message will appear saying the box
was successfully added.

Getting the Homestead repository
So
we have the guest OS, but it doesn’t have PHP or a webserver installed. For that
we need the Homestead repository, which has the config files and scripts that
take care of those things.
Open Git Bash and cd into
a directory of your choice (tipically your home directory) and clone the
Homestead repository:
| 
 1 
2 
3 
 | 
 cd ~ 
git clone https://github.com/laravel/homestead.git Homestead 
 | 
Now
cd into the newly created Homestead directory and initialize the homestead
config:
| 
 1 
2 
3 
 | 
 cd Homestead 
bash init.sh 
 | 
This
will create a configuration file called Homestead.yaml in this directory. We’ll get back to this file
shortly.
Creating a SSH key
Thanks
to Vagrant folder synchronization we’ll be able to edit our project files in the
host machine (Windows) and they will be reflected into the guest machine. But
for other tasks such as running migrations and other commands it will be
necessary to log into the guest via the terminal. For this we’ll need a SSH key.
If you already have one or more you can skip this step.
Once
again, Git
Bash will ease things for us in Windows. Instead of
using a program to create a key and another one to connect to the guest, we can
do all that with this terminal emulator.
To
create a new SSH key enter the following command:
| 
 1 
 | 
 ssh-keygen -t rsa -b 4096 -C "your_email@example.com" 
 | 
A
message like this will appear
| 
 1 
 | 
 Generating public/private rsa key pair. 
 | 
And
then a prompt for a path to store the new key. Simply hit Enter:
| 
 1 
 | 
 Enter a file in which to save the key (/c/Users/you/.ssh/id_rsa): 
 | 
Then
you will be asked for a passphrase twice. This is recommended for security
purposes, but for local development you can leave it empty and just hit Enter
both times:
| 
 1 
2 
 | 
 Enter passphrase (empty for no passphrase):  
Enter same passphrase again: 
 | 
Configuring folders and sites
You
should have now a file called Homestead.yaml inside your Homestead folder. This file should like
this:
| 
 1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
 | 
 --- 
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 
# blackfire: 
#     - id: foo 
#       token: bar 
#       client-id: foo 
#       client-token: bar 
# ports: 
#     - send: 50000 
#       to: 5000 
#     - send: 7777 
#       to: 777 
#       protocol: udp 
 | 
Let’s
see what these settings mean:
- ip: the
local ip address the virtual machine will respond to. This ip should be used in
your hosts file since sites configured in Homestead are served from the virtual
machine, instead from the host machine loopback ip (127.0.0.1). - memory:
amount of RAM in megabytes available for the virtual machine. By default set to
2 GB. - cpus:
number of processors on the virtual machine. - provider: the virtualization system to use. By default
configured already for VirtualBox. - authorize: location of our SSH public key. The tilde (~)
shortcut to point to our user’s home folder is recognized here as
well. - keys:
here we specify the path of the private SSH key. Tipically the same name as the
public key but without the.pubextension. - folders:
this is where we specify which folders in Windows should be synchronized with
the virtual machine.- mapis tipically the folder where you have or will have
your application code; Homestead by default points to a folder
calledCodein your home directory, but you can change it if you
need to. The important thing here is that this folder won’t be created for you,
so if this will be a new folder, you have to create it first.tois
the path where the contents of- mapwill be copied to. This folder will be created
automatically in the virtual machine. - sites:
this is where you will map the domains of each application you want to run with
Homestead. Each site should have its own- mapandtooptions where- mapshould contain the name of the domain
andtoshould point to the site’s root folder in the virtual
machine. By default, Homestead has a domain namedhomestead.appthat points to/home/vagrant/Code/Laravel/public. An important thing to note is that the root of your
sites must be a sub-folder of the path you configured in thefolderssection. For example, if you configured a path in the
virtual machine at/home/vagrant/Code,
your sites’ root must be inside/home/vagrant/Code - databases: the list of MySQL databases you want Homestead to
create when it boots. 
This
is an example of how the folders, sites and databases sections of our configuration file may look:
| 
 1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
 | 
 folders: 
    - map: ~/Projects 
      to: /home/vagrant/Code 
sites: 
    - map: onesite.dev 
      to: /home/vagrant/Code/onesite.dev/public 
    - map: anothersite.com 
      to: /home/vagrant/Code/anothersite/public 
databases: 
    - one_db 
    - another_db 
 | 
In
this case, the code in the host Windows machine is in a folder
called Projects which is located at the user’s home folder.
The Projects folder contents will be synchronized to the virtual
machine to a folder located at /home/vagrant/Code.
We have two sites: onesite.dev and anothersite.com;
we can see they both point to a subfolder within /home/vagrant/Code/.
We have also listed two databases to create: one_db and another_db.
Booting the virtual machine
To
boot the virtual machine, cd into the Homestead folder and execute the following
command:
| 
 1 
 | 
 vagrant up 
 | 
The
virtual machine will start booting and configuring. This should take some
seconds.

Connecting to the virtual machine
Once
the virtual machine has booted you can ssh into by simply running the following
inside your Homestead folder:
| 
 1 
 | 
 vagrant ssh 
 | 
You
should be shown something like the following, which means you are now inside the
virtual machine:

From
here you can for example connect to MySQL to manage your databases. The user for
the MySQL installation is homestead and the password is secret.

Another
way of managing databases is using a GUI application. I’ll show you a nice app
for Windows called HeidiSQL and how to connect from Windows to the MySQL server
in Homestead.
If
you logged into MySQL in the terminal, enter the exit command to quit MySQL and then again to logout from the
virtual machine:

Connecting to MySQL with HeidiSQL
The
first thing to do is to get the installer of HeidiSQL.
Get it, install it and open the application. You should be presented with a
screen like this:

On
this screen we’ll configure the connection to MySQL. Click
the New button and follow
these steps:
- enter a
description for the connection - enter the
connection credentials- user:
homestead - password:
secret - port:
33060 
 - user:
 - click Save
 - click Open
 

A
new window like this will show, the list of databases will differ since I have
created some other ones.

Adding a new Laravel site
The
virtual machine is running and has the tools installed for us to work. We’ll now
setup a new Laravel project. First we need to add the site to
the sites section of the Homestead.yaml file. Let’s call this site mylaravelapp.dev and add a new database as well called mylaravelapp:
| 
 1 
2 
3 
4 
5 
6 
7 
8 
9 
 | 
 sites: 
    - map: homestead.app 
      to: /home/vagrant/Code/Laravel/public 
    - map: mylaravelapp.dev 
      to: /home/vagrant/Code/mylaravelapp/public 
databases: 
    - homestead 
    - mylaravelapp 
 | 
Now
we need to reload the virtual machine and pass a flag so that it “refreshes” the
list of sites and databases and can pick up our new added site. Execute this
command, make sure you are inside your Homestead directory:
| 
 1 
 | 
 vagrant reload --provision 
 | 
The
virtual machine will reboot and create the new database and add a new nginx site
called mylaravelapp.dev:

Once
the virtual machine has finished booting, ssh into the machine, go to the
shared Code folder and create a new Laravel project with
Composer:
| 
 1 
2 
3 
4 
5 
 | 
 vagrant ssh 
cd Code 
composer create-project laravel/laravel mylaravelapp 
 | 
This
will clone the Laravel repository and will begin installing Composer
dependencies. This will install the latest Laravel version, which at the time of
writing is 5.4:

Composer
may take a while depending on your internet connection. Once the installation
finishes we need to open our Windows hosts file located at C:\Windows\System32\drivers\etc\hosts and
add the following:
| 
 1 
 | 
 192.168.10.10 mylaravelapp.dev 
 | 
Administrator privileges. I open it with notepad but I
use Run as
Admnistrator. Also note that the
ip must match the one configured in the Homestead.yaml
file
Save
the hosts file, open your browser and go to http://mylaravelapp.dev.
You should be presented with the Laravel welcome page:

Speeding things up
Even
if the Laravel application is showing the welcome page, it’s very possible that
you notice the page doesn’t load as fast as in a local webserver. This is a
problem with Homestead that for some time we just had to deal with, the good
thing is there is a plugin for Vagrant that will allow us to use NFS (network
file system) on Windows (even when the Vagrant site says it’s simply not
supported).
To
install the plugin simply execute the following command in Git Bash:
| 
 1 
 | 
 vagrant plugin install vagrant-winnfsd 
 | 
The
plugin should take a couple of seconds to install.

Now
there’s only small change we need to do in Homestead.yaml to enable NFS. Add the following to
the folders section in the file:
| 
 1 
2 
3 
4 
5 
 | 
 folders: 
    - map: ~/Projects 
      to: /home/vagrant/Code 
      type: "nfs" 
      mount_options: ['nolock,vers=3,udp,noatime'] 
 | 
Finally,
re-provision the virtual machine so that it picks up the new options:
| 
 1 
 | 
 vagrant reload --provision 
 | 
And
that’s it, you should notice faster page loads now.
Notice
that when connecting to MySQL from HeidiSQL we used port 33060, this is because
Homestead redirects some of the ports. We did this when connecting from the host
machine, but any configuration in your .env file should use the standard port for MySQL 3306
because your application runs inside the virtual machine, the same goes for the
MySQL host, it would use the loopback ip 127.0.0.1.
And
that’s how we reach the end of the tutorial. I really hope you find it useful
for setting up a development environment for PHP.
How to setup Laravel Homestead in Windows的更多相关文章
- Windows上使用Vagrant打造Laravel Homestead可协同跨平台开发环境
		
1.简介 Laravel 致力于让整个 PHP 开发过程变得让人愉悦,包括本地开发环境,为此官方为我们提供了一整套本地开发环境 —— Laravel Homestead. Laravel Homest ...
 - 在 Windows 上进行 Laravel Homestead 安装、配置及测试
		
软件环境:在 Windows 7 64位 上基于 VirtualBox 5.2.12 + Vagrant 2.1.1 使用 Laravel Homestead. 1.准备 先下载VirtualBox- ...
 - laravel本地开发环境的安装及配置 - Windows:安装 Laravel Homestead 虚拟机
		
一.安装 VirtualBox-5.2.22-126460-Win.exe 和 vagrant_2.2.2_x86_64.msi(可视化安装包安装); 安装在D盘 二.导入 Homestead Vag ...
 - Speeding up Homestead on Windows Using NFS
		
Speeding up Homestead on Windows Using NFS Sep 07 2015 Homestead Laravel EDIT: I have another articl ...
 - Laravel Homestead 安装 使用教程详解!
		
1 Laravel Homestead 1 安装: 1 下载: http://www.vagrantup.com/downloads.html 1 配置: 1 1 测试: 1 1 ********** ...
 - Laravel Homestead安装笔记
		
引言: 最近开始学习laravel框架,了解到有个laravel homestead的box,开发起来非常方便快捷,于是就准备开始配置homestead虚拟开发环境了 什么是Homestead 要想学 ...
 - laravel homestead vagrant box安装使用,问题,及相关命令
		
Vagrant is a tool that manages oracle virtual boxes 1.本地下载https://atlas.hashicorp.com/laravel/boxes/ ...
 - 练习Laravel Homestead的安装
		
1 安装VirtualBox和Vagrant 在启动Homestead环境之前,你必须安装VirtualBox(https://www.virtualbox.org/wiki/Downloads)和V ...
 - Laravel Homestead的安装和使用(照搬)
		
原文:https://blog.csdn.net/woqianduo/article/details/81091154/ 1.简介 1.1.Homestead是什么 Laravel Homestead ...
 
随机推荐
- Tomcat启动 Unable to process Jar entry [javassist/XXXXXX.class]
			
例如: 03-Mar-2017 17:01:45.864 SEVERE [RMI TCP Connection(3)-127.0.0.1] org.apache.catalina.startup.Co ...
 - Zabbix 报警通知邮件和微信vim /etc/hosts
			
1安装 sendmail # yum -y install sendmail echo 'This is test mail'>body.txt mail -s 'Test mail' 3013 ...
 - Sql Server中执行计划的缓存机制
			
Sql查询过程 当执行一个Sql语句或者存储过程时, Sql Server的大致过程是 1. 对查询语句进行分析,将其生成逻辑单元,并进行基本的语法检查 2. 生成查询树(会将查询语句中所有操作转换为 ...
 - 遍历树节点(多层)的方法(java)
			
前序遍历,后序遍历,广度遍历,深度遍历,遍历一级节点.以及按钮如何响应点击事件. import java.awt.*; import java.awt.event.*; import java.uti ...
 - "废物利用"也抄袭——“完全”DIY"绘图仪"<三、上位机程序设计>
			
上位机的程序主要是解析图片和生成较好的代码,现在实现的功能有灰度打印,二值打印,轮廓打印,骨骼打印.当然,必不可少的是打印大小的控制.测试了一些图片,总体来说,打印速度依次加快,因为打印的内容依次减少 ...
 - Bootstrap-CL:标签
			
ylbtech-Bootstrap-CL:标签 1.返回顶部 1. Bootstrap 标签 本章将讲解 Bootstrap 标签.标签可用于计数.提示或页面上其他的标记显示.使用 class .la ...
 - 【转】httpservlet 文章
			
HttpServlet类 ellisonDon 2012-10-25 12:42 阅读:2015 评论:0 HttpServlet的功能 ellisonDon 2012-10-25 11:02 ...
 - MMO技能系统的同步机制分析
			
转自:http://www.gameres.com/729629.html 此篇文章基于之前文章介绍的技能系统,主要介绍了如何实现MMO中的技能系统的同步.阅读此文章之前,推荐首先阅读前一篇文章:一个 ...
 - 关系型数据库与Key-value型数据库Mongodb模式设计对比
			
MongoDb 相比于传统的 SQL 关系型数据库,最大的不同在于它们的模式设计( Schema Design )上的差别,正是由于这一层次的差别衍生出其它各方面的不同. 我们可以简单的认为关系型数据 ...
 - 【BZOJ】1756: Vijos1083 小白逛公园(线段树)
			
题目 传送门:QWQ 分析 线段树维护一下最大子序列 维护一下最大前缀 最大后缀 区间和 就ok了 好像只能用结构体..... 代码 #include <bits/stdc++.h> u ...