Skip to main content

Lucoles LTDA

Full Stack Developer, Acquia Certified Drupal 10 and Drupal 7, ITIL, PSM

A step-by-step guide for PHP full stack web development under Windows with WSL2

Image
Zoomed in screen of the Apache webserver on Ubuntu, with the sentence "It works!" in white against a red stripe, followed by a technical explanation text, cropped.

Most PHP developers work from some flavour of Linux, but for those of you stuck with Windows, Microsoft has a native alternative to the ubiquitous VirtualBox - without taking up too much of your system's resources.

Such a solution can also come in handy if for some reason you're unable to run VirtualBox - say, for example, you're blocked from installing programs by the IT department of your company.

For those of you interested, or maybe (like me) curious to try it out, Microsoft has been developing a Linux-friendly environment since Windows 8: The WSL, or Windows Subsystem for Linux. Its current iteration, WSL 2, is available exclusively on Windows 10.

So without further ado, here's how you can get it to work:

Install WSL on Windows

Open Powershell as an administrator and issue the following commands:

PS> dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
PS> dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart

Download the Linux kernel update package from https://wslstorestorage.blob.core.windows.net/wslblob/wsl_update_x64.msi. It will require administrator privileges.

Check existing WSL installations from Powershell:

wsl --list –verbose

Set the default version from Powershell:

wsl --set-default-version 2

Go to the Microsoft Store and download your version of choice. For example, Ubuntu 20.04 LTE can be found at https://www.microsoft.com/store/apps/9n6svws3rx71. Once done, run Ubuntu 20.04 from the Start Menu.

For extra features, install Windows Terminal. From there, you can't open a tab for your Linux distro, because directories such as /home and /var won't be accessible. Run ubuntu2004.exe once from the Powershell tab (the default) and you'll be able to access those directories moving forward.

Should you need direct access to your files, you can do so over the network: \wsl$\Ubuntu\home.

Install Apache2 and setup a Virtual Host

On Ubuntu, run the following commands:

On Ubuntu, run the following commands:

$ sudo apt update && sudo apt upgrade
$ sudo apt install apache2

Browser access is done from localhost (yes, really).

Create a Virtual Host and enable it:

$ sudo nano /etc/apache2/sites-available/my.example.conf

With the contents:

<VirtualHost *:80>
    ServerName my.example
    ServerAlias www.my.example
    ServerAdmin webmaster@my.example
    DocumentRoot /var/www/my.example/public_html

    <Directory /var/www/my.example/public_html>
         Options -Indexes +FollowSymLinks
         AllowOverride All
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/my.example-error.log
    CustomLog ${APACHE_LOG_DIR}/my.example-access.log combined
</VirtualHost>

Then, issue:

$ sudo a2ensite my.example

Create an example HTML page:

$ sudo mkdir -p /var/www/my.example/public_html
$ sudo nano /var/www/my.example/public_html/index.html

With the contents:

<!DOCTYPE html>
<html>
    <head>
         <title>My Example</title>
    </head>
    <body>
         <h1>My Example</h1>
         <p>Hello World!</p>
    </body>
</html>

Give the required access permissions:

$ sudo chown -R $USER:$USER /var/www/my.example/

Check with:

$ sudo apachectl configtest

Check the IP address that corresponds to localhost with:

$ ip a

Under "eth0:" look for a line like this:

inet x.x.x.x/xx brd y.y.y.y scope global eth0

Write down the value of x.x.x.x.

Edit the Ubuntu hosts file:

$ sudo nano /etc/hosts

Adding:

127.0.0.1 my.example

And start the Apache service:

$ sudo service apache2 start

Edit the Windows hosts file (administrator privileges needed) and include the IP you discovered from the previous step:

C:\Windows\System32\drivers\etc\hosts

Adding:

x.x.x.x my.example

Finally, open your browser and visit http://my.example.


Originally published at https://linkedin.com on January 8, 2021.