Windows Subsystem for Linux (WSL) is a feature that allows you to run Linux applications natively on Windows 11. It is a great tool for developers who want to use Linux tools and frameworks on their Windows machines. In this article, we will show you how to install and configure WSL for PHP MySQL development.
Step 1: Enable WSL and install a Linux distribution
Before you can use WSL, you need to enable it on your Windows 11 PC. To do that, follow these steps:
Open the Settings app and click on System.
On the left pane, click on Developer and then toggle on the Developer mode switch.
On the right pane, click on Windows Subsystem for Linux and then click on Install.
Wait for the installation to complete and then restart your PC.
After enabling WSL, you need to install a Linux distribution of your choice. You can do that from the Microsoft Store or from the command line. For this article, we will use Ubuntu 20.04 LTS as an example. To install it from the Microsoft Store, follow these steps:
Open the Microsoft Store app and search for Ubuntu.
Click on Ubuntu 20.04 LTS and then click on Get.
Wait for the download and installation to finish and then launch it from the Start menu or the taskbar.
The first time you launch it, you will be asked to create a user name and password for your Linux account. Enter them and press Enter.
Step 2: Install Nginx, PHP and MySQL on Ubuntu
Now that you have Ubuntu running on WSL, you can install the necessary packages for PHP MySQL development. To do that, follow these steps:
Open your Ubuntu terminal (either from the Start menu or by typing wsl in the Run dialog box).
Update your Ubuntu packages by running: sudo apt update
Install Nginx, PHP and MySQL by running: sudo apt install nginx php mysql-server
Secure your MySQL installation by running: sudo mysql_secure_installation and following the prompts.
Start Nginx and MySQL services by running: sudo service nginx start and sudo service mysql start
Step 3: Configure Nginx, PHP and MySQL on Ubuntu
After installing Nginx, PHP and MySQL, you need to configure them to work together. To do that, follow these steps:
Create a test PHP file in the default web root directory by running: echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php
Edit the default Nginx configuration file by running: sudo nano /etc/nginx/sites-available/default
Find the line that says index index.html index.htm index.nginx-debian.html; and add index.php after it, so it looks like this: index index.php index.html index.htm index.nginx-debian.html;
Find the block that says location / { ... } and add this line inside it: try_files $uri $uri/ =404;
Find the block that says location ~ \.php$ { ... } and uncomment it by removing the # symbols at the beginning of each line.
Save and exit the file by pressing Ctrl+O, Enter, Ctrl+X.
Test your Nginx configuration by running: sudo nginx -t
Reload Nginx by running: sudo service nginx reload
Step 4: Connect to MySQL from Windows
To connect to your MySQL database running on WSL from Windows, you need to change the authentication method for your MySQL user. By default, MySQL uses auth_socket authentication, which is not supported by most Windows applications. To change it to mysql_native_password, follow these steps:
Log in to your MySQL shell by running: sudo mysql -u root -p and entering your password.
Run this command to change the authentication method for your root user: ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'new-password'; where new-password is a strong password of your choice.
Exit the MySQL shell by running: exit
Download and install MySQL Workbench or any other MySQL client application on your Windows 11 PC.
Launch MySQL Workbench and create a new connection with these settings:
Connection name: WSL
Hostname: localhost
Port: 3306
Username: root
Password: new-password
Click on Test Connection and then on OK if it succeeds.
Step 5: Test your PHP MySQL development environment
To test your PHP MySQL development environment, you can create a simple PHP script that connects to your MySQL database and displays some data. To do that, follow these steps:
Create a new PHP file in the web root directory by running: sudo nano /var/www/html/test.php
Paste this code into the file:
<?php
// Connect to MySQL database
$mysqli = new mysqli("localhost", "root", "new-password", "mysql");
// Check connection
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
// Query the database
$sql = "SELECT user, host FROM user";
$result = $mysqli->query($sql);
// Display the results
if ($result->num_rows > 0) {
echo "<table border='1'>";
echo "<tr><th>User</th><th>Host</th></tr>";
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["user"] . "</td><td>" . $row["host"] . "</td></tr>";
}
echo "</table>";
} else {
echo "No results found";
}
// Close connection
$mysqli->close();
?>
Copy
Save and exit the file by pressing Ctrl+O, Enter, Ctrl+X.
Open your browser and go to http://localhost/test.php
You should see a table with the user and host columns from the mysql.user table.
Congratulations! You have successfully configured your Windows 11 PC with WSL for PHP MySQL development. You can now create and run your PHP projects using Nginx, PHP and MySQL on Ubuntu. Happy coding! 😊
How to configure Windows 11 PC with WSL for PHP MySQL development?
July 1, 2023
HowTo
No Comments
satya
Windows Subsystem for Linux (WSL) is a feature that allows you to run Linux applications natively on Windows 11. It is a great tool for developers who want to use Linux tools and frameworks on their Windows machines. In this article, we will show you how to install and configure WSL for PHP MySQL development.
Step 1: Enable WSL and install a Linux distribution
Before you can use WSL, you need to enable it on your Windows 11 PC. To do that, follow these steps:
After enabling WSL, you need to install a Linux distribution of your choice. You can do that from the Microsoft Store or from the command line. For this article, we will use Ubuntu 20.04 LTS as an example. To install it from the Microsoft Store, follow these steps:
Step 2: Install Nginx, PHP and MySQL on Ubuntu
Now that you have Ubuntu running on WSL, you can install the necessary packages for PHP MySQL development. To do that, follow these steps:
wsl
in the Run dialog box).sudo apt update
sudo apt install nginx php mysql-server
sudo mysql_secure_installation
and following the prompts.sudo service nginx start
andsudo service mysql start
Step 3: Configure Nginx, PHP and MySQL on Ubuntu
After installing Nginx, PHP and MySQL, you need to configure them to work together. To do that, follow these steps:
echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php
sudo nano /etc/nginx/sites-available/default
index index.html index.htm index.nginx-debian.html;
and addindex.php
after it, so it looks like this:index index.php index.html index.htm index.nginx-debian.html;
location / { ... }
and add this line inside it:try_files $uri $uri/ =404;
location ~ \.php$ { ... }
and uncomment it by removing the#
symbols at the beginning of each line.sudo nginx -t
sudo service nginx reload
Step 4: Connect to MySQL from Windows
To connect to your MySQL database running on WSL from Windows, you need to change the authentication method for your MySQL user. By default, MySQL uses auth_socket authentication, which is not supported by most Windows applications. To change it to mysql_native_password, follow these steps:
sudo mysql -u root -p
and entering your password.ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'new-password';
wherenew-password
is a strong password of your choice.exit
Step 5: Test your PHP MySQL development environment
To test your PHP MySQL development environment, you can create a simple PHP script that connects to your MySQL database and displays some data. To do that, follow these steps:
sudo nano /var/www/html/test.php
Copy
http://localhost/test.php
Congratulations! You have successfully configured your Windows 11 PC with WSL for PHP MySQL development. You can now create and run your PHP projects using Nginx, PHP and MySQL on Ubuntu. Happy coding! 😊
Related
MYSQLPHPWindowsWSL