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 setup 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 but 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 setup and working smoothly and I could setup sites locally in no time. But like most things tech, there were a few little blips a long 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 incase 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 was very important. 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 php@7.2 && brew remove php@7.2
brew unlink mysql@5.7 && 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 php@7.2 && brew link php@7.2 --force
brew install mysql@5.7 && brew link mysql@5.7 --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 setup a site on Craft CMS)

brew services start nginx dnsmasq php@7.2 mysql@5.7
mysql_secure_installation

One thing I found when setting up a Craft CMS site in Valet was 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.

So 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 setup Valet and you're ready to boot it up. To do this, simply run valet start and watch the services start up. 🎉

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

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

Optional

Once i'd setup Valet, I did find myself running in to 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 in Craft CMS Discord channel (@bymayo)