Extending Homestead: Building A Laravel Dev Environment on Mac OS X

This is an old post!

This post is over 2 years old. Solutions referenced in this article may no longer be valid. Please consider this when utilizing any information referenced here.

I’m a big fan of Laravel and I’ve really enjoyed building things on it. So I’m going to walk you through a quick lesson in getting a dev environment set up.

Setting up Local DNS

A good first place to start is setting up local DNS. In my case, I use the *.dev TLD for my local projects. Rather than reproduce all the steps, I will instead point you at this very good guide on using DNSMasq on OS X.

The one thing to note is that, when it mentions pointing your domain to 127.0.0.1, you instead need to point it to 192.168.10.10. This is the local address for the Homestead virtual machine.

address=/dev/192.168.10.10

Setting Up Homestead

Sites

One of the neat things about Laravel is Homestead, a virtual machine ready to go with all you need to get you up and going as quickly as possible. But, one of the things I don’t like about it is that you either have to have multiple virtual machines for each laravel project (which wastes space) or manually enter each one. So you end up with something like this.

sites:
    - map: homestead.app
      to: /home/vagrant/Development/homestead/public
    - map: laravel.app
      to: /home/vagrant/Development/laravel/public
    - map: llama.app
      to: /home/vagrant/Development/llama/public

This repetition is unnecessary. If you follow the same format and put all your Laravel projects in the same location, you can do something like this instead:

sites:
    - map: '~^(?<project>.+)\.dev$'
      to: /home/vagrant/Development/\$project/public

Now, going to llama.dev will direct you do the code in /home/vagrant/Development/llama/public. Easy!

Databases

But what about databases? Well there’s a solution for this too. In your homestead directory, edit the after.sh file like this:

#!/bin/bash

for file in /home/vagrant/Development/*/.env; do
    . $file;
    DBEXISTS=$(mysql -u root -p{password} --batch --skip-column-names -e "SHOW DATABASES LIKE '"$DB_DATABASE"';" | grep "$DB_DATABASE" > /dev/null; echo "$?")
    if [[ ! $DBEXISTS -eq 0 ]]; then
        mysqladmin -u root -p{password} create $DB_DATABASE;
        mysql -u root -p{password} -e "grant all privileges on $DB_DATABASE.* to $DB_USERNAME@'localhost' identified by '$DB_PASSWORD';";
    fi
done

This little script will read the .env file in each of your Laravel directories and, if the database doesn’t yet exist, it will create it. So any time you need to create a new project, just re-run vagrant provision.

Conclusions

Now, starting a new Laravel project is as easy as issuing just a few commands:

laravel new blog

Edit the .env as needed.

cd ~/Homestead;
vagrant provision

And you’re ready to start coding.

Comments (0)

Interested in why you can't leave comments on my blog? Read the article about why comments are uniquely terrible and need to die. If you are still interested in commenting on this article, feel free to reach out to me directly and/or share it on social media.

Contact Me
Share It
Apple
As I’ve mentioned a couple of times before, one of my projects right now is ripping all the DVDs I own so that I can watch them on my AppleTV (or any AppleTV in the house). Well, one of the problems I’ve run into a couple of times is longer movies that are distributed on two discs. This is usually movies like the Lord of the Rings Extended Edition or The Ten Commandments. Really, they’re one movie, but are distributed as two separate movies because of the restrictions of physical media. Well, digital media imposes no such restrictions on us, so why have two separate movies listed on the AppleTV? So after much trial and error, I finally discovered a way to get everything play nicely together. Unfortunately, this is not an easy problem to solve and even involved me writing a small script that could merge chapter files together because every single method I could find would eliminate chapter markers. So here, in abbreviated form, is the process for merging m4v files together and preserving chapter markers. Note: This tutorial assumes some level of technical proficiency. This is not a point-and-click process (yet :P) and requires the use of multiple tools and the shell. Tools you’ll need: Handbrake or whatever tool you’re using for ripping your legally obtained DVDs. MetaX and/or iDentify Subler remux Quicktime, which is now built into Mac OS X. chaptermerge, a script I wrote that merges chapter files together. The proces: Rip both movies from their individual DVDs using Handbrake or whatever other tool you’re using. Be sure that you’re adding chapter markers. Load each movie into MetaX and download the chapter names. That’s really the only thing you need to add to the file. Save the files with chapter names. Load each movie into Subler and extract the chapter files. To do this, select the chapter track and select File -> Export. Now, open the first movie in Quicktime. Drag the second movie on top of the first one. Quicktime will add the two together. Save the movie for use on an AppleTV. Get a beer or 6, because this takes awhile. While the movie is saving, use chaptermerge to merge the chapter files together. See the docs on how it works. Once the file has finished saving as a Quicktime MOV (it’s actually still h.264 inside the file), fire up remux and convert the merged file back into an m4v. Drag the file into remux, set the output to m4v, and save. Should be pretty quick - a matter of minutes. Load the merged file back into Subler and add the merged chapter track. Drag the chapter file into the Subler window. Save the file. Load the merged file into a tool such as iDentify or MetaX and add the remaining metadata. That’s it! You now have a merged file with both parts of the movie, accurate chapter markers and full metadata, ready to be copied to iTunes and viewed on your AppleTV.
Read More
PHP
Let’s say you have a Laravel application that does some data processing, and you want to monitor a directory for incoming changes, that you can then process using queued jobs. There are a couple of ways you could do something like this. You could scan those directories on a schedule using a cronjob. It’s doable. But what happens if you want to monitor a few thousand directories for changes? You can use tools like incron. Also doable, but another dependency. But what if I told you you could do it all with PHP. And within Laravel, no less?
Read More
Linux
Here’s a quick article about how to make a multiboot USB stick under macOS. These are useful in a lot of situations - such as for doing system installs or system rescues - because you can boot a wide variety of live OSs from a single stick. There are a lot of guides out there for doing this on Linux, and a lot of software for automating it on Windows, but not a lot of guides for doing it on macOS. Fortunately, it is pretty straightforward as the instructions will be broadly similar to doing it on Linux.
Read More