DEV Community

Cover image for How to Serve Laravel Apps With Valet on MacOS
Martins Onuoha
Martins Onuoha

Posted on

How to Serve Laravel Apps With Valet on MacOS

If you’re here, you probably already have Laravel installed on your machine (and you run macOS). If you don’t have Laravel set up properly and you use a different operating system, I’ve written a guide to help you set up Laravel.

The Hitchhiker’s Guide to Laravel Setup | DevjavuThe Hitchhiker’s Guide to Laravel Setup | Devjavu

A step by step guide to setting up Laravel on your machine.

favicon devjavu.space

This is specifically for macOS users who:

  • Have Laravel installed.

  • Would like to run and manage Laravel applications on a single domain (e.g., mylaravelapp.awesome, myapp.app).

  • Would like to run Laravel sites over encrypted TLS (HTTP/2).

  • Would like to share their local sites with the world.

  • Just want to tinker.


Understanding How Valet Works

Valet is a PHP development environment for PHP frameworks, including Laravel, WordPress, etc. Valet configures your Mac to always run Nginx Server in the background when your machine starts.

Using dnsmasq, Valet proxies all requests on the *.test (this can be changed to a custom domain) domain to point to sites installed on your local machine. Valet also allows you to share your sites publicly using local tunnels.

Out of the box, Valet support includes, but is not limited to:


Installation

Valet requires macOS and Homebrew. Before installation, you should make sure that no other programs, such as Apache or Nginx, are binding to your local machine’s port 80.

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Enter fullscreen mode Exit fullscreen mode
  • Update brew
brew update
Enter fullscreen mode Exit fullscreen mode
  • Install PHP7.3.
brew install php
Enter fullscreen mode Exit fullscreen mode
  • Install Composer.

  • Download the installer to the current directory.

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
Enter fullscreen mode Exit fullscreen mode
  php -r "if (hash_file('sha384', 'composer-setup.php') === 'a5c698ffe4b8e849a443b120cd5ba38043260d5c4023dbf93e1558871f1f07f58274fc6f4c93bcfd858c6bd0775cd8d1') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
Enter fullscreen mode Exit fullscreen mode
  • Run the installer.
php composer-setup.php
Enter fullscreen mode Exit fullscreen mode
  • Remove the installer.
php -r "unlink('composer-setup.php');"
Enter fullscreen mode Exit fullscreen mode
  • Install Valet with Composer.
composer global require laravel/valet
Enter fullscreen mode Exit fullscreen mode

To have the Valet command available globally on your system, make sure you have the ~/.composer/vendor/bin directory in your system’s “PATH”.

You can also see how to add the bin folder to your path here.

Configure and install Valet and Dnsmasq to always run when you start your machine:

valet install
Enter fullscreen mode Exit fullscreen mode

Once Valet is installed, try pinging any *.test domain on your terminal using a command such as ping foobar.test. If Valet is installed correctly, you should see this domain responding on 127.0.0.1.


Using a Custom Domain

If you would like to use a different domain other than the default (.test), you can simply tell Valet to use a new domain name:

valet tld new-name

e.g

valet tld app
Enter fullscreen mode Exit fullscreen mode

You should see an output on your terminal verifying that your new domain has been updated:

Restarting dnsmasq...
Valet is configured to serve for TLD [.app]
Restarting php...
Restarting nginx...
Your Valet TLD has been updated to [app].
Enter fullscreen mode Exit fullscreen mode

Valet will automatically start serving your projects at *.app.


Serving Laravel Sites

Once Valet is installed, you’re ready to start serving sites. First, you’ll need to configure a folder where Valet should search for your sites. Think of this as your htdocs folder (if you ever used Xampp or a similar tool).

Create a folder where you’ll have your Laravel sites:

mkdir ~/Laravel_Sites/
Enter fullscreen mode Exit fullscreen mode

Navigate into that directory:

cd ~/Laravel_Sites/
Enter fullscreen mode Exit fullscreen mode

Now run:

valet park
Enter fullscreen mode Exit fullscreen mode

The park command will register your current directory as a path that Valet should search for your sites.

You can proceed to create a new Laravel application in the same folder.

laravel new sleek_site
Enter fullscreen mode Exit fullscreen mode

Your new site should now be accessible at http://sleek_site.app.


Securing Local Sites With TLS

You can serve your Laravel sites locally over HTTPS, thanks to Valet. Simply change into your site directory (~/Laravel_Sites) and run:

valet secure site_name # e.g valet secure sleek_site
Enter fullscreen mode Exit fullscreen mode

You should now be able to access your Laravel application at https://sleek_site.app.


Sharing Local Sites

Valet comes coupled with ngrok for sharing your local sites on a public URL. No additional software installation is required. To share your local site, navigate into your project directory and run

cd sleek_site

valet serve
Enter fullscreen mode Exit fullscreen mode

You should see a server running in your terminal.

Terminal

You can publicly access your Laravel site by opening the URL http://5e44d238.ngrok.io/ in your browser or from a secure source via http://5e44d238.ngrok.io/.


At this point, you’ve successfully set up Valet to serve Laravel applications locally.

Cheers ☕️


Top comments (0)