We use cookies to make your viewing experience better. By accepting you consent, you agree to our Cookie policy

Accept
Improve your Craft CMS skills

A Guide To Running Craft On Laravel Homestead

10 min read
Shape April 2022 HR 28

Struggling with a slow local dev environment for your Craft CMS site? Laravel Homestead provides the ideal all-in-one solution. This definitive guide shares hard-won insights on fully optimizing Homestead to power your Craft development workflow. From quick setup to easy production deployment, learn how Homestead's virtualization supercharges Craft - saving money and headaches along the way.

Laravel Homestead provides an optimized local dev environment for Craft CMS. This guide covers Homestead setup, installing and configuring Craft, optimizing performance, and migrating to production. Key techniques include utilizing Vagrant for virtualization, Git for code deployment, database dumps for syncing data, and external asset volumes for managing uploads.

Prerequisites for Using Laravel Homestead

Installing Requirements like Vagrant & VirtualBox

Before diving into Laravel Homestead, you'll need to install a few prerequisite applications and tools. The main requirements are Vagrant and VirtualBox. Vagrant is an open source tool for building development environments while VirtualBox allows you to run virtual machines on your local system.

Having both Vagrant and VirtualBox installed is crucial for Homestead to function properly. The latest versions can be downloaded directly from the Vagrant and VirtualBox websites. Make sure to follow the installation instructions carefully.

In addition to Vagrant and VirtualBox, having Git command line interface (CLI) available on your system is also necessary when using Homestead. Git allows you to clone the Homestead repository from GitHub to your local machine. So install Git if you haven't already.

Some comfort with using the command line is also recommended before jumping into Homestead. You'll need to run various commands to manage and interact with your Homestead boxes. So get familiar with some common commands like cd, ls, mv, rm, cp etc.

Overall, having Vagrant, VirtualBox, Git CLI and basic command line skills installed and ready will prep your system for efficiently working with the Laravel Homestead development environment.


Understanding Key Concepts like Boxes and Port Forwarding

There are some key concepts around Vagrant and virtual machines that are useful to understand before using Homestead. Doing so will help you effectively leverage Homestead's features.

For instance, the idea of "boxes" in Vagrant is crucial. Boxes are basically packaged Vagrant environments that have a web server, scripts, tools and everything else needed installed and configured. Homestead itself is a Vagrant box.

When you vagrant up a box, Vagrant handles automatically downloading the box image, starting up the virtual machine, and configuring networking - providing a ready-to-use dev environment.

Speaking of networking, port forwarding is another important concept. This allows accessing the web services running inside the Homestead virtual machine from your local browser. For example, port 8000 inside the VM is forwarded to port 8000 on your host machine.

It's also helpful to understand the virtual machine lifecycle of vagrant up, vagrant halt, vagrant destroy that controls creating, suspending and deleting your Homestead boxes. Properly controlling the VM lifecycle will ensure you don't waste resources.

So grasping the main ideas around Vagrant boxes, port forwarding, the VM lifecycle, and networking will provide the conceptual basis to fully leverage Homestead and avoid roadblocks. Refer to the Vagrant documentation to dig deeper on these concepts.

Setting up Required Accounts and Tools

Lastly, there are a few accounts and tools you may want to setup before getting started with Homestead to have a smooth experience.

First, you'll want a GitHub account so you can fork and clone the main Laravel Homestead repository. This provides the Vagrantfile configuration and scripts to build your Homestead boxes. So create a free GitHub account if you don't have one.

You may also want to create an account on Vagrant Cloud. This hosts publicly available Vagrant boxes you can use for quicker setup. Homestead is available in the Vagrant Cloud so having an account lets you access that.

Generating a pair of SSH keys and adding the public key to your GitHub and Vagrant Cloud accounts is also recommended. This allows seamless SSH into your Homestead boxes without a password.

On Windows, you may need to install an SSH client like PuTTY if one doesn't already exist. For Linux and Mac, OpenSSH will be available.

Finally, test that you have a stable, fast internet connection. Downloading the Homestead box image and associated dependencies can use significant bandwidth. A sluggish connection may cause timeouts.

With accounts on GitHub and Vagrant Cloud setup, SSH keys configured, and a speedy internet connection - you'll be primed for quickly getting up and running with Laravel Homestead.

Initial Setup and Configuration of Laravel Homestead

Downloading and Installing the Homestead Vagrant Box

With the prerequisites installed, you can now setup and configure your initial Laravel Homestead environment. The first step is cloning the Homestead repository from GitHub to your local machine.

Head to the main Laravel Homestead GitHub repo and fork it into your own account. Then clone your fork locally with Git CLI, which will download all the Vagrant config files and scripts to run Homestead.

