The powerful Homestead

Homestead is complete dev environment mainly for Laravel. But it also has many (hidden) powerful features. Here is some of them.

Database snapshot (lmm)

Take snapshots of MySQL or MariaDB databases by using Logical MySQL Manager.

This is extremely helpful when working with complex databases or test complex flows that make many changes into db, so you cannot handle all.

Example:

  • You’re testing a payment method, it will create subscription record in db, change user status…
  • You want to keep the state of db before make a payment.
  • Normally, you export the db into SQL file —> do the testing steps —> then restore db by import that SQL file.

With lmm, forget all above time-consumed steps. Just type a single command to take snapshot (A) of current state —> do whatever you want to mess up the db —> type another single command to restore all things into the snapshot (A).

Common work flow

  1. Create a snapshot: sudo lmm branch [snapshot_name].
  2. Modify the database.
  3. Go back to snapshot state: sudo lmm merge [snapshot_name].

Common errors

Snapshot name too long: Snapshot name too long Failed to create the snapshot, but the system will still mount it as new device –> Timeout when start the box.
Solution: Unmount corrupt device (snapshot) by comment it out in /etc/fstab. Boot through VirtualBox interface.

Note 1: Snapshot contains all databases in disk.
Note 2: This feature will increase disk size very fast. I handle it by break down to small vagrant boxes. Then manage them in a single Windows console, check it out here.

Dynamic web server / PHP version

Homestead sticks with Nginx as default, but you freely choose to use Apache if you want. This, in my opinion, makes Homestead a replacement of Scotchbox.
Usage: Modify Homestead.yaml to use Apache for specific sites.

Choose PHP version is similar, you can set for specific sites.
Other dynamic PHP version commands:

  • Use artisan by a specific version:
    php5.6 artisan list
    php7.3 artisan list
  • Change PHP CLI version:
    php56
    php74

Complete example in Homestead.yml

sites:
  - map: example.local
    to: /home/vagrant/code/dir
    php: "7.3"
    type: "apache"
features:
  - apache: true

Note 1: If you use both Nginx and Apache, switch between them by flip command. There are plenty of useful commands / alias, take a look at your_homestead_path/scripts.
Note 2: If all sites are Apache, Apache will be enabled by default.

Expose local environment

Homestead use Ngrok to share your configured site to Internet.
Simply use share [site_name] command.

That’s all, full other features can be found in Laravel official docs: https://laravel.com/docs/7.x/homestead

updatedupdated2020-12-222020-12-22