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 using Apache and phpMyAdmin.
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 Apache, 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 Apache, PHP and MySQL by running: sudo apt install apache2 php mysql-server
Secure your MySQL installation by running: sudo mysql_secure_installation and following the prompts.
Start Apache and MySQL services by running: sudo service apache2 start and sudo service mysql start
Step 3: Configure Apache, PHP and MySQL on Ubuntu
After installing Apache, 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 Apache configuration file by running: sudo nano /etc/apache2/sites-available/000-default.conf
Find the line that says <VirtualHost *:80> and add this line below it: ServerName localhost
Find the line that says DocumentRoot /var/www/html and add this line below it: DirectoryIndex index.php index.html index.htm
Save and exit the file by pressing Ctrl+O, Enter, Ctrl+X.
Enable the PHP module by running: sudo a2enmod php7.4
Test your Apache configuration by running: sudo apache2ctl configtest
Restart Apache by running: sudo service apache2 restart
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: Install phpMyAdmin on Ubuntu
phpMyAdmin is a web-based tool that allows you to manage your MySQL databases using a graphical interface. To install phpMyAdmin on Ubuntu, follow these steps:
Install phpMyAdmin by running: sudo apt install phpmyadmin
During the installation, you will be asked to choose the web server that should be automatically configured to run phpMyAdmin. Select apache2 and press Enter.
You will also be asked whether to configure the database for phpMyAdmin with dbconfig-common. Select Yes and press Enter.
You will then be asked to provide the password for the database’s administrative user. Enter the password you set for the root user in step 4 and press Enter.
You will also be asked to provide a password for the phpMyAdmin application itself. Enter a strong password of your choice and press Enter. Confirm the password by entering it again and pressing Enter.
Restart Apache by running: sudo service apache2 restart
Step 6: 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.
To test your phpMyAdmin installation, follow these steps:
Open your browser and go to http://localhost/phpmyadmin
You should see the phpMyAdmin login page. Enter your username (root) and password (new-password) and click on Go.
You should see the phpMyAdmin dashboard with your MySQL databases and tables.
Congratulations! You have successfully configured your Windows 11 PC with WSL for PHP MySQL development using Apache and phpMyAdmin. You can now create and run your PHP projects using Apache, PHP, MySQL and phpMyAdmin on Ubuntu. Happy coding! 😊
How to configure Windows 11 PC with WSL for LAMP (Linux, Apache, MySQL, PHP) 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 using Apache and phpMyAdmin.
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 Apache, 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 apache2 php mysql-server
sudo mysql_secure_installation
and following the prompts.sudo service apache2 start
andsudo service mysql start
Step 3: Configure Apache, PHP and MySQL on Ubuntu
After installing Apache, 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/apache2/sites-available/000-default.conf
<VirtualHost *:80>
and add this line below it:ServerName localhost
DocumentRoot /var/www/html
and add this line below it:DirectoryIndex index.php index.html index.htm
sudo a2enmod php7.4
sudo apache2ctl configtest
sudo service apache2 restart
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: Install phpMyAdmin on Ubuntu
phpMyAdmin is a web-based tool that allows you to manage your MySQL databases using a graphical interface. To install phpMyAdmin on Ubuntu, follow these steps:
sudo apt install phpmyadmin
sudo service apache2 restart
Step 6: 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
To test your phpMyAdmin installation, follow these steps:
http://localhost/phpmyadmin
Congratulations! You have successfully configured your Windows 11 PC with WSL for PHP MySQL development using Apache and phpMyAdmin. You can now create and run your PHP projects using Apache, PHP, MySQL and phpMyAdmin on Ubuntu. Happy coding! 😊
Related
PHPWindowsWSL