Once cloned, use a text editor to customise the Homestead.yaml file how you want. Here you can add sites, map folders, configure databases and tweak anything else needed for your Craft project setup.

For example, you may map your local Craft code project folder into the Homestead box and add a site record for 'craft.test' that points to that code folder. The Homestead docs explain all available config options.

With your custom Homestead.yaml file configured, you are ready to initially provision your Homestead box using vagrant up. This will download the Homestead box image and boot up the virtual machine for you.

The first time you run vagrant up it may take a while to download the Homestead box. But subsequent runs will be much quicker. Once finished, you can access the sites defined in your Homestead.yaml from your local dev machine.

For example, with a site record set for 'craft.test' you could open http://craft.test:8000 to view that site. The port forwarding enables accessing the VM internally running services.

So in summary, fork and clone Homestead repo, customize Homestead.yaml config file for your Craft project setup, run 'vagrant up' to provision the VM, and then access your defined sites. That covers getting up and running initially.

Configuring Sites, Folders Databases and Networks

Some key parts of the Homestead.yaml config file that are important to configure correctly for your Craft setup are sites, folders, databases and networking.

For sites, define one or more site records mapping a local folder path and domain name. For example, mapping '~/Code/craft' folder to 'craft.test' domain. Homestead will serve these sites over Nginx.

Set folders to sync any host folders you want available within the Homestead VM. For instance, syncing your Craft code folder provides access from within. The home folder is synced by default.

You can define MySQL and PostgreSQL database records with unique credentials per database. Homestead will automatically install and configure these databases.

Lastly, tweak networking as needed by mapping host ports to guest VM ports. For example, forward host port 8000 to guest port 80 for standard HTTP access to your Craft sites.

Properly configuring sites, folders, databases and networking is key for integrating Homestead into your custom Craft CMS application development workflow.

Controlling Homestead Box with CLI Commands

Vagrant provides a set of CLI commands to manage and interact with Homestead boxes. Learning the main ones will allow better control.

For example, vagrant up boots and provisions the Homestead virtual machine based on your Homestead.yaml config. This will set everything up initially.

vagrant halt is used to shut down the virtual machine when not in use. Halt avoids reprovisioning next time.

vagrant destroy completely deletes the virtual machine when you no longer need it. Run vagrant up again to recreate.

vagrant provision forces reprovisioning to apply changes made to Homestead.yaml config or any other customization.

vagrant ssh connects you into the Homestead VM over SSH to interact directly. Useful for debugging issues or VM management.

So becoming familiar with the main Vagrant CLI commands will enable you to proficiently control and manage your Laravel Homestead boxes containing your Craft CMS application code.

Installing and Configuring Craft CMS on Homestead

Uploading Craft CMS Files to Homestead Site Directory

With your Laravel Homestead box up and running, you can now install Craft CMS within it. Start by downloading the latest Craft release from https://craftcms.com/latest.

This will be a .zip file containing the craft core files and installer. Extract this zip to your local Craft project directory that is synced to one of the Homestead sites you configured.

For example, if you mapped the site 'craft.test' to the local folder '~/Code/craft-project', extract and upload the Craft files into that folder. The core Craft files provide the CMS foundation.

You can also upload any plugins, templates, and other Craft files into this site folder. Homestead will sync all these files into the site directory within the Homestead VM. This provides full access.

Having all your Craft CMS core, plugin, template, and other files uploaded to the mapped Homestead site directory prepares the foundation for installation and configuration fully within the VM.

Running Web-Based Installer and Initial Setup

With the Craft CMS files uploaded, you can now access the web-based installer by going to the site URL in your browser. For example, opening http://craft.test:8000.

This will initiate the Craft web installer. Steps include setting the site name, creating the admin account, configuring email settings, and any other initial settings.

The Craft installer provides a user-friendly way to get your site setup directly within the Homestead development environment. Follow all the setup steps prompted.

Make sure to create at least one admin user account during the install process. This will let you fully access the CMS control panel to manage everything after installation.

You may also want to configure additional settings like enabling Dev Mode for template debugging. Adjust Craft's config settings as needed for your particular project.

Overall, running through Craft's web-based installer provides a smooth way to get your CMS initially configured inside the Homestead box. Going through the prompts properly sets up your foundation.

Connecting Craft to Homestead MySQL Database

To complete the Craft installation, you need to connect it to the MySQL server running within Homestead.

First, ensure your Homestead.yaml file has port 3306 forwarded from the VM to your host. This exposes MySQL.

Then in your Craft .env file, enter the MySQL credentials and database name you specified in Homestead.yaml when defining your MySQL database record.

For example, if you had a db record called 'craftdb', use those credentials and 'craftdb' as the Craft database name. This connects them.

