In this tutorial, we will show you how to install the LEMP stack on Windows 10 WSL. For those of you who didn’t know, LEMP is a free and open-source Web development platform that used for web application development. LEMP stands for Linux, Nginx (Engine-X), MariaDB/MySQL, and PHP, all of which are open source and free to use. It is the most common software stack that powers dynamic websites and web applications.
This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo
‘ to the commands to get root privileges. I will show you the step-by-step installation of the LEMP stack under Windows. You can follow the same instructions for Ubuntu 18.04, 16.04, and any other Debian-based distribution like Linux Mint on the Subsystem for Linux 2 (WSL2).
Requirements
- Windows 10 May 2020 (2004), Windows 10 May 2019 (1903), or Windows 10 November 2019 (1909) or later.
- A computer with Hyper-V Virtualization support.
Install LEMP Stack on Windows 10 WSL
Step 1. Enable the Windows Subsystem for Linux 2.
You must first enable the “Windows Subsystem for Linux” optional feature before installing any Linux distributions on Windows. Now run the following command below to enable it:
Open PowerShell as Administrator and run:
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
Step 2. Enable Virtual Machine Feature.
Before installing WSL 2, you must enable the Virtual Machine Platform optional feature. Your machine will require virtualization capabilities to use this feature.
To enable Virtual Machine Platform on Windows 10 (2004) open PowerShell as Administrator and run:
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
Step 3. Set WSL 2 as your default version.
We set WSL 2 as default. Now open PowerShell as Administrator and run this command to set WSL 2 as the default version of WSL:
wsl --set-default-version 2
Step 4. Installing Ubuntu Linux Distribution.
With WSL and the necessary virtualization tech all in place, all that is left for you to do is pick and install a Linux distro from the Microsoft Store. Several different distros are available, including OpenSUSE, Pengwin, Fedora Remix, and Alpine Linux.
To install Ubuntu on Windows 10 open the Microsoft Store app, search for “Ubuntu 20.04”, and hit the “Get” button:
Step 5. Installing Nginx on Windows WSL.
To install Nginx HTTP on your Ubuntu server, run the commands below:
sudo apt update sudo apt install nginx
After successfully installed, run the commands below to stop and start Nginx services:
sudo service nginx stop sudo service nginx start
Next, verify that the webserver is running and accessible by accessing your server’s IP address:
http://localhost
Step 6. Installing MariaDB on Windows WSL.
Run the following command to install MariaDB:
sudo apt install mariadb-server mariadb-client
Once is done, run the commands below to stop, start and enable the MariaDB service to always start up with the server boots:
sudo service mysql stop sudo service mysql start
By default, MariaDB is not hardened. You can secure MariaDB using the mysql_secure_installation
script. You should read and below each step carefully which will set a root password, remove anonymous users, disallow remote root login, and remove the test database and access to secure MariaDB.
mysql_secure_installation
Configure it like this:
- Set root password? [Y/n] y - 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
Log in to the database console using the commands below:
mysql -u root -p
Step 7. Installing PHP on Windows 10 WSL.
Now run the commands below to install PHP and modules to support WordPress:
sudo apt install php-fpm php-common php-mysql php-gmp php-curl php-intl php-mbstring php-xmlrpc php-gd php-xml php-cli php-zip
Next, run the command below to stop and start PHP7.4 services:
sudo service php7.4-fpm stop sudo service php7.4-fpm start
Once installing the PHP and related modules, run the commands below to verify that PHP is installed or not :
php -v
Step 8. Test PHP.
Create a test PHP file called phpinfo.php
the server’s default home directory using the command below:
sudo nano /var/www/html/phpinfo.php
Add the following line:
<?php phpinfo(); ?>
Next, open the Nginx default configuration file and Uncomment the highlighted lines:
sudo nano /etc/nginx/sites-available/default
Uncomment the highlighted lines and save the file:
# Default server configuration # server { listen 80 default_server; listen [::]:80 default_server; # # include snippets/snakeoil.conf; root /var/www/html; # Add index.php to the list if you are using PHP index index.php index.html index.htm index.nginx-debian.html; server_name _; location / { # First attempt to serve request as file, then # as directory, then fall back to displaying a 404. try_files $uri $uri/ =404; } # pass PHP scripts to FastCGI server # location ~ \.php$ { include snippets/fastcgi-php.conf; # # # With php-fpm (or other unix sockets): fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; # # With php-cgi (or other tcp sockets): # fastcgi_pass 127.0.0.1:9000; } # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { }
Restart the Nginx service to have the changes take effect:
sudo service nginx restart
The information about the installed PHP should be displayed here:
http://127.0.0.1/info.php or http://localhost/info.php
Congratulations! You have successfully installed LEMP. Thanks for using this tutorial for installing the latest stable version of the LEMP Stack on Windows Subsystem for Linux 2 (WSL 2) on the Ubuntu 20.04 LTS (Focal Fossa) system. For additional help or useful information, we recommend you check the official Ubuntu website.