How to Install WordPress on Windows WSL

|

, ,

|

The tutorial demonstrates how to install WordPress on Windows 10 using Windows Subsystem for Linux (WSL) 2 with an Nginx HTTP server on Ubuntu 20.04 or 18.04. It includes steps for enabling WSL in Windows, installing Ubuntu on Windows 10, and setting up a Linux environment within Windows. The process also involves installing Nginx HTTP…

This brief tutorial shows students and new users how to install WordPress on Windows 10 WSL (Windows Subsystem for Linux) 2 with Nginx HTTP server on Ubuntu 20.04 | 18.04.

WordPress is the most popular content management system in use today. It’s an open-source platform and works great in Linux systems, including Ubuntu.

You might want to install WordPress on Windows WSL for several reasons. For one, it allows you to use the popular content management system on a Windows machine, which might be your preferred operating system.

Additionally, WSL 2 is an improvement over the original version, offering better performance and system call compatibility. Installing WordPress on WSL allows you to use the Nginx HTTP server, MariaDB database server, and PHP to create a complete web development environment.

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

To get started with running WordPress 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 below to install Nginx, MariaDB, and PHP and configure WordPress to run.

Install Nginx HTTP Server

WordPress requires a web server to function, and Nginx is one of the most popular open-source web servers available today.

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 and start Nginx services.

sudo service nginx stop
sudo service nginx start

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

http://localhost

If you see the above page in your browser, Nginx works as expected.

Install MariaDB Server

You’ll also need a database server to run WordPress. A database server is where WordPress content gets stored.

A truly open-source database server that you can use with WordPress is the MariaDB database server. It is fast and secure and is the default server for almost all Linux servers.

To install MariaDB, run the commands below:

sudo apt install mariadb-server mariadb-client

After installing MariaDB, the commands below can stop and start the service and enable it to start when the server boots.

sudo service mysql stop
sudo service mysql start

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.

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

Install PHP and Related Modules

WordPress is a PHP-based application, and PHP is required to run it. Run the commands below to install PHP and related 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

After installing PHP, configure some basic settings required for WordPress to function correctly.

For this tutorial, PHP 7.4 was installed. Based on your environment, another version of PHP might be installed. So verify that.

sudo nano /etc/php/7.4/fpm/php.ini

Below are good settings to configure for most WordPress websites.

file_uploads = On
allow_url_fopen = On
short_open_tag = On
memory_limit = 256M
cgi.fix_pathinfo = 0
upload_max_filesize = 100M
max_execution_time = 360
date.timezone = America/Chicago

That should get PHP 7.4 installed with some basic settings to allow WordPress to function.

After setting up PHP, the command below can be used to stop and start PHP7.4 services.

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

Create WordPress Database

When all the servers are installed above, it’s time to begin setting up the WordPress environment. First, run the steps below to create a blank WordPress database.

Login to the MariaDB database console using the commands below:

sudo mysql -u root -p

Then, create a database called wpdb

CREATE DATABASE wpdb;

Next, create a database user called wpdbuser and set a password

CREATE USER 'wpdbuser'@'localhost' IDENTIFIED BY 'new_password_here';

Then, grant the user full access to the database.

GRANT ALL ON wpdb.* TO 'wpdbuser'@'localhost' WITH GRANT OPTION;

Finally, save your changes and exit.

FLUSH PRIVILEGES;
EXIT;

Download WordPress

At this point, WordPress is ready to be downloaded and installed. Use the commands below to download the latest version of WordPress.

cd /tmp
wget https://wordpress.org/latest.tar.gz
tar -xvzf latest.tar.gz
sudo mv wordpress /var/www/wordpress

Then, run the command below to allow the www-data user to own the WordPress directory.

sudo chown -R www-data:www-data /var/www/wordpress/
sudo chmod -R 755 /var/www/wordpress/

Configure the Nginx Server block.

Below is where you configure the Nginx VirtualHost file for your WordPress site. This file defines how client requests are handled and processed.

Run the commands below to create a new VirtualHost file called WordPress in the /etc/Nginx/sites-available/ directory.

sudo nano /etc/nginx/sites-available/wordpress

Below is a perfect configuration setting for most WordPress sites on the Nginx server. This configuration should work great.

Copy the content below and save it into the file created above.

server {
    listen 80;
    listen [::]:80;
    root /var/www/wordpress;
    index  index.php index.html index.htm;
    server_name  example.com www.example.com;

    client_max_body_size 100M;
    autoindex off;
    
    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
         include snippets/fastcgi-php.conf;
         fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
         fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
         include fastcgi_params;
    }
}

Save the file and exit.

After saving the file above, run the commands below to enable the new site, then restart the Nginx server.

sudo ln -s /etc/nginx/sites-available/wordpress /etc/nginx/sites-enabled/
sudo service nginx restart

At this stage, WordPress is ready and can be launched by going to the server’s IP or hostname.

http://example.com

That should bring up the WordPress setup wizard.

Type in the database connection and continue.

Create a WordPress admin account and password, then click Install WordPress.

Log in with the account above, and you’re done.

That’s it!

Conclusion:

This post showed you how to install WordPress on Windows 10 with Ubuntu. If you find any error above, please use the comment form below to report.

Like this:



