17/02

Installing Laravel Valet for Craft CMS

Since the beginning of time, I've always been a MAMP preacher.

It was simple; it has a UI to set up hosts, install PHP versions and generally makes development speedier. Until recently, when MAMP required an update for macOS Catalina and I was left with a broken install that I was spending hours trying to fix.

I'd heard of other similar setups over the years - like Laravel Homestead, Vagrant and Docker. Still, pretty much every time I tried to set these up, I'd hit a point where I felt I was still spending many hours setting up a development environment.

So I turned to Google and found Jalen Davenport's article Running Craft CMS 3 on Laravel Valet.

After following the tutorial, I had Laravel Valet set up and working smoothly, and I could set up sites locally in no time. But like most things tech, there were a few little blips along the way. So I thought it would be good to outline my install process.

Install Brew

If you haven't already got Brew installed for macOS, you will need to run $ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" in terminal.

Just in case you have had Valet installed previously, I found that double-checking to see if Valet was previously installed or other Brew packages were installed/needed to be updated very importantly. So I removed valet and unlinked the services I required.

rm -rf ~/.valet
brew unlink nginx && brew remove nginx
brew unlink dnsmasq && brew remove dnsmasq
brew unlink [email protected] && brew remove [email protected]
brew unlink [email protected] && brew remove mysql

After unlinking and removing the services, I installed them again with Brew from fresh.

brew install nginx && brew link nginx
brew install dnsmasq && brew link dnsmasq
brew install [email protected] && brew link [email protected] --force
brew install [email protected] && brew link [email protected] --force

Install Composer & Valet

Once you'd got a few dependencies and services installed, you can install Valet. You need to have Composer installed to install Valet as this is served from a Composer package. Setting the PATH variable can be a little tricky depending on the macOS version you're running, so there are two ways to run this. This also depends if you're using bash or zsh.

brew install composer
# If you use bash:
echo 'export PATH="$PATH:~/.composer/vendor/bin"' >> ~/.bashrc
# If you use zsh:
echo 'export PATH="$PATH:/Users/*REPLACE_USER*/.composer/vendor/bin"' >> ~/.zshrc

Now Composer is installed you should be able to install Valet using Composer.

composer global require laravel/valet
valet install

Setup MySQL & PHP

Once you've installed Valet, you should start-up services that are required by Valet and run the MySQL installation.
(Note: that the services we're using here are to set up a site on Craft CMS)

brew services start nginx dnsmasq [email protected] [email protected]
mysql_secure_installation

When setting up a Craft CMS site in Valet, I found that it would eat through the default Valet PHP and Nginx timeout settings. This isn't because Craft CMS requires intense settings, because it doesn't, but merely because the default settings for Valet & Nginx are extremely low.

To ensure you don't get 500 time out errors, increase the settings on both Valet and Nginx.

cd /usr/local/etc/php/7.2/conf.d
sudo vim php-memory-limits.ini
# Press i
# Add / edit settings:
max_input_vars = 4000
max_execution_time = 240
max_input_time = 240
memory_limit = 512M
# Press Esc
# Type :x then hit Return
sudo vim /usr/local/etc/nginx/valet/valet.conf
# Add to location ~ \.php$ { } block
proxy_connect_timeout       3000;
proxy_send_timeout          3000;
proxy_read_timeout          3000;
send_timeout                3000;
fastcgi_read_timeout        300;
fastcgi_buffers 8 128k;
fastcgi_buffer_size         256k;
# Press Esc
# Type :x then hit Return

# https://github.com/laravel/valet/issues/315
sudo vim /usr/local/etc/php/7.x/php-fpm.d/www.conf
pm.max_children = 200
pm.start_servers = 20
pm.min_spare_servers = 10
pm.max_spare_servers = 20
pm.process_idle_timeout = 10s
pm.max_requests = 500

Start Valet

That's it, you've set up Valet, and you're ready to boot it up. To do this, run valet start and watch the services startup. 🎉

Once this is running, you can then link Valet with a site folder (E.g. the /web folder if you're using Craft CMS)

cd /to/site/public/folder
valet link site-name
valet open site-name

Optional

Once I'd set up Valet, I did find myself running into other minor issues:

.dev Domain

By default, Valet uses the TLD of .dev (E.g. http://site-name.dev). This can be problematic depending on the browser, as some browsers now load .dev domains, so I prefer to use .local for local development. To do this, run valet tld local then add local to System Preferences > Network > Advanced > DNS > Search Domains

Feed Me Imports

When trying to import feeds using the Craft CMS plugin Feed Me, it was throwing cURL errors. To fix this, I came across an issue that mentioned adding 127.0.0.1 and 8.8.8.8to System Preferences > Network > Advanced > DNS > DNS Servers

If you have any questions on this, hit me up on Twitter (@madebymayo) or find me on Craft CMS Discord channel (@bymayo)