How to Install the LEMP Stack on Windows WSL

|

|

The tutorial provides a step-by-step guide on how to install the LEMP stack (Linux, Nginx, MariaDB, PHP) on Windows 10 using Windows Subsystem for Linux (WSL) 2 and Ubuntu operating system. It covers enabling WSL and the Virtual Machine Platform, installing Ubuntu from the Windows Store, and then sequentially installing Nginx HTTP server, MariaDB database…

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

LEMP is an acronym for Linux (Ubuntu), Nginx [engine x] HTTP Server, MariaDB or MySQL Database Server, and PHP Scripting Language. It is a group of open-source software and building blocks of many web applications and most content management systems (CMS) today.

Installing the LEMP stack on Windows WSL allows you to leverage the power of Linux tools and software on a Windows machine. This can be especially useful for web developers who need to run Linux-based web applications or content management systems (CMS) on a Windows machine.

The LEMP stack is a powerful combination of open-source software that includes the Linux operating system, Nginx web server, MariaDB or MySQL database server, and PHP scripting language.

With WSL, you can install and run a full Linux operating system inside Windows, which makes it possible to run LEMP on a Windows machine. Additionally, WSL 2 comes with improved performance and complete system call compatibility, making it a fantastic way to run a Linux environment in Windows.

If you have a machine that meets the requirements above to run WSL 2, continue below.

To get started with running LEMP on Windows with WSL, follow the steps below:

Enable WSL in Windows

To enable WSL in Windows, you will want to open the PowerShell terminal as administrator. Click on Start, then begin typing PowerShell.

Next, right-click the Windows PowerShell app and choose to run as administrator.

When the console opens, run the commands below:

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

After installing, you should get a success message similar to the lines below:

Deployment Image Servicing and Management tool
Version: 10.0.19041.844

Image Version: 10.0.19042.844

Enabling feature(s)
[==========================100.0%==========================]
The operation completed successfully.

Enable Virtual Machine Platform

WSL 2 requires Windows 10 Virtual Machine Platform to be enabled. This is not Hyper-V. To enable the VM platform feature in Windows, run the commands below from the same PowerShell administrator’s console.

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

If you’re using Windows 10 version lower than 2004, then use the commands below:

Enable-WindowsOptionalFeature -Online -FeatureName VirtualMachinePlatform -NoRestart

When you’re done running the commands above, restart your computer for all the configuration changes to apply. If you don’t restart, the below command might not be recognized.

After restarting your computer, log back in and launch PowerShell as administrator. Then, run the commands below to configure WSL 2 as the default version of WSL.

wsl --set-default-version 2

Install Ubuntu on Windows 10

Now that WSL 2 is installed and ready to be used, open the link below to download and install a copy of Ubuntu 20.04 from the Windows store.

Get Ubuntu 20.04 LTS – Microsoft Store

Ubuntu 20.04 LTS on Windows allows you to use Ubuntu Terminal and run Ubuntu command line utilities, including bash, ssh, git, apt, and many more.

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

After launching, Ubuntu should install and prompt you to create your account.

Installing, this may take a few minutes.
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: richard
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/advantage

  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: 10.0.2.15
  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 should do it!

Some troubleshooting commands to run when you run into the issues above. These run below and try to relaunch the Ubuntu image.

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

Now that the Windows 10 WSL environment is ready continue to install Nginx, MariaDB, and PHP and configure LEMP.

Prepare Ubuntu Linux

L in LEMP stands for Linux, in this case, Ubuntu. Since we’ve installed Ubuntu above inside Windows WSL, continue below to update Ubuntu.

Ubuntu is installed above. The commands below help manage and update Ubuntu OS.

sudo apt update
sudo apt upgrade
sudo apt autoremove

There are many other configurations and settings to apply to Ubuntu servers, but we’re not going to cover all here. Since Ubuntu is installed above, continue below to install the other components of the LEMP stack.

Install Nginx HTTP Server

The E in LEMP stands for Nginx [engine x] HTTP server. Nginx is probably the second most popular open-source web server, powering most websites online.

To install Nginx on Ubuntu, run the commands below:

sudo apt update
sudo apt install nginx

After installing Nginx, the commands below can be used to stop, start, and restart Nginx services.

sudo service nginx stop
sudo service nginx start
sudo service nginx restart

To validate that Nginx is installed and functioning, browse your web browser to the server’s hostname or IP address.

You should get a test page if everything works.

http://localhost

Install MariaDB Database Server

M in LEMP stands for MySQL or MariaDB database server. For this tutorial, we’re going to be installing MariaDB instead.

MariaDB is a truly open-source database server you can run with your projects. It is fast and secure and is the default server for almost all Linux.

To install MariaDB, run the commands below:

sudo apt-get install mariadb-server mariadb-client

After installing MariaDB, the commands below can be used to stop, start, and restart MariaDB services.

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

Next, run the commands below to secure the database server with a root password if you were not prompted to do so during the installation.

sudo mysql_secure_installation

When prompted, answer the questions below by following the guide.

  • Enter current password for root (enter for none): Just press the Enter
  • Set root password? [Y/n]: Y
  • New password: Enter password
  • Re-enter new password: Repeat 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 verify and validate that MariaDB is installed and working, log in to the database console using the commands below:

sudo mysql -u root -p

Type the root password when prompted.

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)]>

The server was successfully installed if you see a similar screen.

PHP-FPM and Related Modules

The P in LEMP stands for PHP. PHP is a general-purpose scripting language that glues all the other components of the stack.

To install PHP and recommended modules, run the commands below.

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

That should get PHP installed with recommended PHP modules that you can run with many PHP-based applications.

After installing PHP-FPM and related modules, run the commands below to stop, start and restart PHP-FPM. The default PHP version in Ubuntu 20.04 is PHP7.4-FPM

sudo service php7.4-fpm stop
sudo service php7.4-fpm start
sudo service php7.4-fpm restart

To validate that PHP is installed, run the commands below:

php -v

You should see an output like the one below:

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 with a test PHP script and display the installed version and related modules that are enabled or disabled.

To do that, run the commands below to create a PHP test file called phpinfo.php

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

Then, type the content below and save the file.

<?php phpinfo( ); ?>

Save the file.

Open your browser and browse to your server hostname followed by phpinfo.php

Next, open the Nginx site server block and comment on the highlighted lines below. First, run the commands below to open the Nginx default site server block.

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 Nginx, then type the address and browse the file.

http://example.com/phpinfo.php

You should see the PHP default test page.

That’s it!

Conclusion:

This post showed you how to install the LEMP stack in Windows WSL. If you find any error above, please use the comment form below to report.

Like this:



Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.