LARAVEL

INSTALLING LARAVEL HOMESTEAD ON WINDOWS


As stated in its documentation, Laravel strives to make the entire PHP development experience delightful, including your local development environment. 

Vagrant provides a simple, elegant way to manage and provision Virtual Machines – Vagrant creates and configures a lightweight, reproducible and portable development environment.

Now, Laravel Homestead is an official, pre-packaged Vagrant “box” that provides you a wonderful development environment without needing to install a web server, PHP and the like. Vagrant boxes are disposable so if you notice a defect in an already created box, you can destroy it quickly and create a new one.

Homestead runs on the Linux, Mac & Windows platforms. It comes with PHP 5.6, MySQL, Postgres, Redis, Memcached, Node, Nginx web server and some other good stuff to build cool Laravel applications.

Installing Homestead on all the platforms is a straightforward activity, although on Windows there may be some things to be done differently. These are the things this article hopes to make clear.

To get started, the first thing to do is to install The Homestead Vagrant Box. And to do this there must be VirtualBox/VM Ware and Vagrant already installed on your Windows machine. With those tools already installed, go ahead and run this command from your command prompt:

The command above downloads the Vagrant Box on the machine after you’ve selected the option of either VirtualBox/VM Ware. Alternatively you can download the box file (named hc-download) manually from Atlas  using your browser.

Now, the next step is to run this command:

Once you’ve successfully added the box, the next step is to install the homestead CLI tool. This is done using Composer.

When this is complete, if ~/.composer/vendor/bin is not in your system PATH before, add it to the PATH to enable the use of homestead as a command on your CLI.

Then go ahead and configure Homestead in your directory with

Next you’ll want to configure the project directory that you’ll share with the virtual machine. Doing so requires you to identify the location of your public SSH key, because key-based encryption is used to securely share this directory. If you don’t already have an SSH key with your Windows OS, this SiteGround tutorial offers a succinct set of steps.

The homestead init command creates a number of files in your directory, one of which is Homestead.yaml. In the Homestead.yaml file there are some sections that needs to be edited, one of them is the authorize line in the file. The field for authorize must follow the Windows convention. It will be filled with the location of the SSH key you generated earlier as:

Next is to modify the folders and sites lines of the Homestead.yaml file to set the location of the Laravel project.

This particular step tends to confuse most Homestead beginners, so pay close attention to the following description. The folders object’s map attribute identifies the location in which your Laravel project will be located. You’re free to change this to any location you please, keeping in mind for the purposes of this introduction the directory must be your Laravel project’s root directory (which is C:/Users/Homestead for this tutorial). The folders object’s to attribute identifies the location on the virtual machine that will mirror the contents of the directory defined by the map key, thereby making the contents of your local directory available to the virtual machine.

The sites object’s map attribute defines the domain name used to access the Laravel application via the browser. Change it to the name of the application. If the name of your app is laranaija, then it would be laranaija.app.

Finally, the sites object’s to attribute defines the Laravel project’s root web directory, which is /public by default. This isn’t just some contrived setting; not only is /public the directory you would need to configure when setting up a web server to serve a Laravel application, but /home/vagrant/MyProjects/Laravel/public is also the directory that Homestead’s nginx web server has been configured to use! This means that the path defined by the folders map attribute must contain a directory named Laravel, and inside that a directory named public. If you do not do this, you’ll receive the dreaded 404 error when attempting to access the application via your browser.

Let’s go through an example together.

Begin by setting the folders object’s map attribute to any path you please, likely somewhere within the directory where you tend to manage your various software projects. For instance, mine is currently set like this:

Next, create a directory named Laravel inside the directory identified by the map attribute, and within it create another directory named public. That means the sites section looks like this:

Create a file named index.php inside the public directory, then add this piece of code to it:

Save these changes, and then run the following command from your terminal:

You should see something come up like so:

 

Your Homestead virtual machine is up and running! Open a browser and navigate to the URL http://laravel.app and you should see Hello from Homestead! Congratulations!!! That means everything is in order and you can work on the project.

Note the use of the 8000 port in the URL. This is because the Homestead virtual machine forwards several key ports to non-standard port numbers, allowing you to continue using the standard ports locally. I’ve included the list of forwarded ports in the debug output that followed the vagrant up command. As you can see, port 80 (for HTTP) forwards to 8000, port 3306 (for MySQL) forwards to 33060, port 5432 (for PostgreSQL) forwards to 54321, and port 22 (for SSH) forwards to 2222.

If the virtual machine did not start, or if you do not see Hello from Homestead! when accessing http://homestead.app, then double-check your Homestead.yaml file, ensuring all of the paths are properly set.

Incidentally, if you’d like to shut down the virtual machine you can do so using the following command:

Thanks, please let me know if you have any challenges in the comment section