I had cobbled together a guide awhile back on how to do this, a bit has changed and some of the guide was inaccurate or had unneeded steps.
you will need to be up to date with windows 10, older versions of windows 10 does not have WSL. Make sure you windows is fully up to date, and you are comfortable with working with the command line or wish to learn more about the command line. other than that, enjoy the guide!
So what is WSL?
Well, basically it's the userspace from a linux distro added to win10 to allow you to run native linux apps (currently only CLI, some have gotten GUI stuff to work, but that is beyond the scope of this guide).
Who is it for?
Anyone really. I am a web developer, and one of the reasons i used linux for over 10 years as my daily driver, was due to how poorly a wamp stack performs compared to a lamp stack, and the differences you will encounter in how the two platforms operate. example: windows is a case insensitive OS, so windows doesn't see a difference between MyClass.php and myclass.php, this could be a problem with a case unaware autloloader on linux or mac os x. so it could lead to a unnecessary refactor if/when you deployed to linux/mac os x.
I like this alternative mainly cause I migrated to a UHD monitor and use lower resolution monitors in the setup as well and linux has extremely poor fractional scaling (with the exception of KDE), windows scaling isn't perfect, but it is a lot better than any of the linux DE's atm. there were also other issues i ran into as well, as i recently also built a AMD Ryzen system, and ryzen's performance in linux isn't stellar, so least to say, Linux has several issues right now, that make it difficult for me to use well.
So why should I use this over wamp or a linux setup?
Don't get me wrong, apps likes WAMP/XAMP/MAMP are fine applications and if you are happy with those, then you shouldn't rock the boat. I do a lot of work outside of php with languages like python and technologies like node.js that don't perform very well in windows, they are actually quite cumbersome to install and to use in windows.
So if you do more than just write wordpress plugins i suppose, then you'd prolly don't need this, but if you are curious how much greener the grass is, continue to read.
so why should you use WSL over a full lamp stack? well the simple answers for me would be convenience. like the aforementioned issues i had running linux now days, this is sorta a meld of "best of both worlds".
Okay enough backstory, lets get started. First thing you will need to do is enable the sub system, you can do this a few ways. the fastest way imho is to start up a powershell in administrator mode (click start, type in powershell, when it comes up in cortana, right click on it and choose to run as administrator. if you have UAC enabled, it might prompt you if you wish to continue):
Once it open enter this: (note: you can copy it, either right click on the powershell window or ctrl+v to paste it)
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux
Press enter, it will do a few things, and then it will prompt you to restart. to continue you need to restart (did you really expect anything else? it is windows after all )
Now the Fun!
Now once you are back after the reboot, you should be ready to install a distro. Now microsoft made this really simple, they put them in the app store. so open up the app store and search for 'linux':
Click on "Get the apps" (if you don't see this, if you scroll down, under APPS, you will see the distro list). you should see this:
There are a few distro's to choose from here. This guide will use Ubuntu, but in reality there really isn't any other choice IMHO. (i get this one a lot, "mike, how are you a ubuntu user? its for noobs and you know so much about linux!?", boggles the mind doesn't it? not really, Ubuntu is still more or less debian under the hood, it just has more recent packages and i think put together a bit better than debian and the other distro's, but any way, please refrain from ubuntu hate) any way, click on ubuntu:
Now decision time, this guide will proceed almost identically regardless of your choice here. you can click install here and it will install ubuntu 16.04 LTS, however if you look below, you should see "people also like" and one of the options there, should be ubuntu 18.04 LTS. I highly recommend ubuntu 18.04 LTS as it is newer, and will be supported longer than 16.04 LTS, but the choice is yours.
Let it install, once it is install click open. you will be greeted by a bash prompt, it will ask you for a username and a password (It should be noted as this has driven linux noobs insane, when you type in your password on the CLI, it will never indicate anything is being typed, so if you are expecting something like ****** to show up, it wont. ). Once you are done with that you will see a prompt like this:
well it wont look exactly like this, but you can make it look like this here
alright we have Ubuntu installed, first thing we are going to do is add this ppa (which will have the several versions of php available and updates for them when they come out), enter this into your bash prompt:
sudo add-apt-repository ppa:ondrej/php
this will add the ppa, next we are going to add the webmin Repo for ubuntu, next enter this command:
sudo nano /etc/apt/sources.list
this will open the nano editor, using your arrow keys, go all the way to the bottom and add this on a new line (you can copy from here and use right click to 'paste')
deb http://download.webmin.com/download/repository sarge contrib
to save, ctrl+o (saves) and then ctrl+x (exits), next enter these commands:
sudo apt-key add jcameron-key.asc
sudo apt update
(this last command will update the package managers database).
Alright if you have made it this far, you have completed all the EASY stuff (yes, you have permission to roll your eyes). now the stuff that follows, we will be installing and configure Ubuntu with a LAMP stack, plus with webmin.
alright, lets install everything we will need:
sudo apt install apache2 mysql-server php7.2 php7.2-xml php7.2-zip php7.2-mbstring php7.2-curl php7.2-mysql php7.2-gd php7.2-imagick curl webmin
this can take awhile, webmin takes awhile to install, so go watch a show or play solitaire . once it is done, it will return the prompt. so everything is installed, so next we need to start everything, in bash, enter this:
sudo service apache2 start && sudo service mysql start && sudo service webmin start
(it will prompt you for your user password). it should start up all the services, you can check in your browser by going to http://localhost and you should see something like this:
now check if webmin is running, it will be the computers name (the part in bash prompt after your username) on port 10000, so it would be something like this:
(don't log into it just yet, just make sure it is running, you should see a login prompt). you will be told the site is unsafe cause it doesn't have a cert, you can make an exception or edit webmin's config to change this:
sudo nano /etc/webmin/miniserv.conf
and change it to:
now save and exit (ctrl+o then ctrl+x).
sudo service webmin restart
(sometimes this doesn't work, and you need restart your computer, i am not sure why it does this, if you restart, don't forget to start up all the services again when you open up bash).
if you installed ubuntu 18.04 or installed mysql-server 5.7+, you will need to do this:
In mysql 5.7, you are no longer able to use root to access your database, so you will need to setup another user to do this, in bash:
sudo mysql -u root -p
it will prompt you for the mysql root password, which at this point should be empty, so just hit enter, if that fails, enter your user password. this will enter the mysql prompt, do the follow, making changes where it says username and MYPASSWORDGOESHERE:
GRANT ALL PRIVILEGES ON *.* TO 'username'@'localhost' IDENTIFIED BY 'MYPASSWORDGOESHERE';
hit enter, next:
hit enter, and finally:
hit enter, it will return you to the prompt.
optional (you can instead choose to create a vhost to where you want your document root to be): next we will link a folder from our C drive in windows to a /var/www/html in ubuntu (as you can't edit files on the linux fs with windows editors, something about permissions). now in WSL any of your drives are auto-mounted in /mnt, your C:\ drive will be /mnt/c and any other drive will be mounted under their drive letter. we will be assuming your document root for your web app is C:\myapp, so we need to get this linked to your servers document root folder, which is /var/www/html. to do this, we will create a symlink:
sudo ln -s /mnt/c/myapp /var/www/html
this will now be accessible via http://localhost/myapp/
okay, that it is it. apache is running, webmin is running, and everything is installed to get your local dev environment running. this isn't the end of the guide, just if you want, you can stop here and go about configuring the rest on your own. however if you want to do some more things, keep reading.
Start With Bash:
I like adding stuff to my .bashrc, so when i start the bash prompt, everything starts up for me (after enter my password, but you can set it up where sudo doesn't require a password, but use at your own peril).
in the bash prompt:
using the arrow keys, go to the end and start a new line. add the following, each on their own line:
sudo service apache2 start sudo service mysql start sudo service cron start sudo service webmin start
save and exit (ctrl+o and then ctrl+x)
i like adding cron here, as there are times i need to test out task, and setting a cron is a lot easier than trying to figure out the voodoo magic that is windows scheduler. but when you first open up bash, you will be prompted for your password, and then the services will start.
I really like this module, allows me to work from my home directory, from a folder called public_html and accessible from http://localhost/~username/. first thing to do is enable the module for apache:
sudo a2enmod userdir
now we need to tell php it can parse php in the home dir:
sudo nano /etc/apache2/mods-available/php7.2.conf
php_admin_flag engine off
comment it out, so make it look like this:
#php_admin_flag engine off
save and exit (ctrl+o then ctrl+x).
sudo service apache2 restart
create the public_html folder in your home directory:
takes you back to your home dir.
creates a folder called public_html in your home dir.
ln -s /mnt/c/webapp ~/public_html
will link your webapp folder from your C:\ drive to ~/public_html/webapp. you will be able to access it via url: http://localhost/~username/webapp.
as you can see, there is potentially a lot of work that goes into setting up WSL for a LAMP stack. you might think it isn't worth it, and it might not be for some, but for others it will be, as it will open up more possibilities, with the technology and applications working in a native environment.
btw...why did you have us install webmin, you didn't do anything with it!
you are right, i didn't use it in this guide. you can log into it now, and you can configure your webserver from there. i wanted to show you how it is done on the CLI, to get an appreciation for the software, but with webmin, it will allow you to do configuration with a GUI for apache and php. so if you don't see any need for webmin and are comfortable with the command line editors, then you can uninstall it or not install it to begin with.
if you have any questions or comments, let me know below!
Edited by Michael