How to Serve Laravel Apps With Valet on MacOS
Tags: laravel, valet, webdev
Published: 2023-12-10 21:05:40

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.

{% embed https://devjavu.space/post/the-hitchhiker-s-guide-to-laravel-setup/ %}

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](https://www.nginx.com/) Server in the background when your machine starts.

Using [dnsmasq](https://en.wikipedia.org/wiki/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:

- [Laravel](https://laravel.com/)

- [Lumen](https://lumen.laravel.com/docs/10.x)

- [CakePHP 3](https://cakephp.org/)

- [Concrete5](https://concretecms.com/)

- [Drupal](https://www.drupal.org/)

- [Joomla](https://www.joomla.org/)

- [Magento](https://business.adobe.com/products/magento/magento-commerce.html)

- Static HTML

- [Symfony](https://symfony.com/)

- [WordPress](https://wordpress.org/)

---

## Installation

Valet requires macOS and [Homebrew](https://brew.sh/). Before installation, you should make sure that no other programs, such as Apache or Nginx, are binding to your local machine’s port 80.

- Install [Homebrew](https://brew.sh/).

```bash
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
```

- Update brew

```bash
brew update
```

- Install PHP7.3.

```bash
brew install php
```

- Install Composer.

- Download the installer to the current directory.

```bash
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
```

- Verify the installer SHA-384, which you can also [cross-check here](https://composer.github.io/pubkeys.html).

```bash
  php -r "if (hash_file('sha384', 'composer-setup.php') === 'a5c698ffe4b8e849a443b120cd5ba38043260d5c4023dbf93e1558871f1f07f58274fc6f4c93bcfd858c6bd0775cd8d1') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
```

- Run the installer.

```bash
php composer-setup.php
```

- Remove the installer.

```bash
php -r "unlink('composer-setup.php');"
```

- Install Valet with Composer.

```bash
composer global require laravel/valet
```

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](https://medium.com/better-programming/the-hitchhikers-guide-to-laravel-setup-6c1fd98c2670).

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

```bash
valet install
```

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:

```bash
valet tld new-name

e.g

valet tld app
```

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

```bash
Restarting dnsmasq...
Valet is configured to serve for TLD [.app]
Restarting php...
Restarting nginx...
Your Valet TLD has been updated to [app].
```

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:

```bash
mkdir ~/Laravel_Sites/
```

Navigate into that directory:

```bash
cd ~/Laravel_Sites/
```

Now run:

```bash
valet park
```

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.

```bash
laravel new sleek_site
```

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:

```bash
valet secure site_name # e.g valet secure sleek_site
```

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

---

## Sharing Local Sites

Valet comes coupled with [ngrok](https://ngrok.com/) 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

```bash
cd sleek_site

valet serve
``` 
You should see a server running in your terminal.


![Terminal](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sgciukvo9e8zyhsm11aa.png)


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

---

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

Cheers ☕️

---