Laravel Valet

Introduction

Valet is a Laravel development environment for Mac minimalists. No Vagrant, no /etc/hosts file. You can even share your sites publicly using local tunnels. Yeah, we like it too.

Laravel Valet configures your Mac to always run Nginx in the background when your machine starts. Then, using DnsMasq, Valet proxies all requests on the *.test domain to point to sites installed on your local machine.

In other words, a blazing fast Laravel development environment that uses roughly 7 MB of RAM. Valet isn't a complete replacement for Vagrant or Homestead, but provides a great alternative if you want flexible basics, prefer extreme speed, or are working on a machine with a limited amount of RAM.

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

However, you may extend Valet with your own custom drivers.

Valet Or Homestead

As you may know, Laravel offers Homestead, another local Laravel development environment. Homestead and Valet differ in regards to their intended audience and their approach to local development. Homestead offers an entire Ubuntu virtual machine with automated Nginx configuration. Homestead is a wonderful choice if you want a fully virtualized Linux development environment or are on Windows / Linux.

Valet only supports Mac, and requires you to install PHP and a database server directly onto your local machine. This is easily achieved by using Homebrew with commands like brew install php and brew install mysql. Valet provides a blazing fast local development environment with minimal resource consumption, so it's great for developers who only require PHP / MySQL and do not need a fully virtualized development environment.

Both Valet and Homestead are great choices for configuring your Laravel development environment. Which one you choose will depend on your personal taste and your team's needs.

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.

  • Install or update Homebrew to the latest version using brew update.
  • Install PHP 7.4 using Homebrew via brew install php.
  • Install Composer.
  • Install Valet with Composer via composer global require laravel/valet. Make sure the ~/.composer/vendor/bin directory is in your system's "PATH".
  • Run the valet install command. This will configure and install Valet and DnsMasq, and register Valet's daemon to launch when your system starts.

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.

Valet will automatically start its daemon each time your machine boots. There is no need to run valet start or valet install ever again once the initial Valet installation is complete.

Using Another Domain

By default, Valet serves your projects using the .test TLD. If you'd like to use another domain, you can do so using the valet tld tld-name command.

For example, if you'd like to use .app instead of .test, run valet tld app and Valet will start serving your projects at *.app automatically.

Database

If you need a database, try MySQL by running brew install [email protected] on your command line. Once MySQL has been installed, you may start it using the brew services start [email protected] command. You can then connect to the database at 127.0.0.1 using the root username and an empty string for the password.

PHP Versions

Valet allows you to switch PHP versions using the valet use [email protected] command. Valet will install the specified PHP version via Brew if it is not already installed:

  1. valet use [email protected]
  2. valet use php

{note} Valet only serves one PHP version at a time, even if you have multiple PHP versions installed.

Resetting Your Installation

If you are having trouble getting your Valet installation to run properly, executing the composer global update command followed by valet install will reset your installation and can solve a variety of problems. In rare cases it may be necessary to "hard reset" Valet by executing valet uninstall —force followed by valet install.

Upgrading

You may update your Valet installation using the composer global update command in your terminal. After upgrading, it is good practice to run the valet install command so Valet can make additional upgrades to your configuration files if necessary.

Serving Sites

Once Valet is installed, you're ready to start serving sites. Valet provides two commands to help you serve your Laravel sites: park and link.

The park Command

  • Create a new directory on your Mac by running something like mkdir ~/Sites. Next, cd ~/Sites and run valet park. This command will register your current working directory as a path that Valet should search for sites.
  • Next, create a new Laravel site within this directory: laravel new blog.
  • Open http://blog.test in your browser.

That's all there is to it. Now, any Laravel project you create within your "parked" directory will automatically be served using the http://folder-name.test convention.

The link command may also be used to serve your Laravel sites. This command is useful if you want to serve a single site in a directory and not the entire directory.

  • To use the command, navigate to one of your projects and run valet link app-name in your terminal. Valet will create a symbolic link in ~/.config/valet/Sites which points to your current working directory.
  • After running the link command, you can access the site in your browser at http://app-name.test.

To see a listing of all of your linked directories, run the valet links command. You may use valet unlink app-name to destroy the symbolic link.

