How to setup Apache, MySQL and PHP in Linux Subsystem for Windows 10
As we know right now Microsoft Windows 10 has a new additional feature called Linux Subsystem for Windows. This feature gives the leverage of running a full bash shell on Windows. This is not a Virtual PC. I repeat this is not a Virtual PC. It’s a fully working Linux Subsystem under Windows 10. I have created a five-minute video how to install the Linux Subsystem for Windows 10. Have a look on this one before you begin.
If you had a look in the Windows Store, there are few options on choose. I have chosen Ubunutu 16.04 LTS.
Lets have a look on what we have on this article:
- Setting up Apache 2 in Linux Subsystem for Windows 10
- Setting up MySQL Server in Linux Subsystem for Windows 10
- Setting up PHP in Linux Subsystem for Windows 10
- Visual Studio Code and PHP Extensions
- Few tweaks to work on PHP, Visual Studio Code with Linux Subsystem for Windows
Setting up Apache 2 in Linux Subsystem for Windows 10
In this section I will go through how to install Apache 2 on Linux Subsystem for Windows 10. Apache is a web server that we require to execute PHP web applications in Ubuntu. Before going further make sure you have setup Linux Subsystem for Windows 10.
OK, let’s fire up the bash shell. After a successful installation it should be in the start menu or you can use the search bar in the task bar and fire it up.
The default bash shell should look like this.
Type this command and press ENTER in the shell to check the current Ubuntu version installed.
$ lsb_release -a
You should see an output like this.
OK, we are good to go with installing Apache 2. Lets type the following command in the bash shell.
$ sudo apt-get install apache2
After typing this command, you will be presented with a question whether to install Apache or not. Default answer is “Y”, so just press ENTER to continue. Wait till everything is setup and you are presented with the default prompt. This means you have done installing Apache 2. Next is you need to start the Apache service. Type the following command to start the Apache service.
$ sudo service apache2 start
This command will prompt you with a password to enter. Enter the password you have given when you install Linux Subsystem for Windows. After that Apache service will start.
NOTE: Ignore the warning. We can still use Apache without any problem.
Now let us fire-up our favorite browser and test whether the Apache is running and we have a welcome page. In the browser address bar type “localhost” and press ENTER. You should a Apache welcome screen like this.
Congratulations!!! We are done installing Apache 2.
Setting up MySQL Server in Linux Subsystem for Windows 10
Good, now we have installed Apache 2 successfully. Let us get on with the next step of installing MySQL Server. Fire-up your bash shell if you have not yet opened it. And type the following command in the shell.
$ sudo apt-get install mysql-server
Answer “Y” for the prompt to continue the installation of MySQL Server. This will start installing MySQL Server and its necessary libraries to execute. In the middle of the installation you will be prompted with a password to your server instance. Enter and confirm the password and continue on the installation.
Lets start the MySQL server by typing the following command.
$ sudo service mysql start
This should start the MySQL Server instance.
Now lets test our MySQL instance by typing this command.
$ mysql -uroot -p
This command will prompt you for the password. Type the password you gave during the installation process of MySQL Server. And you should see the mysql prompt.
Lets try a quick command to check everything is fine. Type this statement in the mysql prompt and press enter.
mysql> show databases;
You should see an output like this.
Excellent, now you have a running MySQL server instance. Type this command to exit from the prompt.
You are back in the bash shell prompt.
Setting up PHP in Linux Subsystem for Windows 10
So far we have installed Apache2 and MySQL server. Let us have a look on how to setup PHP. Here we go. Fire-up your bash shell and type the following command to install PHP and its required libraries.
$ sudo apt-get install php libapache2-mod-php php-mcrypt php-mysql
Above command will install few dependencies with the PHP installation itself. PHP module for Apache and an encryption library for PHP plus PHP MySQL connector. Yes you can execute separate commands for each as well.
Answer yes to continue the installation and in couple of minutes you are done. Lets test our PHP installation. Type the following command to reach the default Apache document root which is /var/www/html
$ cd /var/www/html
Lets create a test file using a default text editor found in Ubuntu which is vim.
$ sudo vim test.php
You will have to type “sudo” before the vim command, cause /var/www/html directory by default owned by the root user. You should see a screen like this.
- Press “i” or INSERT in your keyboard to start editing
- Type the following code.
<?php phpinfo(); ?>
- Your editor should look like this
- Press ESC key
- Type :wq (colon , w and q) to write to the disk and quit the editor
Now you have created a php file to test our installation. Fire-up your favorite browser and type localhost (if you didn’t see the default Apache page, make sure you start the Apache service). Now type following address in the address bar:
You should this nice screen if everything is OK.
Excellent. Now we have a fully running Apache, MySQL and PHP in our Windows 10 system.
Visual Studio Code and PHP Extensions
OK, now you need some tooling to develop your PHP applications. Yep there are few good tools. But lets have a look on this great light weight editor which is Visual Studio Code. You can download using this link below.
- Open Visual Studio Code
- Click on the “Extensions” button in the left side tool bar.
- Type “php crane” in the search text box in the Extensions explorer. (This extension does not require PHP to be installed in your system. Cause PHP is installed under Linux Subsystem for Windows, we just need the IntelliSense for PHP)
- Click on “Install” button for PHP IntelliSense and PHP Debug to get the IntelliSense and Debug support for PHP.
- Now click “Reload” button to load the installed extension.
- Again, click on the “Explorer” icon in the left tool bar and click on “New file” link in the startup screen.
- Select a location to save your file.
- Name it as “index.php” and hit save.
- Ignore the warning from Visual Studio Code that we don’t have PHP installed. We just require the syntax highlighting and the IntelliSense.
- Now start typing some PHP code and you will see the IntelliSense kicking on.
Now you have done setting up the code editor as well.
Few tweaks to work on PHP, Visual Studio Code with Linux Subsystem for Windows
Wonderful. Now we have everything setup to do some PHP Coding. All in one place. But there is a small issue we need to address. How do we access the Linux Subsystem from Windows or the other way around. Yep there is a way to access your windows drive from the bash shell. This is how do you access.
- Open the bash shell.
- Type the following command.
- Now you should see all the mounted drives and the path to access. And the one in the red box is you need.
- Type these two command and you will have access to your C drive in windows.
$ cd /mnt/c$ ls
- As you see some folders you don’t have access. But you have access to files and folders you have created later. Like this “Projects” directory.
- This how you access the windows folders from Linux Subsystem. I have created a folder named “PHP” under “Projects” folder windows. And the path looks like this.
- This path should be accessed via Linux Subsystem using this command.
$ cd /mnt/c/Projects/PHP/
Now you can open Visual Studio Code and do your work here.
- Open Visual Studio Code
- Click on Open Folder button and navigate to your PHP test folder (mine C:\Projects\PHP )
- Now click “New file” or “New File” icon to create a new file.
- Type the name as “index.php” and press enter.
- Type some PHP code testing, or copy paste the code below.
<<!DOCTYPE html><html> <head>
<meta charset="utf-8" /> <title>PHP Application in Linux Subsystem for Windows</title> </head> <body> <h1>PHP Application in Linux Subsystem for Windows<h1> <?php $welcome = "<h2>Hello PHP World !!</h2>";
print($welcome); ?> </body></html>
- Hit CTRL + S to save.
Still you cant test this application. Since Apache doesn’t know about his folder. Apache looks at /var/www/html by default. Lets tell our linux system to create a link under /var/www/html directory and look at our C:\Projects\PHP folder (make sure you give the correct path to your windows folder)
- Open the bash shell
- Navigate to /var/www/html directory using this command
$ cd /var/www/html
- Type the following command to create the link.
$ sudo ln -s /mnt/c/Projects/PHP /var/www/html/myphp
- Type this command to see the link is created.
- Nice, you should see the symlink is created. Now you have a direct connect with your windows folder and the Linux directory.
- Let’s fire-up the browser and test our code (Make sure Apache is running)
- In the address bar type localost:/myphp and press ENTER.
- Excellent!!! You are done now.
- Let’s open the VS Code and do some changes, refresh the browser. You are good to go.
Now you have a fully running system to build PHP applications under Windows 10 with Linux Subsystem for Windows 10.
Update: An updated article of same is here, but focused on NodeJS, ReactJS and VS Code