Do You Valet Linux

in #programming6 years ago (edited)

Valet Linux - PHP Development Environment
blog1_title.png
Valet Linux Logo
PHP Logo - By Colin Viebrock, CC
Dnsmasq Logo - By the libvirt Project, CC
Nginx Logo - By Same author as NginxLogo.gif, PD

Introduction

Hello and welcome, today I would like to share a project that I found a few months ago that has greatly simplified my local web development workflow. Until recently, I used Vagrant based environments such as VVV or Homestead for all of my PHP projects. While these environments are both robust and feature rich, they are also quite heavy, and maintaining them can be time consuming. With all the configuration, provisioning, starting, and stopping of the virtual machines, not to mention that running a fully virtualised server can murder an aging laptop battery, I decided to look around for something a bit lighter, and Valet Linux caught my eye. Valet Linux is a pretty handy project for setting up a quick and lean PHP development environment. It is super easy to install and has been rock solid for my development needs.

Valet Linux

Valet Linux is a Linux port of the Laravel Valet project, and it is brought to us by Carlos Priego. I thank you Carlos, and I greatly appreciate the work you have put into bringing Valet to Linux. While Laravel Valet, and by extension, Valet Linux is billed as a Laravel development environment, it is also well suited for most any PHP project. With that said, lets get it installed, and checkout what it can do.

During installation, Valet installs and configures an instance of Nginx, as well as, DnsMasq and provides a command-line utility for configuration and management. DnsMasq will proxy the configured top level domain to the local instance of Nginx, thus allowing you to address your projects with what looks like a normal web address. For example you could host your project locally with an address that looks like this. http://project.test . Additionally, Valet provides the means to control which folder on your system that a given domain points to, thus allowing you to organize your projects however you please on the file system. Valet's command-line utility is very simple and strait-forward. We will be covering it in more detail once we get Valet installed.

Installation

Prior to the installation, it would be wise to take a look at Valet's requirements . It has been my experience that Valet will bark at you during the installation if you are missing any of its dependencies. One important detail to take note of prior to the installation, never install Valet with-in the "root" or "sudo" context. With that said, on to the installation.

  1. Since Valet's installer is distributed via Composer, you will need to have Composer installed. If you already have Composer installed then skip this step. If you don't have Composer installed, you can install it via your distributions package manager, or you can download it from their website. Since I am on an Arch based distro, I will use Pacman to install Composer and since PHP is a dependency of Composer, PHP will be installed as well.
sudo pacman -S composer
  1. Now that Composer is installed, we will have it download Valet. Composer will put a link to Valet's executable inside Composer's global "vendor/bin" directory. The location of this directory can depend on your distribution. You can usually find Composer's bin directory in one of the following locations.
    ~/.composer/vendor/bin
    ~/.config/composer/vendor/bin
composer global require cpriego/valet-linux
  1. For convenience, I like to add Composers's "vendor/bin" directory to my system's path. This can be accomplished multiple ways and will depend on your distribution. I usually add it to my .bashrc or .zshrc file. Here is an example of what my path looks like.
export PATH=$HOME/bin:/usr/local/bin:$HOME/.config/composer/vendor/bin:$PATH

After editing you .bashrc or .zshrc, you will have to use the source command to apply the change.

source .bashrc
or
source .zshrc
  1. Now we can install valet. Remember do not perform this step as root or sudo.
valet install

"You have missing Valet dependiencies:
 - jq
 - xsel

Please refer to https://cpriego.github.io/valet-linux/requirements on how to install them."

Oops, I forgot to install the dependencies.

sudo pacman -S jq xsel

Now, we can install Valet.

valet install

During Valet's installation it will install and configure Nginx and DnsMasq via your package manager.


Using Valet

If all went well in the previous steps, we should now be able to use Valet. Here's a listing of Valet's commands.

  domain          " Get or set the domain used for Valet sites"
  fetch-share-url  "Get the URL to the current Ngrok tunnel"
  forget           "Remove the current working (or specified) directory from Valet's list of paths"
  help             "Displays help for a command"
  install          "Install the Valet services"
  link             "Link the current working directory to Valet"
  links            "Display all of the registered Valet links"
  list             "Lists commands"
  open             "Open the site for the current (or specified) directory in your browser"
  park             "Register the current working (or specified) directory with Valet"
  paths            "Get all of the paths registered with Valet"
  port             "Get or set the port number used for Valet sites"
  restart          "Restart the Valet services"
  secure           "Secure the given domain with a trusted TLS certificate"
  secured          "Determine if the site is secured or not"
  share           " Generate a publicly accessible URL for your project"
  start            "Start the Valet services"
  status           "View Valet service status"
  stop             "Stop the Valet services"
  uninstall        "Uninstall the Valet services"
  unlink           "Remove the specified Valet link"
  unsecure         "Stop serving the given domain over HTTPS and remove the trusted TLS certificate"
  update           "Update Valet Linux and clean up cruft"
  which            "Determine which Valet driver serves the current working directory"
  

Domain

Lets check to see what the domain is currently set to.

valet domain

"test"

It looks like the domain is set to "test".

I will make a new folder called sites in my home directory and then register this directory with Valet.

mkdir sites
cd sites
valet park
"The [sites] directory has been added to Valet's paths."

Now that this folder had been registered or parked, any folders within this directory can be addressed by going to http://folder-name.test
Now if we create a new folder in this directory, for example, I will create a new folder called info and then create a file within that directory called index.php . Within the index.php file we can add the following content.

<php? echo  phpinfo(); ?>

Now if you open a browser and go to http://info.test, you should see a response with all of your PHP information. Pretty cool right.

What if you don't like the .test domain. Well, you can change it, but you do want to exercise some caution when choosing the domain. You will want to choose a name that is not a valid top-level domain or a least a rarely used one. For example, if you were to change Valet's domain to .com then any address that ends with .com will get proxied to your localhost, thus making it very difficult for you to communicate with a .com outside of your computer. In general, avoid using the obvious ones like, .com, .net, .io, etc... and you should be good. Being the super creative person that I am, I typically just set the domain name to valet.

valet domain valet
"Your Valet domain has been updated to [valet]."

Now that I have changed the domain to valet, I will no longer be able to reach our index.php file via http://info.test because it is now located at http://info.valet

Link and Unlink

You can also link folders to Valet. Linking differs from parking in the following way. When you park a folder, the top-level folders within the parked folder will be addressable by going to http://sub-folder-name.valet. On the other hand, Linked folders are addressable by http://linked-folder-name.valet. I think that makes sense? Additionally, you can specify an alternative name when linking a folder.

mkdir test
cd test
valet link

or

mkdir test
valet link test

or

mkdir test
cd test
valet link awesome

The first 2 examples will link the test folder to Valet and can be found at http://test.valet. The 3rd example will link the test folder to Valet and can be found at http://awesome.valet. I find this particular feature to be useful when working on projects that have a build process that outputs to a folder, as in a public or dist folder. you can link the output folder to valet and access the build project with ease.

To get a list of the folders linked to Valet, you can do the following.

valet links
"
+---------+-----+----------------------+------------------+
| Site    | SSL | URL                  | Path             |
+---------+-----+----------------------+------------------+
| awesome |     | http://awesome.valet | /home/jason/test |
+---------+-----+----------------------+------------------+
"

To unlink a folder you simply unlink it.

valet unlink awesome

"The [awesom] symbolic link has been removed."

Share

The last command that we will be looking at today is the share command. The share command lets you share a project temporarily with anyone over the Internet. Valet accomplishes this via ngrok. Which is itself a pretty cool service worth checking out. After you run the share command, the globally accessible URL will automatically be copied to your clipboard, and that's it. Your project will be globally accessible via the ngrok link until you kill the share command or the 8 hour time limit is exceeded.


valet share
"
ngrok by @inconshreveable                                         (Ctrl+C to quit)
                                                                                  
Session Status                online                                              
Session Expires               7 hours, 59 minutes                                 
Version                       2.2.8                                               
Region                        United States (us)                                  
Web Interface                 http://127.0.0.1:4040                               
Forwarding                    http://638d3f07.ngrok.io -> awesome.valet:80         
Forwarding                    https://638d3f07.ngrok.io -> awesome.valet:80        
                                                                                  
Connections                   ttl     opn     rt1     rt5     p50     p90         
                              0       0       0.00    0.00    0.00    0.00  
"

Well, I guess that is it for now. I hope you find Valet Linux to be as useful as I have, and encourage you to give it a try. Please let me know what you think in the comments, and don't forget to have fun.

Sort:  

Congratulations @luttrellbits! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :

You published your First Post
You made your First Vote
You got a First Vote

Click here to view your Board
If you no longer want to receive notifications, reply to this comment with the word STOP

Support SteemitBoard's project! Vote for its witness and get one more award!