{tip} You can use valet link to serve the same project from multiple (sub)domains. To add a subdomain or another domain to your project run valet link subdomain.app-name from the project folder.

Securing Sites With TLS

By default, Valet serves sites over plain HTTP. However, if you would like to serve a site over encrypted TLS using HTTP/2, use the secure command. For example, if your site is being served by Valet on the laravel.test domain, you should run the following command to secure it:

  1. valet secure laravel

To "unsecure" a site and revert back to serving its traffic over plain HTTP, use the unsecure command. Like the secure command, this command accepts the host name that you wish to unsecure:

  1. valet unsecure laravel

Sharing Sites

Valet even includes a command to share your local sites with the world, providing an easy way to test your site on mobile devices or share it with team members and clients. No additional software installation is required once Valet is installed.

Sharing Sites Via Ngrok

To share a site, navigate to the site's directory in your terminal and run the valet share command. A publicly accessible URL will be inserted into your clipboard and is ready to paste directly into your browser or share with your team.

To stop sharing your site, hit Control + C to cancel the process.

{tip} You may pass additional parameters to the share command, such as valet share —region=eu. For more information, consult the ngrok documentation.

Sharing Sites On Your Local Network

Valet restricts incoming traffic to the internal 127.0.0.1 interface by default. This way your development machine isn't exposed to security risks from the Internet.

If you wish to allow other devices on your local network to access the Valet sites on your machine via your machine's IP address (eg: 192.168.1.10/app-name.test), you will need to manually edit the appropriate Nginx configuration file for that site to remove the restriction on the listen directive by removing the the 127.0.0.1: prefix on the directive for ports 80 and 443.

If you have not run valet secure on the project, you can open up network access for all non-HTTPS sites by editing the /usr/local/etc/nginx/valet/valet.conf file. However, if you're serving the project site over HTTPS (you have run valet secure for the site) then you should edit the ~/.config/valet/Nginx/app-name.test file.

Once you have updated your Nginx configuration, run the valet restart command to apply the configuration changes.

Site Specific Environment Variables

Some applications using other frameworks may depend on server environment variables but do not provide a way for those variables to be configured within your project. Valet allows you to configure site specific environment variables by adding a .valet-env.php file within the root of your project. These variables will be added to the $_SERVER global array:

  1. <?php
  2. // Set $_SERVER['key'] to "value" for the foo.test site...
  3. return [
  4. 'foo' => [
  5. 'key' => 'value',
  6. ],
  7. ];
  8. // Set $_SERVER['key'] to "value" for all sites...
  9. return [
  10. '*' => [
  11. 'key' => 'value',
  12. ],
  13. ];

Custom Valet Drivers

You can write your own Valet "driver" to serve PHP applications running on another framework or CMS that is not natively supported by Valet. When you install Valet, a ~/.config/valet/Drivers directory is created which contains a SampleValetDriver.php file. This file contains a sample driver implementation to demonstrate how to write a custom driver. Writing a driver only requires you to implement three methods: serves, isStaticFile, and frontControllerPath.

All three methods receive the $sitePath, $siteName, and $uri values as their arguments. The $sitePath is the fully qualified path to the site being served on your machine, such as /Users/Lisa/Sites/my-project. The $siteName is the "host" / "site name" portion of the domain (my-project). The $uri is the incoming request URI (/foo/bar).

Once you have completed your custom Valet driver, place it in the ~/.config/valet/Drivers directory using the FrameworkValetDriver.php naming convention. For example, if you are writing a custom valet driver for WordPress, your file name should be WordPressValetDriver.php.

Let's take a look at a sample implementation of each method your custom Valet driver should implement.

The serves Method

The serves method should return true if your driver should handle the incoming request. Otherwise, the method should return false. So, within this method you should attempt to determine if the given $sitePath contains a project of the type you are trying to serve.

For example, let's pretend we are writing a WordPressValetDriver. Our serves method might look something like this:

  1. /**
  2. * Determine if the driver serves the request.
  3. *
  4. * @param string $sitePath
  5. * @param string $siteName
  6. * @param string $uri
  7. * @return bool
  8. */
  9. public function serves($sitePath, $siteName, $uri)
  10. {
  11. return is_dir($sitePath.'/wp-admin');
  12. }

The isStaticFile Method

