Skip to content

Instantly share code, notes, and snippets.

@artistro08
Last active April 4, 2024 19:53
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save artistro08/5ab9ad7e43c000bb15c23a4f779e5449 to your computer and use it in GitHub Desktop.
Save artistro08/5ab9ad7e43c000bb15c23a4f779e5449 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.

@superern
Copy link

superern commented Jul 15, 2023

this works well for me.

I was using laragon before so it did set my hosts to use .test already.

all i just had to do is follow this!

If someone needs to know how to get the pem file. you can see it here.

image
basically this is how it's going to look like

image

@artistro08
Copy link
Author

Thanks @superern !

@AbiruzzamanMolla
Copy link

i am using this on wsl

but i am not understating this part

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

what to do now?

@artistro08
Copy link
Author

That's it actually. The setup is linked to the valet config

@AbiruzzamanMolla

@sohaibilyas
Copy link

valet install
[nginx] is not installed, installing it now via Apt... 🍻
Nginx was already enabled
Stopping nginx...
PHP Logs
Installing php config
Restarting php-fpm
Restarting php8.1-fpm...
[dnsmasq] is not installed, installing it now via Apt... 🍻
Dnsmasq was already enabled
[inotify-tools] is not installed, installing it now via Apt... 🍻
Installing Valet DNS service...
Valet-dns has been enabled
Systemd-resolved has been disabled
Systemd-resolved was already disabled
Stopping systemd-resolved...
Restarting network-manager...
Restarting dnsmasq...
Starting valet-dns...
Restarting nginx...
valet status
DomainException: Unable to determine PHP service name. in /home/sohaib/.config/composer/vendor/cpriego/valet-linux/cli/Valet/PhpFpm.php:418
Stack trace:
#0 /home/sohaib/.config/composer/vendor/cpriego/valet-linux/cli/Valet/PhpFpm.php(362): Valet\PhpFpm->fpmServiceName()
#1 /home/sohaib/.config/composer/vendor/cpriego/valet-linux/cli/includes/facades.php(28): Valet\PhpFpm->status()
#2 /home/sohaib/.config/composer/vendor/cpriego/valet-linux/cli/valet.php(146): Facade::__callStatic()
#3 [internal function]: Silly\Application->{closure}()
#4 /home/sohaib/.config/composer/vendor/php-di/invoker/src/Invoker.php(74): call_user_func_array()
#5 /home/sohaib/.config/composer/vendor/mnapoli/silly/src/Application.php(95): Invoker\Invoker->call()
#6 /home/sohaib/.config/composer/vendor/symfony/console/Command/Command.php(324): Silly\Application->Silly\{closure}()
#7 /home/sohaib/.config/composer/vendor/symfony/console/Application.php(1063): Symfony\Component\Console\Command\Command->run()
#8 /home/sohaib/.config/composer/vendor/symfony/console/Application.php(320): Symfony\Component\Console\Application->doRunCommand()
#9 /home/sohaib/.config/composer/vendor/symfony/console/Application.php(174): Symfony\Component\Console\Application->doRun()
#10 /home/sohaib/.config/composer/vendor/cpriego/valet-linux/cli/valet.php(408): Symfony\Component\Console\Application->run()
#11 {main} is stopped...
Nginx is stopped...```

valet install worked without any errors!

@artistro08
Copy link
Author

@sohaibilyas , Hmm that may be because valet is installed with php8.1. Not sure if it's compatible with installing but switching to it after installing with 7.4 works for me.

@sohaibilyas
Copy link

@artistro08 i tried with php7.4 too but same error!

@artistro08
Copy link
Author

@sohaibilyas Did you uninstall completely first? Sorry, forgot to mention that

@AbiruzzamanMolla
Copy link

image

@artistro08
Copy link
Author

@AbiruzzamanMolla not sure as to why your config is missing. I would contact the author to see if it's a bug with them

@MiloudiMohamed
Copy link

Can anyone show me what the default content of /etc/hosts (within wsl) is? I followed these steps on a fresh wsl (ubuntu 22.04) installation but nothing loads
valet status shows all services running

@artistro08
Copy link
Author

@MiloudiMohamed , while I can't show you a default /etc/hosts/, I think the best way is to install another version of Ubuntu 22.04. I know you can have multiple of the same, but if not, you can install the older version.

as for the issue for nothing loading, I had to restart the VM sometimes when it came to getting things to work. wsl --shutdown should do the trick there.

@MiloudiMohamed
Copy link

@artistro08, I had to do a fresh Windows 11 installation (unfortunately) in order to get this to work, my setup was messed up for some reason, but I'm happy I got this to work eventually.

About the .localhost domain, I used to use valet-wsl which is no longer maintained now, they used a tool called Acrylic DNS Proxy to get the .test working automatically, do you think it is possible to do the same for this one?

@artistro08
Copy link
Author

@MiloudiMohamed I've tried before to no avail. Since Acrylic uses the same port as WSL, WSL fails to start. but, it might work now with the new experimental WSL flags: https://devblogs.microsoft.com/commandline/windows-subsystem-for-linux-september-2023-update/

@MiloudiMohamed
Copy link

Good to know, Thanks!

I'll play around with it and in case I can make this work I'll edit my comment to include the steps to reproduce it in case anyone wants to do it.

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