Skip to content

Instantly share code, notes, and snippets.

@kevyworks
Forked from artistro08/README.MD
Created May 19, 2024 04:52
Show Gist options
  • Save kevyworks/0142512a07f43e32d7878a1d62dbbc1a to your computer and use it in GitHub Desktop.
Save kevyworks/0142512a07f43e32d7878a1d62dbbc1a to your computer and use it in GitHub Desktop.
How to setup a LEMP Development Environment with WSL2 & Valet Linux

How to setup a LEMP Development Environment with WSL2 & Valet Linux

FYI

This tutorial is based on a clean Ubuntu-22.04 install. Use at your own risk.

Prerequisites

Setup

First thing you'll want to do is install PHP and some dependencies. Install 7.4 first (to be able to switch if you need to)

sudo apt install unzip php php-cli php-fpm php-json php-intl php-common php-mysql php-zip php-gd php-mbstring php-curl php-xml php-pear php-bcmath 

Second thing you'll need to do is install composer:

curl -sS https://getcomposer.org/installer | php \
            && sudo mv composer.phar /usr/local/bin/ \
            && sudo ln -s /usr/local/bin/composer.phar /usr/local/bin/composer

Next, you'll need to install dependencies for Valet Linux

sudo apt-get install network-manager libnss3-tools jq xsel

After you have all that done, we can install Valet Linux:

composer global require cpriego/valet-linux

Once you install valet linux, you'll need to update your PATH for your environment to run the valet commands. In your .bashrc file (located in your home directory), add this line to the bottom of your file (or wherever you prefer in the file):

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

You'll also need to reload the bash shell after adding this. Just close the terminal or run the following command:

source ~/.bashrc

Installing Valet Linux

Next, we can install and setup Valet. To install Valet Linux, run the following command:

valet install

We also need to set a directory to serve the sites. To do this, I usually just put a .sites folder in my home directory and serve them from there:

mkdir ~/.sites && cd ~/.sites

valet park

Valet is now installed and initially setup, but now we have a few more steps to get the rest working.

Domain Setup

Typically valet uses the .test domain prefix for you to access websites on your local development environment. In order for the .test domain to work, you'll need to add the domain to your Windows hosts file under C:\Windows\System32\drivers\etc. This however is a pain and not ideal since Valet on Mac usually just works out of the box.

To fix this, We can change the domain ending in valet from .test to .localhost. Since all browsers loopback .localhost to 127.0.0.1, this resolves everything effortlessly. To do this, run the following command:

valet domain localhost

There's also some extra stuff you'll need to do as well if you want to forward listening services to the setup. First, we will have to turn off the recreation of the /etc/resolv.conf. To do so, edit your /etc/wsl.conf and add the following lines

[network]
generateResolvConf=false

Next, we will need to unlink the /etc/resolv.conf and copy Valet's config over:

sudo unlink /etc/resolv.conf
sudo cp /opt/valet-linux/valet-dns /etc/resolv.conf

SSL Setup (Optional)

By default, Valet Linux installs a certificate to the system allowing you to use the https secure protocol (valet secure <folder directory>) without Chrome or any other browser nagging you in the address bar and before you visit the site. This however doesn't work and you'll need to install the certificate manually.

To do so, You'll need to pull the certificate file from the Valet Linux installation and install it manually. To do that, Navigate to ~/.valet/CA in a terminal, open it in explorer by running explorer.exe . and import the LaravelValetCASelfSigned.pem into your certificate settings. Images of the location below for reference (provided by @superern)

image image

To get your your Certificate Settings, In Chrome, Go to your settings. Search for certificates, then click Security. Scroll down and click on Manage Certificates. This should open a new window. In the tabs of the window, click Trusted Root Certification, then click "Import...". Click "Next" then, "Browse" and navigate to the directory you opened. To see the LaravelValetCASelfSigned.pem, on the bottom right, select the dropdown by the "Open" and "Cancel" buttons and select "All Files (.)". Select the LaravelValetCASelfSigned.pem then click "Open". Click "Next" then "Finish". SSL should be setup and you'll be able to browse SSL enabled sites served by Valet Linux.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment