The ULTIMATE guide to setup Windows Subsystem for Linux (WSL) with Ubuntu, Apache2, MySQL and PHP
By Dillon Smart · · · 2 Comments
Windows is now good for development! Yes! All thanks to Windows Subsystem for Linux (WSL). No more WAMP or Xampp, WSL can do everything you need. In this ultimate guide to setup Windows Subsystem for Linux with Ubuntu, Apache2, MySQL, and PHP we will cover:
- What is Windows Subsystem for Linux?
- Setting up Windows Subsystem for Linux with Ubuntu
- Installing a LAMP server (Linux, Apache2, MySQL, PHP)
What is Windows Subsystem for Linux?
Windows Subsystem for Linux is a compatibility layer for running Linux binary executables on Windows 10. WSL was created by Microsoft for developers and allows those who need to use Bash, common Linux tools, and other Linux-first tools on Windows.
WSL provides a terminal running Bash shell, where Linux commands and tools can be executed. Packages and applications can be installed just as they would on any Linux machine, using package managers such as APT.
From the terminal, users can access both the Linux file system and the Windows file system, which can be found in the /mnt directory.
Setting up Windows Subsystem for Linux with Ubuntu
First things first, we need to enable WSL on our machine. Open PowerShell as administrator and run:
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux
Now its time to install the OS you want to run. In this example we will install Ubuntu from the Windows Store, however, you can install other distros directly from the Windows store.
In the Windows Store, search for Ubuntu, and once installed, open it. A console window will open which will initialize the instillation. This may take a few minutes. After installation has been completed, you will be asked to set the default user and password.
Once completed, search in the Cortana search bar for Bash.exe. You will be straight into the user directory.
Alternatively, you can download Windows Terminal, from which you can open Ubuntu.
Install a LAMP server (Linux, Apache2, MySQL, PHP)
For our server, we will be using Apache2 and MySQL. Before we install lamp-server we need to run the following:
sudo apt-get update && sudo apt-get upgrade
Then run:
sudo apt-get install lamp-server^
Now we need to make some changes to the apache2.conf file. To open and edit the file run:
nano /etc/apache2/apache2.conf
You shouldn’t need to use sudo for the moment, however, if the above doesn’t work then append the command with sudo.
Now add the following 2 lines to the file.
Servername localhost AcceptFilter http none
Now start Apache2 using the following command:
sudo service apache2 start
To start MySQL run:
sudo mysql start
Linking your directory in windows with your server directory
Now that we have the basics set up and running, we can link our working directory within Windows with your WSL Apache server. If you have used different local environment setups then this step will be familiar to you, but what this does is take a directory where you have all your projects within Windows, and makes them accessible to your server directory (linking the directories).
Example:
Create a directory within your User/Documents folder named projects (notice lower case).
Next, open your terminal or Bash.exe and run the following:
cd / sudo ln -s /mnt/c/Users/windows-user-name-here/Documents/projects /var/www/html
All done! Now you can create new directories and files within your projects folder, make changes to files as you usually would using your favorite IDE or text editor, and run them in your browser by visiting localhost, all of which is severed from your Apache2 server on WSL.
Apache2LinuxMySQLPHPUbuntuWindowsWindows Subsystem for Linux
2 Comments
IKnowThatNow
[…] For help setting up WSL for web developers, check out The ULTIMATE guide to setup Windows Subsystem for Linux (WSL) with Ubuntu, Apache2, MySQL and PHP […]
I Know That Now
[…] In this post we will show you how to change PHP version on Ubuntu, Linux. This also works on WSL. Follow this guide to setup Windows Sub System for Linux. […]