Developing Laravel Applications on a FreeBSD Vagrant Box

2017-07-03

# What is this?

This is the installation guide for my custom development environment for Laravel applications on FreeBSD. The resulting Vagrant box has the following features:

• FreeBSD as a host!
• Specifying PHP environment on a per-project basis (v5.6 and v7.0 supported).
• MySQL server with easy to create databases.
• Windows support

If that sounds appealing to you keep reading. In the examples below I have three default "applications" that will be used as stand-ins for your application.

Tested operating systems:

• Mac OS 10.12
• Windows 10 64 bit

# Installation Instructions

1. You'll want to install the following on your system:
1. Clone this repository (same for Windows provided you have git. Otherwise download the zip file from the releases page and extract it wherever you please):
git clone "https://github.com/marblenix/Homestead-on-FreeBSD"
cd Homestead-on-FreeBSD
1. Copy the Vagrant.yaml file to it's proper location and modify it to suit your needs:
cp Vagrant.yaml ~/.config/Vagrant.yaml
$EDITOR ~/.config/Vagrant.yaml Windows users do the following: • Hold down the Windows key and press 'r'. • Type cmd.exe and press enter. • Run the following commands in the new terminal (change line 1 to be the path you downloaded/extracted the Homestead-on-FreeBSD project): cd %HOMEDRIVE%%HOMEPATH%\path\to\the\folder\Homestead-on-FreeBSD mkdir %HOMEDRIVE%%HOMEPATH%\.config copy Vagrant-Windows.yaml %HOMEDRIVE%%HOMEPATH%\.config\Vagrant.yaml notepad.exe %HOMEDRIVE%%HOMEPATH%\.config\Vagrant.yaml While I did keep the .yaml options as close as possible to the official Homestead release, only a subset of the options available were implemented. 1. OPTIONAL: Make sure you have a ssh key in ~/.ssh/id_rsa. • Windows users: run vagrant ssh from the project folder and use those settings for PuTTY. You'll need to convert the private_key to the ppk format first using PuTTYGen. • This is required if you want to use the applications I included in the box such as composer, grunt, npm, and scss-lint. If you have these on your host machine and prefer to run those commands there feel free to skip this step. 2. Change anything you need in the Config/Includes folder. This is a list of apache24 config files that will be added to new box. You can add any modules you might need to Config/Includes/global.conf. I've included example configuration files for both a-simple-blog and cool-crm applications that has a separate Laravel application for the admin side. This must be done before the initial vagrant up. If you have already initialized the environment you'll need to run vagrant provision in order for your changes to be reflected. Windows users: The first time you run vagrant up Vagrant will complain that FreeBSD doesn't have the capability to mount SMB shares. I have worked around this for the time being in the Vagrantfile. You can safely ignore this and manually run vagrant provision after the first vagrant up. ## Host Machine Changes There are a number of changes you should consider making to your host machine to make life a bit more tolerable. 1. Modify /etc/hosts (C:\Windows\System32\Drivers\etc\hosts for Windows) to include the hostnames of your application(s). This should be the same hostnames you set for the Config/Includes/*.conf files. To continue with the example applications here is what the hosts file should contain: 192.168.10.10 a-simple-blog.app 192.168.10.10 cool-crm.app You'll notice I didn't add cool-crm-admin.app. That's because it's going to piggy-back off the cool-crm.app hostname, much like a real-world application would. 1. Note for Mac OS users: Consider adding the following commands to your sudoers file. This will allow you to run vagrant up multiple times without entering your users password. Run sudo visudo and append the following to the end of the file: Cmnd_Alias VAGRANT_EXPORTS_ADD = /usr/bin/tee -a /etc/exports Cmnd_Alias VAGRANT_NFSD = /sbin/nfsd restart Cmnd_Alias VAGRANT_EXPORTS_REMOVE = /usr/bin/sed -E -e /*/ d -ibak /etc/exports %admin ALL=(root) NOPASSWD: VAGRANT_EXPORTS_ADD, VAGRANT_NFSD, VAGRANT_EXPORTS_REMOVE See the Vagrant docs for more information. This requires that you be apart of the admin group. You can check which groups you belong to with this command: dscl . search /Groups GroupMembership$(whoami).
If you are not apart of the admin group you can change the last line to say %AGroupYouBelongTo instead of %admin.

## Start 'er Up

1. Run vagrant up (should take ~15 minutes)
2. Run vagrant reload (should take ~1 minute).
3. Double check /etc/exports (Network > MachineName on Windows) to make sure that Vagrant properly created the shares you declared in ~/.config/Vagrant.yaml.

4. You should now be able to connect to any SQL databases you've setup in the ~/.config/Vagrant.yaml file. Point your favorite SQL application at 192.168.10.10 with the username and password you setup and you should be able to connect and restore from a backup.

5. Clear up any composer mischief. Get into your new box with vagrant ssh. You'll see that you have your projects waiting for you in the \$HOME directory. Run the following commands to help clear out any cached objects.

vagrant ssh
cd cool-crm
composer install
php artisan clear-compiled
php artisan optimize

Repeat the above for each project directory you have. I have also included npm, grunt, and scss-lint in the box for you to be able to build your static assets.

You should now be able to reach a-simple-blog.app, cool-crm.app, and cool-crm.app/admin from a browser. For this example I just had all project folders include a simple index.php file containing only phpinfo();.

tree Projects/
Projects/
├── a-simple-blog
│   └── public
│       └── index.php
├── cool-crm
│   └── public
│       └── index.php
└── index.php