How to Install the LAMP Stack on Windows WSL

This simple tutorial shows students and new users how to install the LAMP stack on Windows 10 WSL (Windows Subsystem for Linux) 2 with Ubuntu OS.

LAMP is short for Linux (Ubuntu), Apache2 HTTP Server, MariaDB or MySQL Database Server, and PHP Scripting Language. It’s a group of free, open-source software that powers many websites and content management systems (CMS) today.

Why Install LAMP on Windows WSL?

Installing the LAMP stack on Windows WSL is useful for several reasons. First, it lets you run a full Linux operating system inside Windows. This is helpful if you need to use Linux-based tools or software.

Second, the LAMP stack is popular for web development and hosting. Installing it on your Windows machine lets you create and test websites on your own computer.

Finally, WSL 2 gives you better performance and works like real Linux. This means you get a smoother experience when running Linux applications on Windows.

How to Install LAMP Stack on Windows WSL

Step 1: Enable WSL in Windows

⚠️ Admin Privileges Required

To enable WSL in Windows, open the PowerShell terminal as administrator. Click on Start, then type PowerShell.

Right-click the Windows PowerShell app and choose “Run as administrator”.

When the console opens, run this command:

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

You should see a success message saying the operation completed successfully.

Step 2: Enable Virtual Machine Platform

⚠️ Admin Privileges Required

WSL 2 needs the Windows 10 Virtual Machine Platform to be enabled. Run this command from the same PowerShell administrator console:

dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart

If you’re using Windows 10 version lower than 2004, use this command instead:

Enable-WindowsOptionalFeature -Online -FeatureName VirtualMachinePlatform -NoRestart

After running the commands, restart your computer so the changes take effect.

After restarting, log back in and launch PowerShell as administrator. Then run this command to set WSL 2 as the default version:

wsl --set-default-version 2

Step 3: Install Ubuntu on Windows 10

Now that WSL 2 is ready, download and install Ubuntu 20.04 from the Windows Store using the link below:

Get Ubuntu 20.04 LTS – Microsoft Store

Ubuntu 20.04 LTS on Windows lets you use the Ubuntu Terminal and run Linux command line tools like bash, ssh, git, apt, and many more.

Click the Get button and install it. After installing Ubuntu, you’ll be able to launch it from the Windows WSL environment.

When you launch Ubuntu, it will prompt you to create a user account. This may take a few minutes.

Here’s what you’ll see:

Please create a default UNIX user account. The username does not need to match your Windows username.
For more information visit: https://aka.ms/wslusers
Enter new UNIX username: user1
New password:
Retype new password:
passwd: password updated successfully
Installation successful!
To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.

