This post will guide you through setting up a local PHP development environment using MySQL on a Windows PC. We'll setup Windows to use WSL 2 and install the Ubuntu 20.04 LTS distribution. Then we'll install Git, PHP, Node and MySQL as well as install and configure Visual Studio Code and TablePlus.
Prerequisites
To use WSL 2, you must be running Windows 10:
- For x64 systems: Version 1903 or higher, with Build 18362 or higher.
- For ARM64 systems: Version 2004 or higher, with Build 19041 or higher.
- Builds lower than 18362 do not support WSL 2. Use the Windows Update Assistant to update your version of windows.
Install WSL2
The Windows Subsystem for Linux (WSL) is a new Windows 10 feature than enables you to run native Linux command-line tools directly on Windows.
Open PowerShell as an administrator and enter the following commands:
Enable WSL
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
Enable Virtual Machine Platform
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
Once you've done this, you'll need to restart your PC. Then click here to download and install the Linux kernel update package.
Finally, open PowerShell as an administrator and set the default WSL version:
wsl --set-default-version 2
Install Ubuntu 20.04 LTS
Ubuntu is a Linux distribution based on Debian and mostly composed of free and open-source software.
Open the Microsoft Store App and search for Ubuntu. Choose the 20.04 LTS Option and install.
Once installed open the distribution from your programs:
As this is the first use of Ubuntu 20.04 LTS, you will be asked to set up a new user:
Finally, you'll need to update the Ubuntu 20.04 LTS Operating System:
Download available package information
sudo apt update
Install available upgrades of all packages
sudo apt upgrade
Install Git
Git is a distributed version-control system for tracking changes in source code during software development.
Install git
sudo apt-get install git-all
Check install and version
git --version
Install PHP
PHP is a general-purpose scripting language especially suited to web development.
Install PHP package
sudo apt install php
Check install and version
php -v
Install Composer
Composer is an application-level package manager for the PHP programming language that provides a standard format for managing dependencies of PHP software and required libraries.
Click here and copy the script from the downloads page:
Paste the script into your Ubuntu 20.04 LTS terminal to download Composer. You'll then want to make Composer available globally:
sudo mv composer.phar /usr/local/bin/composer
Check install and version
composer -V
Install Node & NPM
Node.js is an open-source, cross-platform, back-end, JavaScript runtime environment that executes JavaScript code outside a web browser and npm is a package manager for the JavaScript programming language.
We'll use Node Version Manager(NVM) to manage Node and NPM. Use the following command to download and install:
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.0/install.sh | bash
Check install and version
nvm -v
You can then use NVM to install the latest version of node:
nvm install node
Check install and version
node -v
npm -v
Install Visual Studio Code
Visual Studio Code is a free source-code editor made by Microsoft for Windows, Linux and macOS.
The Remote - WSL extension lets you use VS Code on Windows to build Linux applications that run on the Windows Subsystem for Linux (WSL).
Click here to download and install the latest version of Visual Studio Code.
Once installed you will need to add the Remote - WSL extension:
Once the Remote - WSL extension has been installed, you can open folders within your Ubuntu system within Visual Studio Code.
Install and Configure MySQL
Install MySql Server package
sudo apt install mysql-server
Start the MySQL Server service
sudo service mysql start
Open MySQL command-line client
sudo mysql
You can now send commands to MySQL. First you will need to create a new user and assign permissions:
Create a new database user
CREATE USER 'jack'@'localhost' IDENTIFIED WITH mysql_native_password BY 'myPassword';
Create a new application database
CREATE DATABASE MY_APP_DB;
Give the user permissions the new application database
GRANT ALL PRIVILEGES ON MY_APP_DB.* TO 'jack'@'localhost';
Exit the MySQL command-line client
exit;
You can now log back into the MySQL command-line client as the user you have just created, use the newly created database and add a table.
Open MySQL command-line client
mysql -u jack -p
Select the database you've just created
USE MY_APP_DB;
Create a new table
CREATE TABLE users(
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(30) NOT NULL,
email VARCHAR(50)
);
Add records to the table you've just created
INSERT INTO `users` (`name`, `email`) VALUES
('Jack', 'jack@email.com'),
('Dave', 'dave@email.com'),
('Joey', 'joey@email.com');
Exit the MySQL command-line client
exit;
Install the PHP MySQL extension
sudo apt-get install php-mysql
Install and Configure TablePlus
Click here to download the latest version of TablePlus.
Once installed open the application and select "Create a new connection..."
Enter the connection details, using the database and user you created previously:
Field | Value |
---|---|
Name | MY_APP_DB |
Host | 127.0.0.1 |
User | jack |
Password | myPassword |
Database | MY_APP_DB |
Test the connection then Save:
Once connected to the database, you'll see the records you created previously:
Create a Test Application
You'll want to close any Ubuntu 20.04 LTS terminals you have open to allow any changes to take place. Once re-opened enter the following commands:
Create a new folder
mkdir my-test-application
Change directory to the new folder
cd my-test-application
Create a new file
touch index.php
Open the directory in Visual Studio Code
code .
This will open Visual Studio Code with the contents of the directory present in the workspace to the left of the screen:
Add the following to the index.php file:
<?php
$servername = "127.0.0.1";
$username = "jack";
$password = "myPassword";
$dbname = "MY_APP_DB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, name, email FROM users";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["name"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
Save the file in Visual Studio Code, then go back to your Ubuntu 20.04 LTS terminal and enter the following command:
php -S localhost:8000
This will start a local development server. You can now open a web browser and navigate to localhost:8000.
Conclusion
You should now have everything you need to start developing on PHP. Enjoy!
Top comments (6)
I encoutered an error once i attempt this "apt-get update"
Resolution:
/etc/resolv.conf was getting re-created for me every time. this fixed it for me:
Create a file: /etc/wsl.conf.
Put the following lines in the file
[network]
generateResolvConf = false
In a cmd window, run wsl --shutdown
Restart WSL2
Create a file: /etc/resolv.conf. If it exists, replace existing one with this new file.
Put the following lines in the file
nameserver 8.8.8.8
Repeat step 3 and 4. You will see git working fine now.
Thank you so much bro! simple and clean tutorial
Thank you so much!
This tutorial is greatful πππ
This really helped!
Thanks a lot!!
Awesome!
Thanks for putting this together.
Great guide, thanks