A practical introduction of Laravel Valet

A practical introduction of Laravel Valet

Posted on:February 26, 2020 at 10:00 AM

Hello.

In my previous post, I wrote about how to use Virtual Host in MacOS. Today, I will try to make your life easier who uses laravel and want to implement a virtual host. In this tutorial, I would love to show you a practical introduction to Laravel Valet. Let’s deep driving into it.

Table of Contents

Open Table of Contents

Scenario

Imagine that, you have a Laravel project on MacOS, now you want to use a virtual host for the local development for accessing anything.test, instead of http://127.0.0.1. In that case, laravel brings valet for you our of the box.

I assume you have a fresh machine. You can skip any step if you feel that you have already.

Let’s start-

1. Install Homebrew

You can follow official docs or use as follow-

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

2. Install PHP version.

brew install php

3. Install Database

You can install database as you wish.

For MySql

brew install [email protected]

Check more in the official docs.

For MariaDB

brew install mariadb

Now run the following command-

sudo mysql -u root

mysql> USE mysql;
mysql> SELECT User, Host, plugin FROM mysql.user;

You may see something like this-

+------------------+-----------------------+
| User             | plugin                |
+------------------+-----------------------+
| root             | auth_socket           |
| mysql.sys        | mysql_native_password |
| debian-sys-maint | mysql_native_password |
+------------------+-----------------------+

As you can see in the query, the root user is using the auth_socket plugin.

Run this command and reset the password:

ALTER USER 'root'@'localhost' IDENTIFIED BY 'root';

Now your mySQL login password is: root.

4. Download composer

Download composer from this link: https://getcomposer.org/installer

Then, Rename it composer.phar

5. Run PHP Phar

Try with this command-

php composer.phar

Then move it to the composer directory by the following command-

mv composer.phar /usr/local/bin/composer

6. Install Valet Globally

Run this command to install valet globally-

composer global require laravel/valet

Then-

export PATH=$PATH:~/.composer/vendor/bin

7. Install Valet

Now run this command to install valet-

valet install

8. Park your directory into Valet

To park your whole coding directory or any specific directory, you just go to the directory and run the following command. For example, in my case, I put all of my coding projects into /Users/username/desktop/code, so I will go to the code folder then run the following command-

valet park

9. Access your site.

Once you parked successfully, all of your project inside the folder will be accessible via .test domain. For example, I have the following projects inside the code directory-

code
    - project1
    - project2
    - project2

Now, I just go to the browser and run http://project1.test where I should able to see the content of the project1 folder.

It’s cool, right?

I really love this feature. I like it.

Use HTTPS in your project

You can even use https in your local project. To make it secure, you just need to run valet secure your-project-folder. For example, in my case, I will go to code and want to make secure project1, so I will run the following command.

valet secure project1

Even if you want, you can unsecure also. To unsecure your project, you can try as following-

valet unsecure project1

Change domain tld

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.

Change default domain.

By default laravel valet use folder name as the default domain name. To change the default domain, just go to the parked folder and run valet link your-desire-domain. For example, in my case, instead of project1 I want to use learning.test. So I will cd to /code folder and run the following command-

valet link learning

Now, I can go to http://learning.test which will be opening the project1.

Some useful commands

valet forget Run this command from a “parked” directory to remove it from the parked directory list.

valet log View a list of logs which are written by Valet’s services.

valet paths View all of your “parked” paths.

valet restart Restart the Valet daemon.

valet start Start the Valet daemon.

valet stop Stop the Valet daemon.

valet trust Add sudoers files for Brew and Valet to allow Valet commands to be run without prompting for passwords.

valet uninstall Uninstall Valet: Shows instructions for manual uninstall; or pass the --force parameter to aggressively delete all of Valet.

valet links See the list of the linked project

valet unlink SITENAME If you want to unlink any site from the valet, you can try this.

Hope this post helps you.

Thanks for reading.