The isStaticFile should determine if the incoming request is for a file that is "static", such as an image or a stylesheet. If the file is static, the method should return the fully qualified path to the static file on disk. If the incoming request is not for a static file, the method should return false:

  1. /**
  2. * Determine if the incoming request is for a static file.
  3. *
  4. * @param string $sitePath
  5. * @param string $siteName
  6. * @param string $uri
  7. * @return string|false
  8. */
  9. public function isStaticFile($sitePath, $siteName, $uri)
  10. {
  11. if (file_exists($staticFilePath = $sitePath.'/public/'.$uri)) {
  12. return $staticFilePath;
  13. }
  14. return false;
  15. }

{note} The isStaticFile method will only be called if the serves method returns true for the incoming request and the request URI is not /.

The frontControllerPath Method

The frontControllerPath method should return the fully qualified path to your application's "front controller", which is typically your "index.php" file or equivalent:

  1. /**
  2. * Get the fully resolved path to the application's front controller.
  3. *
  4. * @param string $sitePath
  5. * @param string $siteName
  6. * @param string $uri
  7. * @return string
  8. */
  9. public function frontControllerPath($sitePath, $siteName, $uri)
  10. {
  11. return $sitePath.'/public/index.php';
  12. }

Local Drivers

If you would like to define a custom Valet driver for a single application, create a LocalValetDriver.php in the application's root directory. Your custom driver may extend the base ValetDriver class or extend an existing application specific driver such as the LaravelValetDriver:

  1. class LocalValetDriver extends LaravelValetDriver
  2. {
  3. /**
  4. * Determine if the driver serves the request.
  5. *
  6. * @param string $sitePath
  7. * @param string $siteName
  8. * @param string $uri
  9. * @return bool
  10. */
  11. public function serves($sitePath, $siteName, $uri)
  12. {
  13. return true;
  14. }
  15. /**
  16. * Get the fully resolved path to the application's front controller.
  17. *
  18. * @param string $sitePath
  19. * @param string $siteName
  20. * @param string $uri
  21. * @return string
  22. */
  23. public function frontControllerPath($sitePath, $siteName, $uri)
  24. {
  25. return $sitePath.'/public_html/index.php';
  26. }
  27. }

Other Valet Commands

CommandDescription
valet forgetRun this command from a "parked" directory to remove it from the parked directory list.
valet logView a list of logs which are written by Valet's services.
valet pathsView all of your "parked" paths.
valet restartRestart the Valet daemon.
valet startStart the Valet daemon.
valet stopStop the Valet daemon.
valet trustAdd sudoers files for Brew and Valet to allow Valet commands to be run without prompting for passwords.
valet uninstallUninstall Valet: Shows instructions for manual uninstall; or pass the —force parameter to aggressively delete all of Valet.

Valet Directories & Files

You may find the following directory and file information helpful while troubleshooting issues with your Valet environment:

File / PathDescription
~/.config/valet/Contains all of Valet's configuration. You may wish to maintain a backup of this folder.
~/.config/valet/dnsmasq.d/Contains DNSMasq's configuration.
~/.config/valet/Drivers/Contains custom Valet drivers.
~/.config/valet/Extensions/Contains custom Valet extensions / commands.
~/.config/valet/Nginx/Contains all Valet generated Nginx site configurations. These files are rebuilt when running the install, secure, and tld commands.
~/.config/valet/Sites/Contains all symbolic links for linked projects.
~/.config/valet/config.jsonValet's master configuration file
~/.config/valet/valet.sockThe PHP-FPM socket used by Valet's Nginx configuration. This will only exist if PHP is running properly.
~/.config/valet/Log/fpm-php.www.logUser log for PHP errors.
~/.config/valet/Log/nginx-error.logUser log for Nginx errors.
/usr/local/var/log/php-fpm.logSystem log for PHP-FPM errors.
/usr/local/var/log/nginxContains Nginx access and error logs.
/usr/local/etc/php/X.X/conf.dContains *.ini files for various PHP configuration settings.
/usr/local/etc/php/X.X/php-fpm.d/valet-fpm.confPHP-FPM pool configuration file.
~/.composer/vendor/laravel/valet/cli/stubs/secure.valet.confThe default Nginx configuration used for building site certificates.