Showing posts with label vagrant. Show all posts
Showing posts with label vagrant. Show all posts

April 4, 2015

Edit/Add to hosts file in Windows

On Mac or Linux, this is a cinch.
sudo vi /etc/hosts

On Windows, it sucks, and you will probably run into the "You cannot modify the Hosts file" error. Here's the workaround.

Navigate to Start Menu -> All Programs -> Accessories and right-click Notepad to run it as an administrator. You may be asked for your password at this point:

Navigate to the hosts file in Windows Explorer. It is located at C:\Windows\System32\drivers\etc\hosts. Right click it, and un-select the Read-Only attribute:

Now use Notepad to navigate to the hosts file. Be sure to view All Files!


Finally, change what you need to change. When done, do not just click File -> Save, but instead do File -> Save As... and overwrite the hosts file. You may again need to specify All Files as done above, else the hosts file you modified will just be saved as a .txt copy, which does nobody any good. Lastly, after saving, remember to make the hosts file Read-Only again.

March 19, 2015

Laravel 5 Environment setup

The .env file is a non-version-controlled file that can contain sensitive information such as settings and passwords. Laravel 5 comes with a .env.example file to get things started.

The way you use environment variables is by declaring them in the .env file, and then invoking them as such:
'key' => env('VALUE', 'default')
'host' => env('DB_HOST', 'localhost')

Additionally, further configuration can be set in files in the config folder. For example, all the database connection information is set up in the config/databases.php file. Everything in that folder is used to that extent, so be sure to have a peek there if you install any useful packages into your app.

Laravel error pages also change what they display based on the environment. For example, the APP_ENV variable in the .env file controls how your app interacts with the browser. If set to APP_ENV = local, there will be much more verbose error pages (useful to a developer), as opposed to APP_ENV = production, which will show only the more user-friendly "Whoops, looks like something went wrong." message! Pretty useful stuff.

February 20, 2015

The Laravel 5 Vagrant box: Homestead 2.0

It is possible that if you followed along with the last post, you might get some errors in starting up a Laravel 5 project. The error might depend on several factors such as OS or previous package dependencies, and manual intervention is probably required. This is why a virtual machine is a great way to manage development environments, particularly via Vagrant.

Homestead is Laravel's officially pre-packaged Vagrant box that is already set up with everything needed to run a Laravel project.

Virtual Machine setup


Install Vagrant
Install Virtualbox
Add the Homestead Virtualbox:
vagrant box add laravel/homestead

Homestead source files setup


Now, from here there are two ways of getting a box running. There is no need to do both.

I prefer cloning the Github repository, which is also the officially documented way at the time of this writing for Laravel 5.0. My reason is that if you "dirty" your Homestead repository, it really doesn't take a lot of effort to just remove the dirty/broken/outdated Homestead VM, reclone it afresh, and reimport your settings file. We will just clone the Github repo:


git clone https://github.com/laravel/homestead.git Homestead
cd Homestead
bash init.sh

This will create a Homestead.yaml file, placed in your ~/.homestead directory. This settings file can easily be re-initialized by running the init.sh script again, as above.

The alternate way is using Composer to globally install Homestead. This was previously the officially documented approach, at least back during Laravel 4.2. I am not sure I like this approach, but there are articles out there evangelizing the merits of doing this. I would rather modularize my development environments as done the first way, instead of globally invoking Homestead anywhere in my system (how often do you really need to create a new Laravel box, when a single box can capably manage all of your local Laravel projects? Capriciously, running homestead up is annoying when vagrant up is already so ingrained!):


composer global require "laravel/homestead=~2.0"
homestead init

This will create a Homestead.yaml file, placed in your ~/.homestead directory.

SSH key setup


The ~/.homestead/Homestead.yaml file already sets a sane default location for your SSH key; if yours is different then update it. See here to create a key from scratch if it doesn't yet exist.

Shared folders/sites setup


In the ~/.homestead/Homestead.yaml file, map out the path you want access to in your Homestead VM. I keep all my projects and VMs (including this Homestead VM) in a Projects folder, so let's update the `folders` variable using relative paths, and also the sites (of which you can have as many as you want) with the specific Laravel projects you want to access:


folders:
    - map: ../../Projects
      to: /home/vagrant/Projects

sites:
    - map: mylaravel5project.dev
      to: /home/vagrant/Projects/mylaravel5project/public

Hosts file setup


