Skip to content

Instantly share code, notes, and snippets.

@dccampbell
Created February 28, 2020 16:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dccampbell/55feea0943dc526309d746a24d594ac4 to your computer and use it in GitHub Desktop.
Save dccampbell/55feea0943dc526309d746a24d594ac4 to your computer and use it in GitHub Desktop.
Local Dev Environments for PHP Projects w/ Homestead
APP_URL=http://www.myproject.test
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=myprojectdb
DB_USERNAME=homestead
DB_PASSWORD=secret
MAIL_DRIVER=smtp
MAIL_HOST=localhost
MAIL_PORT=1025
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
/*.log
/.vagrant
/aliases
/Homestead.json
/Homestead.yaml
/user-customizations.sh
/Vagrantfile

Local Development

Laravel Homestead is used as the local development environment.

Local Setup Summary

  1. Install PHP and Composer
  2. Install Virtualbox (6.0+), Vagrant (2.2.7+), and vagrant-hostsupdater
  3. Execute composer run local and vagrant up

Hosts File

The Hostsupdater plugin for Vagrant is recommended to avoid having to manually manage your hosts file.

vagrant plugin install vagrant-hostsupdater

To also avoid having to type your password every time you vagrant up, see the plugin docs linked above for instructions on adding a sudoers file.

Alternatively, you can manually edit your system's hosts file to map each site listed in Homestead.yaml to the IP address also specified there.

Configuration

.env, Homestead.yaml, Vagrantfile are generated automatically and are ignored by git, so you can safely make changes specific to your local environment in those files. You can also create user-customizations.sh to add additional provision commands which will run following after.sh.

.env.example and Homestead.yaml.example are copied by composer run local.

Access

  • Each domain in Homestead.yaml should be accessible via browser when vagrant is running.
  • Mailhog can be accessed via a domain or localhost on port 8025.
#!/bin/bash
source /vagrant/aliases
# set default PHP version
php72
# goto project dir
cd /home/vagrant/code
# build frontend
npm ci --no-progress
npm run dev
{
"require-dev": {
"laravel/homestead": "^10.0"
},
"scripts": {
"local": [
"@php -r \"file_exists('.env') || copy('.env.example', '.env');\"",
"@composer install",
"@composer exec homestead make",
"# Ready to vagrant up!"
]
}
}
ip: 192.168.10.10
memory: 2048
cpus: 1
provider: virtualbox
name: myproject
hostname: myproject.test
folders:
- map: .
to: /home/vagrant/code
sites:
- map: www.myproject.test
to: /home/vagrant/code/public
type: apache
php: "7.2"
databases:
- myprojectdb
@dccampbell
Copy link
Author

dccampbell commented Feb 28, 2020

Notes:

  • .env.example, .gitignore, and composer.json are just snippets of those files directly relevant to this example setup.
  • PHP 7.2 is used here just as an example of how to set a consistent PHP version other than the Homestead default.
  • after.sh provides an easy place to install any system dependencies or do some extra setup like auto-restoring database backups.
  • The use of .env here is targeted at Laravel projects. Other projects may need to pass this information into the app differently.
  • For Laravel projects, the composer local script is a good place to add commands like ide-helper:generate and ide-helper:meta.
  • Homestead has a lot of convenient features that can be additionally enabled and easily configured.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment