Developing on Windows with WSL2 (Subsystem for Linux) and PHP

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi i'm adam kolp and you're at beachcast today we're going to be talking about developing on windows with wsl and a full lamp stack so stick around and we'll get right on that [Music] i've been developing web applications for a very long time and one of the most effective ways to be able to create something really fast is through having a local development environment well recently as some of you may know i switched from using linux as my primary desktop to using windows there were multiple reasons why i did that but i'm not going to talk about those on this video let's just talk about the development environment so with the development environment one of the easiest ways to create a linux like development environment on windows is using wsl2 now i've created some wsl videos previously and i'll link to those up above so check those out if you need to get a wsl environment up and running or if you want to incorporate docker into your wsl this video is not going to cover that i'm going to continue as if you've already watched those videos or if you already have a wsl environment set up on your windows desktop laptop server or what have you so some of the prerequisites for this video is that you already have wsl2 installed or understand it so that way you can follow along from my desktop i'm going to go ahead and launch the instance of ubuntu that i want to work with for this video now in this case i'm going to select ubuntu 1804 now i do have two versions of ubuntu installed on this system i have 2004 but for the sake of this video i've installed a second instance of ubuntu running 1804 and that's the instance that i'm going to use so we can see here that i am in that instance and um if i look at my present working directory i'm in my home directory now this is a fresh instance of ubuntu i've done nothing on this yet but so i do need to get it set up one of the first things that i want to do for setting up a development environment locally is installing apache i use apache as my web server so let me go ahead and do that so i'm going to type in sudo apt-get install and i'm going to install apache 2. and it's going to prompt me for my sudo password so i'm going to go ahead and enter that if it's asked if i'm sure i want to install it and of course i am so i'm going to type a y for yes and then let it go through and do the install okay apache has been installed so now we're on to the next step now for this example i'm going to go ahead and create an install of php now php still runs a large portion of the internet and i would imagine there's a lot of people creating development environments in php so that's why i'm going to use it so i'm going to install php the same way i did with apache sudo apt-get install php and it's saying it's telling me what it's going to install in this case php 7.2 because i'm using ubuntu 1804. uh i would get a newer version if i was in on on my installation of 2004 but again for this example i'm going to use i'm going to use this so i'm going to hit yes and it'll start installing and get php installed for us and ready for use okay so the install has finished we now have apache we now have php and i want to verify that so i'm going to go ahead and type in php hyphen i which will bring up the information for php and i did get the information for php i could also do hyphen v and that would give me the version of php that's installed so i know i have php installed i know i have apache installed i can verify that as well if i open up my browser and go to localhost and hit enter and it brings up the default apache page so apache is working php is also installed i haven't verified that php is working with apache yet but that's okay we'll come to that the next thing that i typically do in my development environments is i create a directory on the host machine meaning my windows machine where that's that's where i'm actually going to develop the application i don't develop the application inside ubuntu i develop it on my host machine and but then i link to it in the ubuntu so that way it uses it in conjunction with the web server so to do that what i'm going to do is i bring up my f drive my f drive is a secondary drive on this system i'm going to create a new folder and we're going to call that folder projects and so i'm going to create a projects folder and that folder is where i'm going to house my projects right now there's nothing there that is where i'm going to house my projects and what i want to do in the in the ubuntu machine now is i want to link to that projects on the host machine so i'm going to flip over to the ubuntu command line and in looking at that right now my present working directory is slash home slash adam kulp i want to create a symbolic link from this directory to that map drive now if we experiment a little bit if i if i list the contents of slash mnt on in this ubuntu instance i see that there is a c and there is an f and there's also wsl i because i created that file on my f drive i now want to map through using that f drive so that's what i'm going to do i'm going to create a link so i'm going to create uh in in my current working directory of slash home slash adam call i'm going to create a link and i want it to be symbolic and i'm going to link to mnt slash f slash projects i want to link that to a project in this directory so i'm going to go ahead and type in projects in my current working directory typing that in now it created that symbolic link if i hit l we can see that i do in fact have this projects folder and it is linked through to that f drive on the host machine so now i can use that for all of my developments and that's the idea now over here if i create for instance i create a new file new file let's create a new text document and let's call this index.php okay it's asking me if i'm sure i want to rename it and i do so i've created that index.php there if i go over here now and i change directories into projects and list we can see that i do have the index.php so it has been created and it's mapped i can see it here if i if i edit this file i can use vi for instance and edit index.php and editing this what i'm going to do is i'm going to create some php and i'll just type in echo hello world just like that and save that file and so now i've got that index.php but it's not really accessible right now although i've created the directory and i've got it there and that's where i'm going to put all my projects and i've got my index.php there what i have not done yet is tell apache where to find it and of course you need apache to be able to find your projects so that's what i'm going to do next to do this on in a development environment such as this i usually activate the extension for apache called user dir that's a user directory short for user directory so to activate that i'm going to type in let me let me clear here i'm going to type in sudo uh a to n mod that's apache 2 enable mod and type in user dir and so now it's enabled user dir however it is not activated for apache until i restart apache so typing sudo service uh yeah apache 2 restart so i'm going to restart the service apache and i've done that so now user dir is active however it's not going to serve up that file that i created just yet right now right now the user dir by default is looking for a subdirectory in my home directory called public html i didn't create public html instead i created projects um because i'm going to house multiple projects there not just one if i was housing only one project in there then i would go with the default of just having public html i can show you that if i type in sudo vi i'm going to use vi to edit the file and i'm going to be looking at etsy apache 2 mods available and we want to look at user dir and the config file for user dir so if we edit that here we can see the base setups by default it set up the user dir to be it set up the user dirt to be public html and as i said i don't want to use public html instead i want it to be projects so i'm going to go ahead and rename that to projects now down here in the directory it also shows that it's using public html so i'm going to edit that as well now you'll notice here that the directory this is just uh this is a vanilla apache it's it's slash home slash and the asterisk is the wild card meaning that it's expecting input into that so the wild card would be in my case adam kolp so we're going to put in projects here for development environments i'm going to go ahead and open this up a little bit i'm going to put allow overrides all the other thing that i want to do is this require statement i need to give it permissions to allow the things to be served there so i'm going to put require all granted just like that so now we've got require all granted we've got allow all overrides we've defined our directory and we've also told userdur to use the projects as the directory so i'm going to go ahead and save this now and once again i'm going to go ahead and restart apache i don't know that i need to do that but i'm going to do it in any way so now if i come back here where i have localhost typed if i put slash and put adamkulp slash and in here i don't need to put the projects directory because it should know that already so if i just hit enter here we can see that i do get my hello world as expected because that's the index.php that i created there so i am getting into the project's directory now we're ready to use that for our web development because i'm using php for my local development environment i also use composer a lot with php if you're you if you're doing php applications chances are you're using composer as well it is the dependency manager for php now i need to get that installed and how i'm going to do that is i'm going to go to dot getcomposer.org okay so from the composer page i'm going to click on download i want to download it and it gives four lines here that we need to put into the command line so that way it will download the composer executable if you will and then we'll be ready to to use it so i'm gonna go ahead and copy that go over here to my linux prompt and paste that in and now it is downloaded composer in the in the current directory and if i if i do a list we can see i have a composer.far right here and that is the latest version of composer however i want to use composer globally especially in a development environment i don't want it i want to don't want to have to call it from within this directory all the time i would rather it just be able to call it by typing composer at the command line so now i want to relocate this and in the composer website if i look here it also gives additional instructions on how to install globally so if i go to install globally i can see it's telling me to move composer.far to user local bin composer and in doing that will then make it available globally because it's putting it in my path in my user local bin folder so i'm going to go ahead and just copy this command as it is here go back to my terminal and paste it in here and of course i can't can't do it because i need to put sudo first it has to be a super user in order to put it in user local bin so doing so now it is taking that file if i if i list the contents of the directory we see the composer.far is not there anymore it has moved it to user local bin composer at this point if i type in composer at from command line now we're getting prompted by composer of what command do you want to execute okay so with composer installed there are two other packages that i want to pull down that make life a little bit easier i'm going to type in sudo apt get install and we're going to do php hyphen dom just like that hit enter it's prompting me for password there we go now it's installing that and then we will have php dom installed now in addition to that another thing that i want to install for composer's sake is the unzip extension unzip doesn't come installed by by default and ubuntu but as composer is downloading the packages it works a little bit faster if it's able to download the compressed version and then uncompress it once it's downloaded it the alternative would be composer would download the entire package a file at a time instead of an archive and doing it as an archive is is a much much better approach downloading things so i'm also going to install that so we're going to do sudo let me click here we're going to do sudo apt-get install unzip now i'm ready to install a sample application now i am i am in my current working directory my home directory slash adam kulp projects and in this directory right now all i have is the index.php in php one of the most popular frameworks today is the laravel framework so i'm going to go ahead and install laravel as a sample application for the sake of this video now to do that i'm going to use composer so i'm going to issue the command composer and i'm going to do create project great project i'm going to tell it to prefer this and by passing that flag it tells it to pull the one of the latest version of uh the of the files or of the archive rather than rather than downloading just the current master branch or main branch as the case may be so we're going to do laravel laravel and i'm going to have it put that in a directory in this location called sample site hit enter now it's going to reach out to the package manager composer and pull down the latest packages i just asked for okay so after a few minutes it's completed the download and installation of uh laravel locally we can see here on on the screen it's already gone through and discovered any packages used in artisan already done uh key generation as well and so artisan is also working as part of this install of of laravel now if i come over to my browser and from adam kopp if i type in the directory that i just created which is sample site sample site and of course i want to go into the public directory of this location we see that i do in fact have laravel up and working right now now one thing to note is when you're developing in this way when you're using wsl2 as a linux distribution and you're mapping to files on your host machine you will discover some slowness that's to be expected linux is working on a virtual machine within windows and wsl2 makes adds a lot of benefits to it there's a lot of nice things going on there but they still haven't completely eliminated the slowness when you're using files from the host machine within the vm it's not as slow as using vms in other ways though i've experienced much more slowness using things like virtualbox or vmware versus using wsl2 now wsl 2 also you can install it and use hyper-v but hyper-v is not faster hyper-v is actually slower than using wsl version 2. so just know that going in so as as we do this as i'm developing uh locally and i'm waiting for i save changes and then i expect to execute it in case of the website here there is going to be a second lag from the time that you refresh the browser till the page is rendering also notice here that i'm actually using that user der extension in apache to render this site in my browser i'm using the the address localhost slash and in my home directory item called slash sample site slash public you can also add a virtual host to apache and make it just a simple domain name like mysite.com or dot dev or whatever you want to do locally you can also use php's built-in web server i could execute php's built-in web server here and use that to serve the page on an alternative port as i did the laravel install it did take a few minutes for it to install laravel laravel has a lot of dependencies to be the full-fledged framework that it is and through that it does take a little while to install alternatively if i was in a hurry to get laravel installed i could have just done it through the the windows terminal whether it's powershell or the command prompt and installed it that way if i had php and composer installed on my windows environment as well as in wsl some added benefits of being able to do this as well let's say for instance in my projects directory if i change directory into the sample site directory we can see that i have the laravel site here if i were to type in code and then just the dot meaning the current work and directory this will execute from within the virtual machine it will open up vs code on my windows machine using the current working directory in on in the vm and and that in turn opens up vs code in the current working directory and we can see here i have the level app already opened up uh straight within that machine so i've got the environment already working if i wanted to use vs code alternatively if i wanted to use something like phpstorm then i would open up phpstorm tell it where the current working directory is on the host machine and create my development environment as normal with phpstorm it's kind of nice having the extra vs code ease of being able to edit if you wanted to do a sample application or some type of proof of concept and you needed to open it up quickly from within wsl it is very easy to do code and then dot and have it open up the editor straight from there another thing i could do is from within wsl is i could type explorer explorer.exe and do the dot for the current working directory and that would open up windows explorer in the current working directory and give me access to the files using windows explorer as well so just some added functionality that you get in the connectivity between wsl and windows very handy i hope you found this useful if you like this video please give a like and so other folks know if you have any comments if you do your development environment in a different way please leave a comment down below let me know let me know how you do things a little bit differently with wsl wsl2 and how you run your environments a little bit differently if you found a way to make things run faster that would be awesome leave that comment down there as well and above all take care be good to yourself and others i look forward to see you in the next video youtube is going to show you some videos up here on the sides that you might be interested in watching next i look forward to talking with you next time
Info
Channel: Beachcasts Programming Videos
Views: 846
Rating: undefined out of 5
Keywords: developing on windows with wsl2, lamp stack on wsl 2, wsl 2 local dev environment, lamp stack on wsl, local dev environment wsl 2, wsl php install, install laravel on wsl, wsl laravel, wsl2 php, wsl2 lamp stack, laravel on wsl2, windows subsystem for linux, php programming videos, beachcasts, adam culp, developing php on windows, install laravel on wsl 2, wsl2 development environment, laravel development on windows 10, development environment wsl 2
Id: _H1iKXksjF0
Channel Id: undefined
Length: 22min 15sec (1335 seconds)
Published: Tue Oct 26 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.