If you want to access your project in the browser with a pretty name instead of Homestead's default IP, http://192.168.10.10/, be sure to define some hosts for easier access.

Alias setup


To add Bash aliases to your Homestead box, simply add to the aliases file in the root of the ~/.homestead directory.

Create a Laravel Project


composer create-project laravel/laravel projectname

Launch!


Simply run:
vagrant up
To destroy the VM and start fresh, run:
vagrant destroy --force
To SSH into Homestead when it is running, from the Homestead cloned directory, run:
vagrant ssh

Now we are set to visit http://192.168.10.10/ and will see the first mapped Laravel project's main page. If several apps were specified in the Homestead.yaml file's sites, and hosts file definitions were set for all of them, you can access each of them individually via those mapped URLs (i.e. mylaravel5project.dev). Enjoy!

October 29, 2014

First introduction into Laravel 4 PHP Homestead, getting started from scratch

Update: This post is now deprecated with the release of Homestead 2.0, which runs Laravel 5. A new post is here, and this one will be kept for historical reference.



I've been enjoying using the Laravel PHP framework over the past year or so, but I always found myself referring back to documentation when I start up a new local project. There are always changed dependencies, and lots of other miscellaneous changes, and I would need to update my Vagrantfile and provisioner software accordingly. This Laravel-Vagrant blog post by culttt was a staple of information for me for a long time, but as of the time of this posting, has been deprecated for a while. Luckily for us, there is a new kid on the block, and this one is officially supported and documented!

Enter Homestead, a pre-packaged Vagrant box with all the dependencies provisioned for, to run a new Laravel project. This was a breeze to get going even on my Windows machine (and I prefer using Cygwin with a Console2 shell as well, which usually requires extra effort finding dependencies than Ubuntu's apt-get or OSX's homebrew), and everything simply worked. Just be sure to have the latest version of Vagrant and Virtualbox installed on your computer!

Let's add the Homestead Virtualbox:

vagrant box add laravel/homestead

If you have an older version of Vagrant, you will likely get the error below:

This command was not invoked properly. The help for this command is
available below.

Usage: vagrant box add <name> <url> [--provider provider] [-h]

        --checksum VALUE             Checksum
        --checksum-type VALUE        Checksum type
    -c, --clean                      Remove old temporary download if it exists.
    -f, --force                      Overwrite an existing box if it exists.
        --insecure                   If set, SSL certs will not be validated.
        --cacert certfile            CA certificate
        --cert certfile              The client SSL cert
        --provider provider          The provider that backs the box.
    -h, --help                       Print this help

To fix it, I simply upgraded from the failing version 1.4.3, to the most up-to-date version 1.6.3 as of this writing. Just download the newest installer for Vagrant and set it up, else via Unix command line set it up for a global install as below (substituting for whatever version is most current for you), and then re-run the box add command from before:

wget https://dl.bintray.com/mitchellh/vagrant/vagrant_1.6.3_x86_64.deb
sudo dpkg -i vagrant_1.6.3_x86_64.deb

Continue by cloning the official Homestead repository into your folder of choice:

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

Now, in the Homestead.yaml file, you need to set your SSH key info. Chances are, if you've ever used SSH everything is already good to go (it comes with some sane defaults), but if not, check out the documentation on how to do that. Folder mapping is assigned to nginx here as well. If you already have a Laravel project you wish to use, you can just tweak the default folders map, as cloned from the repo, but since I am starting from scratch, I had to do this:

folders:
    - map: ../Homestead
      to: /home/vagrant/Homestead
sites:
    - map: homestead.app
      to: /home/vagrant/Homestead/laravelAuth/public

I'll be making a new Laravel authentication app once this is all set up, so I'm gonna call my project laravelAuth, and preemptively set up the public sites map for it, as done above. Note that you can map several different apps in this development environment.

From here, it is simply vagrant up, and in a short while, you'll have a working Laravel dev environment! In the Homestead.yaml file, our box's ip address is set as 192.168.10.10 by default, so opening http://192.168.10.10/ in your browser should yield... an error? "No input file specified." ...what?

That is because we haven't actually created our Laravel project, so there is nothing for this development environment to show yet! This is easily remedied by entering your box via vagrant ssh, and running:

cd /vagrant
composer create-project laravel/laravel laravelAuth --prefer-dist

Reload your page, and now you have arrived at a brand spanking new Laravel home page. Code away!