Test the connectivity by loading a Craft page. It should now connect and sync the database schema automatically. You may have to tweak your .env settings to troubleshoot any database connection issues.

With these steps complete, you now have Craft CMS fully installed and configured in your local Laravel Homestead development environment. From here you can develop your Craft site and leverage Homestead's benefits like easy sharing and portability.

Optimizing Laravel Homestead for Craft CMS

Craft-Optimized Nginx Configuration and VHost

The default Nginx configuration provided by Homestead can be further optimized for running Craft CMS. This improves performance.

For starters, enable clean URLs by setting $craft->config->general->enableCleanUrls = true; in your /config/general.php file.

Then update the Nginx site vhost config with try_files $uri $uri/ /index.php?$query_string; to properly support clean URLs.

You may also want to update the root path to point directly to your /web folder rather than the top-level Craft directory for improved security.

For caching, enable Nginx FastCGI caching for Craft by adding fastcgi_cache_path and fastcgi_cache directives. This significantly speeds up page loads.

Tweak the expiration time and other caching settings as needed for your particular site traffic patterns and requirements.

These Nginx optimizations tailored specifically for running Craft CMS in Laravel Homestead provide a performance boost over the default setup.

Setting Up Asset Volumes and Symlinks

Another area to optimize is managing Craft assets and uploads. Homestead makes this easier.

You can create an asset volume in Homestead mapped to a shared folder location. Then using symlinks, link your Craft /web/uploads folder to this volume.

This provides more space for uploads, shared access from the host, and separation between assets and the site code.

For cloud storage, add a Rackspace or Amazon S3 bucket as a volume and symlink it. Then enable asset storing there via the Craft control panel.

With assets now stored externally from the site code, you get redundancy, unlimited capacity and consolidate assets across environments.

Combined with version control excluding uploads, you achieve complete Craft site and asset portability between Homestead boxes.

Enabling Xdebug and Automated Workflows

There are also some nice productivity boosts Homestead offers when running Craft.

Install Xdebug in your Homestead box to enable debugging Craft locally. Set triggers in your IDE to initiate debugging sessions on demand.

Syncing your database down from production is easy with vagrant rsync-auto using SSH. Or script MySQL dumps/imports via crontab for automation.

Docker Compose can automatically handle running services like Redis and Elasticsearch alongside Homestead for a unified environment.

For frontend asset building, run NPM scripts on host that SSH into Homestead and execute them there for consistency. Or sync Node folders.

These are just some examples of ways to further enhance and automate your local development workflow using Homestead for powering Craft CMS projects.

Migrating Craft from Local Homestead to Live Server

Pushing Git Repository from Homestead to Production

When ready to go live, you need to migrate your Craft site code from Homestead to the production server. Using Git for version control streamlines this.

Within Homestead, ensure your Craft project files are committed to a Git repository. Ideally on GitHub or Bitbucket for sharing between environments.

On the production server, pull down this same Git repo containing the code. The folder structure should mirror what's on Homestead so Craft runs properly.

With the code deployed, enable maintenance mode in Craft's General Settings to prevent access during migration. Also turn off indexing and cron jobs temporarily.

Any new code changes made locally can be committed to Git and pushed from Homestead up to production at any time for continuous deployment.

Overall, keeping your Craft site code in a Git repo lets you smoothly push changes from local Homestead development to your live production server.

Syncing Database from Homestead to Live Database

In addition to code, you need to migrate the database from Homestead to production. Take care in doing this without overwriting data.

A safe approach is to export a mysqldump from the Homestead database and import it into the empty production database. This migrates schema and content.

For iterative updates, using rsync for selective database syncs from Homestead down to live works well. You can control what production data gets overwritten.

You might run PHP scripts to programmatically sync entities like new entries and products that don't yet exist in production.

Take your production site out of maintenance mode once you've fully migrated the database from your local Homestead box.

Considerations for Assets and User Uploads

When migrating a Craft site from Homestead to production, don't forget to handle assets and user uploads carefully.

Ideally you already have assets stored externally on S3 or a cloud volume. In that case, no migration is needed as assets remain consolidated.

If assets exist locally, manually copy over folders like /web/uploads from Homestead to the hosted server. Be selective to avoid overwriting.

Check the Asset Volumes settings in the Craft control panel on the live site. Reconfigure volumes to point properly to the file paths on the new hosting provider.

With code, database, assets, and uploads migrated - you'll have a fully functional live production Craft site that matches your local Homestead environment!

Shape April 2022 HR 202
Andy Golpys
- Author

Andy has scaled multiple businesses and is a big believer in Craft CMS as a tool that benefits both Designer, Developer and Client. 

Share
Feedback
Show us some love
Email Us
We usually reply within 72 hours
Agency Directory
Submit your agency
Affiliate Partners
Let's chat