PHP Development with Windows Subsystem for Linux, version 2

PHP Development with Windows Subsystem for Linux, version 2

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

1. 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

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

3. Check existing WSL installations from Powershell:

wsl --list –verbose

4. Set the default version from Powershell:

wsl --set-default-version 2

5. Go to the Microsoft Store at https://aka.ms/wslstore and download your version of choice.

a. For example, Ubuntu 20.04 LTE can be found at: https://www.microsoft.com/store/apps/9n6svws3rx71.

b. Once done, run Ubuntu 20.04 from the Start Menu.

6. For extra features, install Windows Terminal from: https://docs.microsoft.com/en-us/windows/terminal/get-started.

a. From Windows Terminal, you can’t open a tab for your Linux distro, because directories such as /home and /var won’t be accessible.

b. Run ubuntu2004.exe once from the Powershell tab (the default) and you’ll be able to access those directories moving forward.

7. 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

1. On Ubuntu, run the following commands:

$ sudo apt update && sudo apt upgrade

$ sudo apt install apache2

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

3. Create a Virtual Host and enable it:

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

-------- 8< --------

<VirtualHost *:80>

    ServerName my.example

    ServerAlias www.my.example

    ServerAdmin [email protected]

    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>

-------- 8< --------

$ sudo a2ensite my.example

4. Create an example HTML page:

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

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

-------- 8< --------

<!DOCTYPE html>

<html>

    <head>

         <title>My Example</title>

    </head>

    <body>

         <h1>My Example</h1>

         <p>Hello World!</p>

    </body>

</html>

-------- 8< --------

5. Give the required access permissions:

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

6. Check with:

$ sudo apachectl configtest

7. Check the IP address that corresponds to localhost with:

$ ip a

8. 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.

9. Edit the Ubuntu hosts file:

$ sudo nano /etc/hosts

Adding:

127.0.0.1 my.example

10. And start the Apache service:

$ sudo service apache2 start

11. 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

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


要查看或添加评论,请登录

Luciano Cossich Sales的更多文章

社区洞察

其他会员也浏览了