How to Install WordPress with WSL on Windows 11

|

|

The tutorial guides on installing WordPress on Windows 11 via the Windows Subsystem for Linux (WSL). The process starts with enabling WSL in Windows 11 followed by installing a specific Linux distro, in this case, Ubuntu 20.04. The next steps include setting up the LEMP server to run WordPress, which involves installing the Nginx HTTP…

This brief tutorial shows students and new users how to install WordPress on Windows 11 using Windows Subsystem for Linux (WSL).

You can now install and run WordPress directly from a Windows desktop or server via Windows Subsystem for Linux (WSL) without installing third-party apps or using a virtual machine. In 2017, Microsoft released WSL and WSL2, which lets developers run a GNU/Linux environment, including most command-line tools, utilities, and applications directly in Windows without setting up a virtual machine or dual-boot.

WSL2 is the new and improved version that comes with a performance boost and full system call compatibility, and it is built with a new architecture that delivers features that make WSL an amazing way to run a Linux environment in Windows.

With Windows 11, installing WSL is slightly different than in Windows 10. However, the steps below will show you how to do that.

To install and run WordPress on Windows 11, you must first install and enable WSL.

Install Windows Subsystem for Linux in Windows 11

To enable WSL in Windows, you must open the Command Prompt as administrator. Click on Start, then begin typing Command Prompt.

Next, right-click the Command Prompt app and choose to Run as administrator.

When the console opens, run the commands below to install Windows Subsystem for Linux (WSL):

wsl --install

Wait for WSL to be installed.

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

Installing: Virtual Machine Platform
Virtual Machine Platform has been installed.
Installing: Windows Subsystem for Linux
Windows Subsystem for Linux has been installed.
Downloading: WSL Kernel
Installing: WSL Kernel
WSL Kernel has been installed.
Downloading: GUI App Support
Installing: GUI App Support
GUI App Support has been installed.
Downloading: Ubuntu
The requested operation is successful. Changes will not be effective until the system is rebooted.

Restart your computer.

WSL should be installed and ready to use. When you want to update, simply run the commands below:

wsl --update

Install Specific Linux distro on Windows 11

Now that WSL is installed, you can install your own Linux distro. To list the available distributions to install, simply run the commands below:

wsl --list --online

You should then see all available distributions that can be installed on WSL.

NAME            FRIENDLY NAME
Ubuntu          Ubuntu
Debian          Debian GNU/Linux
kali-linux      Kali Linux Rolling
openSUSE-42     openSUSE Leap 42
SLES-12         SUSE Linux Enterprise Server v12
Ubuntu-16.04    Ubuntu 16.04 LTS
Ubuntu-18.04    Ubuntu 18.04 LTS
Ubuntu-20.04    Ubuntu 20.04 LTS

To install a Linux distribution from the list above, simply run the commands below using the distribution name. For example, to install Ubuntu 20.04, run the commands below:

wsl --install -d ubuntu-20.04

You should then get a message that the distribution is installed.

Downloading: Ubuntu 20.04 LTS
Installing: Ubuntu 20.04 LTS
Ubuntu 20.04 LTS has been installed.
Launching Ubuntu 20.04 LTS.

After installing, you should get a Ubuntu command console with setup details.

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 LTS (GNU/Linux 4.4.0-22000-Microsoft x86_64)

Some troubleshooting commands to run when you run into issues.

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

Now that Ubuntu Linux is installed and ready, continue below to install the LEMP server to run WordPress. First, install the Nginx HTTP Server.

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 properly.

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 Nginx VirtualHost

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 very good 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 WSL 2 on Windows 11, install Ubuntu, and run WordPress. If you find any error, please report it in the comment form below.


Discover more from Geek Rewind

Subscribe to get the latest posts to your email.

Like this:



7 responses to “How to Install WordPress with WSL on Windows 11”

  1. Romandas Avatar
    Romandas

    Thanks for awesome guide. I managed to install with PHP 8.0.
    Also if somebody stuck with opening example.com to external site, you must edit windows host file and add these two lines to the bottom.
    127.0.0.1 example.com
    127.0.0.1 http://www.example.com

    according to this guide
    https://geekrewind.com/how-to-edit-windows-11-hosts-file/

    1. Richard Avatar
      Richard

      You cannot not use example.com with IP 127.0.0.1 externally. You can only view it from the same machine.

  2. Romandas Avatar
    Romandas

    Yes, unless you do not assign 127.0.0.1 to example.com in Windows host file.

  3. Pierre Avatar
    Pierre

    Great, it worked!
    Do not forget to edit the hosts file in windows

  4. Bryce Wisekal Avatar
    Bryce Wisekal

    Hello,

    I’ve followed these instructions perfection but getting an error “502 Gateway Error” when loading “example.com”. Do you have any suggestions? Tried all the obvious steps (ie., clearing cache, checking installations are correct, etc). Any thoughts on the next steps?

    1. Richard Avatar
      Richard

      Hi,
      WSL networking has improved since this article. However, there are some checks you need to perform after installing WordPress.

      1. Replace “example.com” with your own domain name. If using example.com in a test environment, then update the Windows hosts file and point example.com to the computer IP address.
      2. Add your domain name (example.com) in the hosts file on your Windows machine.
        Read this post below to learn how to edit your Windows hosts file.
        https://geekrewind.com/how-to-edit-windows-11-hosts-file/

      3. Confirm that the PHP-FPM version is referenced correctly in the Nginx config file.
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        If a different version of PHP is installed, ensure it’s used in the config file.

      4. Always start services for good measures.

      sudo service nginx start
      sudo service php7.4-fpm start
      sudo service mariadb start

      Hope this helps.
      Thanks
       

  5. Romasius Avatar
    Romasius

    Finally after 5 days of struggling and trying 5 different instructions I have WP. Only this instruction worked.
    The only thing missing is what to do after rebooting the PC, how to make http://www.example.com work again
    Thank you very much
    I’m already a programmer :-)))))))

Leave a Reply to PierreCancel reply

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

Blog at WordPress.com.

Discover more from Geek Rewind

Subscribe now to keep reading and get access to the full archive.

Continue reading