12 responses to “How to Install WordPress on Windows WSL”

  1. Steff Avatar
    Steff

    This doesn’t seem to work, I’ve tried it on WSL2.

  2. Avery Avatar
    Avery

    I successfully get this far and then no success:
    “At this stage, WordPress is ready and can be launched by going to the server’s IP or hostname.”

    I have tried localhost [takes me to “Welcome to nginx!” page]
    I have tried localhost/wordpress [gives me “404 Not Found” page]

    Please help.

    1. Richard Zayzay Avatar
      Richard Zayzay

      You’ll need to create a local host record in Windows 11 to create the domain name referenced in the setup.

      Read this post: https://websiteforstudents.com/how-to-edit-windows-11-hosts-file/

  3. jk pits Avatar
    jk pits

    Typing http://example.com takes me to an external site.

    Result:
    Example Domain

    This domain is for use in illustrative examples in documents. You may use this domain in literature without prior coordination or asking for permission.

  4. Marc Avatar
    Marc

    Thank you very much! For once I manage to do something with Linux… It’s that it’s well explained ! 🙂

    But now, can you explain : (or refer me to a manual)

    – How to access to phpmyadmin
    OR
    – How to import/export a database

    – How to delete a project
    – How to work on several projects at the same time (if possible)

    Sorry for all these questions, used to XAMPP, all this is new for me…

    Thank you !

    Translated with DeepL/Translator (free version)

  5. walter Avatar
    walter

    Using WSL2 / Ubuntu:

    I seemed to be able to install everything via the command line. It didn’t complain although I could not tell what the proper responses were supposed to be. When I was creating tables and users sometimes (MariaDB) said ‘1 row affected’, other times it said ‘0 rows affected.’ I assumed both were correct because I had no information to the contrary.

    I am comfortable enough with nano and the bash shell. I know nginx is installed because localhost in my Windows 10 browser gave me the nginx default page, but I have no idea where the file(s) are.

    I can see that WordPress is installed in /var/www/wordpress/ but when I enter I get “-bash No such file or directory”. I tried restarting nginx a couple of times. I don’t know how to check which version of PHP I have.

    I also edited the windows host file to redirect my domain to 127.0.0.1. This redirects it to the nginx root correctly, but returns a 404 error. Neither url gets me to an install screen. WP is installed under /var/www/wordpress. I double checked the config file.

    I know there are differences between WSL2 Ubuntu and Ubuntu running under a VirtualBox, but these directions do not mention is it was meant for either or both.

  6. Petar Bjelica Avatar
    Petar Bjelica

    Hi, I am getting an error in console – ERR_INCOMPLETE_CHUNKED_ENCODING 200 (OK). WP Dashboard wont load. Do you know the solution ?

  7. Nardu Avatar
    Nardu

    OMFG this was a sticky, stubborn and terrible installation.
    I can see I’m not the first, but trying to do this on WSL2 is an absolute minefield of errors, firewall drama and everything in between, with very little documentation to refer to if your install doesn’t work first time.

    If your installation fails to write to wp-config there is nowhere to turn.

    Suggest: Update the error reporting in the WP installation,
    because vague errors have cost me literal days to just get started. 🤮

  8. Klaylton Fernando Avatar
    Klaylton Fernando

    Tutorial served its purpose. My installation ran the first time. I just had to make the adjustments in the host file to set the website address.

  9. inhahe Avatar
    inhahe

    Didn’t work for me.
    ————————————————————————————————————————————————————————–
    In order to log into MariaDB to secure it, we’ll need the current
    password for the root user. If you’ve just installed MariaDB, and
    haven’t set the root password yet, you should just press enter here.

    Enter current password for root (enter for none):
    /usr/bin/mysql_secure_installation: 249: cannot create .my.cnf.1169: Permission denied
    /usr/bin/mysql_secure_installation: 250: cannot create .my.cnf.1169: Permission denied
    /usr/bin/mysql_secure_installation: 251: cannot create .my.cnf.1169: Permission denied
    /usr/bin/mysql_secure_installation: 253: cannot create .my.cnf.1169: Permission denied
    /usr/bin/mysql_secure_installation: 218: cannot create .mysql.1169: Permission denied
    /usr/bin/mysql_secure_installation: 220: cannot open .mysql.1169: No such file
    grep: .my.output.1169: No such file or directory
    OK, successfully used password, moving on…

    Setting the root password or using the unix_socket ensures that nobody
    can log into the MariaDB root user without the proper authorisation.

    Enable unix_socket authentication? [Y/n]
    /usr/bin/mysql_secure_installation: 218: cannot create .mysql.1169: Permission denied
    /usr/bin/mysql_secure_installation: 220: cannot open .mysql.1169: No such file
    Failed!
    Cleaning up…
    inhahe@Logoplex3:/mnt/c/Windows/system32$ sudo service mysql start
    mysql: unrecognized service

  10. AK Digital Avatar
    AK Digital

    Thanks for this helpfull tuto !

    Therefor, you forgot to indicate that we must add the line “127.0.0.1 example.com” in C:WindowsSystem32Driversetchosts in order to be able to reach our custom domain.

  11. A Guide to Removing the Windows Subsystem for Linux (WSL) from Windows 11 - Geek Rewind

    […] How to install WordPress on WSL […]

Leave a Reply to Klaylton Fernando Cancel 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.