The ULTIMATE guide to setup Windows Subsystem for Linux (WSL) with Ubuntu, Apache2, MySQL and PHP

By Dillon Smart · · · 2 Comments

Ubuntu

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?

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. […]

Was this helpful? Leave a comment!

This site uses Akismet to reduce spam. Learn how your comment data is processed.

How to Upload and Download files to and from a Linux server over SSH

Updated 16th August 2022

Its common place to need to upload and download files to and from a remote Linux server. To upload and download files from a remote Linux server you can use the scp (secure copy) command. To download a file you can use: To upload a file you can use: scp /path/to/file/your/uploading username@ip-or-server-name:/path/to/location/to/upload/to Note: It is

How to check Composer version – Install and use Composer for PHP

Updated 11th January 2024

Composer is a dependency manager for PHP. First released in 2012, Composer, and its adoption by popular frameworks such as Laravel has single-handedly driven the rise in PHP adoption in recent years. In this post, I will show you how to install and use Composer. How to install Composer To get started, download Composer from

SQL Union – What is a Union and how to combine two or more SQL Select queries

Updated 1st December 2023

The SQL UNION Operator is used to combine two or more SELECT statements together into one set of results. Things to know about SQL Union Operator To execute a SQL UNION statement successfully, you need to be away of the requirements of a UNION statement. Example using SQL UNION In our example we have 2