Setup PHP LAMP stack in Windows 10 With WSL

Published: Updated:

First you will need to enable"Windows Subsystem for Linux", Open PowerShell as Administrator and run:

Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux

After that restart your computer. Now that the WSL is enabled go to Microsoft store and choose whatever Linux distro you like. I had choose debian, as I used it in production as well.

  1. Check if any updates exists

    sudo apt-get update
  2. Install updates

    sudo apt update
  3. Install apache2

    sudo apt install apache2
  4. Enable mod_rewrite

    sudo a2enmod rewrite
  5. Restart apache

    sudo service apache2 restart
  6. Install php7.2

    sudo apt-get install php7.2
  7. Actually was installed PHP 7.3 as well and setup as default version. To check PHP version:

    php -v
  8. Mounting the development folder. On WSL C driver is mounting /mnt/c/ and D driver is /mnt/d/.So in my case I have the working folder on D:LocalDevelopment, so the commands will be:

    ln -s /mnt/d/localdevelopment /var/www/localdevelopment
  9. PHP to show error messages
    backup php.ini and replace it with the development version.

    cd /etc/php/7.3/apache2
    sudo cp php.ini php-bk-ini
    sudo cp /usr/lib/php/7.3/php.ini-development  /etc/php/7.3/apache2/php.ini
  10. install debug extension

    sudo apt-get install php-xdebug
  11. install php-dom extension

    sudo apt-get install php7.3-dom
  12. Verify which extensions are installed:

    php7.3 -m | head
  13. Installed the extension needed:

    apt install php7.3-bcmath php7.3-bz2 php7.3-curl php7.3-gd php7.3-intl php7.3-json php7.3-mbstring php7.3-readline php7.3-xml php7.3-zip php7.3-mysql
  14. In php ini change:

    max_execution_time = 240
    memory_limit = 512
    alow_max_filesize=128M
    allow_url_include on

    sudo nano /etc/php/7.3/apache2/php.ini

Close the file and save changes.

Restart Apache:

sudo service apache2 restart

Check that phpinfo.php now shows an Xdebug section. Then edit xdebug.ini by running

 sudo nano /etc/php/7.2/mods-available/xdebug.ini

and add the following:

[XDebug]
xdebug.remote_enable = 1
xdebug.remote_autostart = 1

Restart Apache again and XDebug is ready to go.

sudo service apache2 restart

Configure VS code for xdebugg.
For me to have this work, I’ve install VS Code Insider and Remote-WSL extensions.
Open a php file, go to Debug/Open cofiguration and this will open launch.json and add
“pathMappings”: {“/mnt/c/”: “c:/”,}

Will need to configure VS code to use Linux. Select your default terminal by pressing F1 in VS Code and typing/selecting Terminal: Select Default Shell and choose WSL bash.

Visual Code Ediitor screenshot

I have installed PHP Intelephense to get relevant code suggestions and PHP debug for debugging.

Restart VS Code

Setting up virtual hosts in Apache2. The conficuration file is available in /etc/apache2/sites-available. By default, there is one site available called 000-default.We can use this file as a starting point to create the configurations for sites we wanted/

   sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/mysite.conf

Then we can edit the config site

   sudo nano /etc/apache2/sites-available/mysite.conf

and add

ServerAdmin webmaster@localhost

ServerAdmin webmaster@localhost
DocumentRoot /var/www/html/mysite

Options FollowSymLinks
AllowOverride None

ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory “/usr/lib/cgi-bin”>
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all

Is a good idea to have separate logs for each website, so simple change the Error log line in the above file with the path /var/www/html/mysite/error.log

Save the file. Now we need to enable the website:

bash sudo a2ensite mysite.conf

And again, have to restart apache to load the new configutrations.

  sudo service apache2 restart

The next step is to add the local domains in Windows host file. This is located in C:\Windows\System32\drivers\etc\hosts, edit as an administrator and add the following line:

127.0.0.1 mysite.com

Install mariadb server:

  sudo apt-get install mariadb-server

When the installation is complete, run a simple security script that comes pre-installed with MariaDB which will remove some insecure default settings and lock down access to your database system.

sudo mysql_secure_installation

This will take you through a series of prompts where you can make some changes to your MariaDB installation’s security options. The first prompt will ask you to enter the current database root password. Because you just installed MariaDB and haven’t made any configuration changes yet, this password will be blank, so just press ENTER at the prompt.
The next prompt asks you whether you’d like to set up a database root password. Type N and then press ENTER. In Debian, the root account for MariaDB is tied closely to automated system maintenance, so we should not change the configured authentication methods for that account.
From there, you can press Y and then ENTER to accept the defaults for all the subsequent questions. This will remove some anonymous users and the test database, disable remote root logins, and load these new rules so that MariaDB immediately respects the changes you have made.
To start mariadb:

sudo service mysql start

Apart from those is fixing issues if you run: mysql -uroot -p as will get:
ERROR 1698 (28000): Access denied for user ‘root’@‘localhost’
Is recommended to not use root, instead create a new mysql user and grant all options

GRANT ALL ON *.* TO 'admin'@'localhost' IDENTIFIED BY 'your pass' WITH GRANT OPTION;

Install nodejs:

sudo apt install nodejs

Install npm:

sudo apt install npm

Verifying npm version, I get te error

  npm --version
  not foundram Files (x86)/nodejs/npm: 3: /mnt/c/Program Files (x86)/nodejs/npm:
  : not foundram Files (x86)/nodejs/npm: 5: /mnt/c/Program Files (x86)/nodejs/npm:
  /mnt/c/Program Files (x86)/nodejs/npm: 6: /mnt/c/Program Files (x86)/nodejs/npm: Syntax error: word unexpected (expecting "in")

This version was resolved with

  export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

After this command:

  npm --version
  5.8.0

Resources used:

https://www.digitalocean.com/community/tutorials/how-to-install-linux-apache-mariadb-php-lamp-stack-debian9

https://www.itwriting.com/blog/10213-setting-up-php-for-development-on-windows-services-for-linux-in-windows-10.html

https://github.com/microsoft/WSL/issues/3882