Welcome to Ubuntu 20.04.2 LTS (GNU/Linux 4.4.0-19041-Microsoft x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/support

  System information as of Mon Apr 12 17:57:37 CDT 2021

  System load:    0.52      Processes:             7
  Usage of /home: unknown   Users logged in:       0
  Memory usage:   26%       IPv4 address for eth0: 192.168.1.100
  Swap usage:     0%

1 update can be installed immediately.
0 of these updates are security updates.
To see these additional updates run: apt list --upgradable

That’s it! If you run into issues, try these troubleshooting commands:

wsl --set-default-version 1
bcdedit /set hypervisorlaunchtype auto start

Now you’re ready to install Apache, MariaDB, and PHP.

Step 4: Prepare Ubuntu Linux

Ubuntu is now installed inside Windows WSL. Update it using these commands:

sudo apt update
sudo apt upgrade
sudo apt autoremove

These commands help manage and update your Ubuntu operating system.

Step 5: Install Apache HTTP Server

Apache is the most popular free web server used on most websites online.

To install Apache on Ubuntu, run these commands:

sudo apt update
sudo apt install apache2

After installing Apache2, use these commands to stop, start, and restart Apache:

sudo service apache2 stop
sudo service apache2 start
sudo service apache2 restart

To check that Apache is working, open your web browser and go to:

http://localhost

You should see a test page if everything works.

Step 6: Install MariaDB Database Server

MariaDB is a free database server you can use with your projects. It’s fast, secure, and the standard database for most Linux systems.

To install MariaDB, run these commands:

sudo apt-get install mariadb-server mariadb-client

After installing MariaDB, use these commands to stop, start, and restart the database:

sudo service mysql stop
sudo service mysql start
sudo service mysql restart

Next, secure your database by running this command:

sudo mysql_secure_installation

Answer the questions like this:

  • Enter current password for root (enter for none): Just press Enter
  • Set root password? [Y/n]: Y
  • New password: Enter your password
  • Re-enter new password: Repeat your password
  • Remove anonymous users? [Y/n]: Y
  • Disallow root login remotely? [Y/n]: Y
  • Remove test database and access to it? [Y/n]: Y
  • Reload privilege tables now? [Y/n]: Y

To check that MariaDB is working, log in using this command:

sudo mysql -u root -p

Type your root password when asked. You should see:

Welcome to the MariaDB monitor.  Commands end with ; or g.
Your MariaDB connection id is 46
Server version: 10.3.29-MariaDB-0ubuntu0.20.04.1 Ubuntu 20.04
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
MariaDB [(none)]>

If you see this, MariaDB installed successfully.

Step 7: Install PHP and Related Modules

PHP is the scripting language that connects all the other parts of LAMP together.

To install PHP and useful modules, run this command:

sudo apt install php libapache2-mod-php php-common php-mysql php-gmp php-curl php-intl php7.4-mbstring php-xmlrpc php-gd php-xml php-cli php-zip

To check that PHP is installed, run:

php -v

You should see something like:

PHP 7.4.3 (cli) (built: Oct  6 2020 15:47:56) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
with Zend OPcache v7.4.3, Copyright (c), by Zend Technologies

You can also test PHP by creating a test file. Run this command:

sudo nano /var/www/html/phpinfo.php

Type this content and save the file:

<?php phpinfo( ); ?>

Restart Apache with:

sudo service apache2 restart

Then open your browser and go to:

http://localhost/phpinfo.php

You should see the PHP information page.

Summary

You’ve successfully installed the LAMP stack on Windows WSL! Here’s what you did:

  • Enabled WSL 2 on Windows with PowerShell
  • Installed Ubuntu 20.04 from the Windows Store
  • Updated Ubuntu with the latest packages
  • Installed Apache web server
  • Installed MariaDB database server and secured it
  • Installed PHP and important modules

Your LAMP stack is now ready to use for web development and testing on your local Windows machine. If you run into any issues, check the troubleshooting commands above or leave a comment below.

Frequently Asked Questions

What is the LAMP stack and why is it important?

The LAMP stack is a software bundle consisting of Linux, Apache, MySQL/MariaDB, and PHP. It is essential for web development as it provides the necessary tools to create and host dynamic websites and applications.

How do I enable WSL on Windows 10?

To enable WSL on Windows 10, open PowerShell as an administrator and run the command 'dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart'. After executing the command, restart your computer to apply the changes.

What are the benefits of using WSL 2 for LAMP stack installation?

WSL 2 provides a full Linux kernel, improved performance, and complete system call compatibility, making it ideal for running Linux applications on Windows. This allows for a more seamless experience when developing and testing web applications using the LAMP stack.

Can I install Ubuntu on WSL 2 for the LAMP stack?

Yes, you can install Ubuntu on WSL 2 to set up the LAMP stack. Simply download Ubuntu 20.04 from the Microsoft Store or the official Ubuntu website and follow the installation instructions to get started.

What should I do if my WSL commands are not recognized?

If your WSL commands are not recognized, ensure that you have restarted your computer after enabling the Virtual Machine Platform and setting WSL 2 as the default version. If the issue persists, double-check that all required features are enabled in Windows.

Categories:

,
  1. Christophe Avatar
    Christophe

    Thanks for this guide, this exactly what I was looking for.
    One more thing: after installing php you will need to restart the apache2 service.

  2. Nicolas Aramboles Avatar
    Nicolas Aramboles

    Excellent material, this and other online tutorials, where very detailed.

  3. Steven Cooper Avatar
    Steven Cooper

    Good until the very end. Example.com? Are you serious? Exactly how does one set that up? It’s one thing to just install all of these components; anyone with 2 brain cells can figure that part out. Without initial setup that at least explains how to launch a PHP page on localhost and connect to the Database, what value does any of this have? Nothing. What a waste of time.

Leave a Reply

Your email address will not be published. Required fields are marked *