Install PHP, Apache, MySQL and WordPress on Windows with WSL Ubuntu

  • dani


6 min read
Photo by Gabriel Heinzer / Unsplash

In a previous article, I talked about how to install WSL on Windows and how it enables you to easily build local development environments on a Windows system. If you do not know about WSL and did not setup it up yet, start with reading the previous article:

Install Linux on Windows with WSL
Install Linux distribution (e.g. Ubuntu) on your Windows computer using Windows Subsystems for Linux.

In this article, I explain how to get a local web development environment with PHP, Apache web server and MySQL database to be used for e.g. WordPress, all in an Ubuntu subsystem on Windows using WSL.

If you have a fresh installation of an Ubuntu subsystem, first thing you should do (if you have not done that already) is update all software with the following commands for Aptitude (apt):

sudo apt update
sudo apt upgrade

Let us start with setting up the Apache web server first, before we move on to PHP, MySQL database and the WordPress blog system.

Install Apache web server

Installing the Apache web server is the easy part. We just install it from the default Aptitude software repository:

sudo apt install apache2

Next, we activate the mod_rewrite module used for rewriting URLs:

sudo a2enmod rewrite

We do not install any other mods, yet. Beforehand, we install PHP and then add the mods.

Install PHP

Installing PHP is not as forward as installing Apache. The problem is that WSL typically uses long-term support (LTS) Ubuntu versions. Hence, it installs the latest LTS version for Ubuntu, that is Ubuntu 20.04. The 20 stands for the year 2020 and 04 for month April, so this version is kinda outdated already (as of writing this post). A major drawback for us is that Ubuntu 20.04 is bundled with PHP 7.4 (again, as of writing this post). Therefore, if you run sudo apt install php it installs the PHP version from the default Aptitude software repository, which is PHP 7.4.

We do not want that version. We want to move forward with PHP 8. Technically, WordPress in particular may work well with PHP 7 – at least without plugins. But you may want to use PHP for other software as well, so better opt for PHP 8 from the beginning.

To install PHP 8, we need to add a new software repository and explicitly install the PHP packages in version 8. We could still omit the version but then we would fall back to installing PHP 7 from the default repository. Use the following commands on your Windows Terminal in the Ubuntu system to install PHP 8.1:

sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo apt update
sudo apt install php8.1

Check that PHP is installed with php --version . The output should look similar:

daniel@DESKTOP:~$ php --version
PHP 8.1.9 (cli) (built: Aug 15 2022 09:39:52) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.9, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.9, Copyright (c), by Zend Technologies

Next, we install common PHP extensions (you may want to add more extensions depending on the libraries used in your PHP code):

sudo apt install php8.1-common php8.1-mysql php8.1-xml php8.1-xmlrpc php8.1-curl php8.1-gd php8.1-imagick php8.1-cli php8.1-dev php8.1-imap php8.1-mbstring php8.1-opcache php8.1-soap php8.1-zip php8.1-redis php8.1-intl

Finally, we install the Apache PHP 8.1 mod, disable the PHP 7.4 mod and activate the one for PHP 8.1:

sudo apt install libapache2-mod-php8.1
sudo a2dismod php7.4
sudo a2enmod php8.1

To apply all changes we restart Apache:

sudo service apache2 restart

Your Apache should be responding on the URL http://localhost:80. You can test that PHP is working by placing an index.php file in the directory /var/www/html/ with the content <? phpversion(); ?>. It should print information about the installed PHP, i.e. the version number and activated extensions.

Let us continue to install the MySQL database before we finally install the WordPress blog system on our local environment.

Install MySQL database

This part is easier than the previous, we just install MySQL 8 from the default repository:

sudo apt install mysql-server

After the command finished, check if MySQL is indeed installed using:

mysql --version

This should print your installed version, something like mysql  Ver 8.0.30-0ubuntu0.20.04.2 for Linux on x86_64 ((Ubuntu)). Next, start the MySQL server:

sudo /etc/init.d/mysql start

Up next, secure the MySQL installation using the following script:

sudo mysql_secure_installation

This guides you through some questions to secure your installation.

To access your database, there are three options:

  1. You can access the database right-away from your Ubuntu command line in the Windows Terminal with the command sudo mysql. This starts the interactive input in which you can type SQL statements like SHOW DATABASES; to show all available databases or SELECT * FROM ... to query data. Adding new databases or users may not be as straightforward as using a graphical interface, so let us see other options.
  2. Another option is to install PHPMyAdmin on the Linux machine, which you can then access through the Apache2 web server we just installed with any browser on your Windows system. This may require some Apache virtual host configuration.
  3. The easiest option is to install the MySQL workbench as standalone software on your Windows system. With the workbench, you simply connect to the database on localhost:3306 using the root user. The software gives you a feature-rich graphical interface to your database.

It is a good practice to create a separate user with its own database for every application that uses the database to store information. The user should only have access rights granted for its database. That way, if one application is affected by a security vulnerability and exposes the login data to the database, the damage may be restricted to the application's database. For a local development environment, security might not be a big concern. However, the separation is nice in case you want to delete an application and drop its database at once. So, create a new database user with its own database before we move on to install WordPress in the next section.

Install WordPress

On the Ubuntu system, we use the WordPress command-line interface (CLI) to install a new WordPress instance. To do so, we download the CLI from the Terminal:

curl -O

Next, we check if it works:

php wp-cli.phar --info

This should print something like:

daniel@DESKTOP:~$ php wp-cli.phar --info
OS:     Linux #1 SMP Fri Apr 2 22:23:49 UTC 2021 x86_64
Shell:  /bin/bash
PHP binary:     /usr/bin/php8.1
PHP version:    8.1.9
php.ini used:   /etc/php/8.1/cli/php.ini
MySQL binary:   /usr/bin/mysql
MySQL version:  mysql  Ver 8.0.30-0ubuntu0.20.04.2 for Linux on x86_64 ((Ubuntu))
SQL modes:
WP-CLI root dir:        phar://wp-cli.phar/vendor/wp-cli/wp-cli
WP-CLI vendor dir:      phar://wp-cli.phar/vendor
WP_CLI phar path:       /home/daniel
WP-CLI packages dir:
WP-CLI global config:
WP-CLI project config:
WP-CLI version: 2.6.0

Next, we move the CLI tool from the home directory of our user to the system executable path:

chmod +x wp-cli.phar
sudo mv wp-cli.phar /usr/local/bin/wp

We check again, if it works:

wp --info

Finally, we create the WordPress instance in a new directory called wordpress in /var/www/html/:

cd /var/www/html/
mkdir wordpress
cd wordpress
wp core download

This downloads the latest WordPress version into the directory, the output should look like:

daniel@DESKTOP:/var/www/html/wordpress$ wp core download
Downloading WordPress 6.0.2 (en_US)...
md5 hash verified: 03b5aeda1b4f26178015befd66a65145
Success: WordPress downloaded.

Your WordPress instance is now ready to be configured. You can configure the WordPress using the web installation routine by accessing http://localhost:80/wordpress in your browser. Or, you move ahead with the WP-CLI. Create a new database user on your MySQL database and then create a new wp-config.php for your WordPress with the credentials for the user and database your created in the last section:

wp config create --dbname=wordpress --dbuser=wpuser --dbpass=wpuserpass

Create the database if it does not exist yet:

wp db create

Run the installation script:

wp core install --url=localhost/wordpress --title="My cool blog" --admin_user=wpcli --admin_password=wpcli

That's it! You successfully installed a local WordPress development environment for developing themes or plugins.