WSL 2 is almost here! and if you are here, reading this post. Please consider reading about WSL 2
During one of the recent chit-chats, few of my contacts were asking me about the “real” business sense behind Linux on Windows. It is simple. Microsoft is:
Going to integrate LINUX so deep within Windows that, in the very near future, one don’t have to run a separate LINUX Server. Simple like that.
After experimenting with KALI Linux WSL, I managed to install Xfce & XRDP on Ubuntu 18.04 WSL & the only few things I couldn’t do with the WSL was starting services automatically when the subsystem starts. There are hacks, well, I don’t think they really worth a try soon as Microsoft will BE bringing it on to WSL pretty soon.
Now, let us get back to the topic. I’ve installed Ubuntu 18.04 WSL nothing less than half dozen times. Every time something goes wrong & I am forced to uninstall and re-install the WSL to continue with additional experiments. My last attempt was to install Kubuntu desktop, that screwed my Xfce & after 2 days of continuous efforts to “fix” few “unknown” things, I had to uninstall one Ubuntu WSL that had a fully functional WordPress instance :(
So, I decided to learn how to backup a functional WSL, so that if something goes wrong, I can restore & continue
I’ve ready at few places that Windows 10 version 1903 comes with WSL backup functionality out of the box. Until you decide to upgrade your box to 1903, you may use the below for backing up your dear WSL instance.
I came across a pretty decent thread here & a user Jaime’s answer is the apt ONE, missing only few details like /tmp folder.
Where /mnt/c is C:\> drive of your Windows Machine. You may change /mnt/c/ to /mnt/d/ or /mnt/e/, based on how your partitions are laid. Closely watch the excluded directories. You MUST exclude them.
My tar ball was around 1.5GB in size & I moved the tar ball to another machine which didn’t have WSL activated.
First you have to enable Window Subsystem for linux from Turn Windows Features on or off (If you are not restoring the backup to same Windows Machine). This will ask you to restart your Windows Machine.
If you are going to restore a Ubuntu 18.04 WSL rename launcher.exe to Ubuntu1804.exe & rename your tar ball to rootfs.tar.gz
Open an elevated command prompt & switch to D:\wsl
and just execute, for example “Ubuntu1804.exe”
It will take a while for the installation to complete. As the superuser thread states, it installs and opens the WSL for you. From next time, all you need to do is to start Ubuntu1804.exe & your WSL instance is up and running!
However, I had an issue with the restored instance. It was related to missing /tmp folder. I found a solution for it with nixcraft article.
The good thing is I found a “solution” myself after wasting almost a working day, just because I was stupid enough not to read my own POST dealing with it :/
I will publish a new POST detailing what has changed & how you could avoid getting into a mess with WLS LAMP installations & configurations in a day or two.
Update: 18th April 2019
Although I was able to successfully install LAMP on multiple Windows 10 stand alone machine WLS, yet to figure out a way to install MySQL on a Windows machine running WLS that has already joined a domain. I am not sure whether it is due to some domain policy related issues, access issues or due to a corruption with my Windows. I will definitely try to find out the root cause (hopefully).
====
Usually I prefer a virtual machine for all my Linux/Development requirements. Well, Microsoft is “trying” to setup a Linux subsystem within Windows 10 from a while, which is “yet” to be fully functional in many means. Many benchmarks proven that Microsoft Subsystem for Linux for has the poorest I/O which Microsoft claims, will be addressed in the future releases.
So, why someone will choose WSL against a VM which has better functionalities? From an eager inquisitive person’s perspectives, I will choose WSL for just couple of reasons and they are:
WSL could access the full hardware, when a VM has limited resources. Ie, say your computer running has 4 cores and 8GB memory, you will be limited to 2 cores for your VM (anything beyond that would cause unexpected issues) and maximum of 4GB memory. Here WSL don’t have such limitations, it could access all your 4 cores and the maximum memory & could be launched like just another win32 application!
I’ve been trying to setup WSL using both Ubuntu & OpenSUSE Leap 42.3 over my Windows 10 1709 & I had better results with Ubuntu. OpenSuse has many issues those were beyond my skills to address and resolve. Hence, I chose to stick with Ubnutu for now.
So, installation of WSL on Windows 10 recent versions were made pretty easier compared to the previous releases. A seasoned Windows user could install Ubuntu for WSL very easily by following the instructions below
Once rebooted, from the Windows store, search for Ubuntu or OpenSUSE and just install it! That simple it is.
Once installed, as mentioned with the first image, one can start the WSL either from command prompt or by clicking the tile that is available with the start menu.
As a developer, I would take the pain to setup the WSL for ONLY few reasons like testing PHP application(s) those are based on MySQL database (and outta curiosity)
So next, let us see we can setup LAMP server (LAMP is an archetypal model of web service stacks, named as an acronym of the names of its original four open-source components: the Linux operating system, the Apache HTTP Server, the MySQL relational database management system (RDBMS), and the PHP programming language. The LAMP components are largely interchangeable and not limited to the original selection. As a solution stack, LAMP is suitable for building dynamic web sites and web applications) on our new WSL
You may refer to this post for a detailed information about how to install individual components like apache, MySQL and PHP. Please note, the link that I provided deals with PHP5, which is not anymore available from Ubuntu repositories, hence I chose to go ahead with PHP7.0 for my WSL instance
Issue the following command(s) to install Apache, MySQL,PHP and phpMyAdmin
During the installation, you will be prompted for setting up MySQL root credentials, which is mandatory in order to access the MySQL server at later stages using phpmyadmin module
For PHP, you must install libapache2-mod-php, else you would experience php pages being blank when functions like phpinfo(); are called.
Once the packages are installed (without errors) you can restart apache to verify everything is working fine
[code language="text" gutter="false"]
sudo service apache2 restart or sudo /etc/init.d/apache2 restart
[/code]
If your windows box already has IIS listening to port 80 and a Windows instance for MySQL is listening to port 3306, then you must change the ports for both apache and MySQL before restarting the services.
MySQL installation will GET IN TO a number of undocumented errors if you have another instance of MySQL listening to 3306 (ie, on your Windows box). Hence make sure that you shutdown the instance before attempting to install MySQL on WLS. Please remember, WLS is not another virtual machine, so if a port is open on your “HOST”, that is definitely a conflict for the WLS.
For changing the port of Apache server edit the following file
During the installation you will be prompted for selecting the http server stack and password for MySQL root account. You must provide the root password that you have set during the MySQL installation at this prompt
Once the phpmyadmin is installed, you need to manually enable couple of extensions and they are dealt like following:
(I did a fresh WLS instance on 17th April 2019 & the phpmyadmin installation has changed a little bit here and there, asking for the webserver & an additional setup that helps to setup the phpmyadmin user passwords etc)
[code language="text" gutter="false"]
sudo service apache2 restart
sudo service MySQL restart
[/code]
Example:
[code language="text" gutter="false"]
rajesh@rt04:/etc/apache2$ sudo service apache2 restart
* Restarting Apache httpd web server apache2 [ OK ]
rajesh@rt04:/etc/apache2$ sudo service mysql restart
* Stopping MySQL database server mysqld [ OK ]
* Starting MySQL database server mysqld [ OK ]
rajesh@rt04:/etc/apache2$
[/code]
Now you can see that there are no errors or warning while starting both apache and MySQL. So let us go ahead and check phpmyadmin
That’s all folks!
There is no shutdown for WSL, just type “exit” and you are good. I am yet to test whether mere exiting does anything harmful to running instances of apache and MySQL. Until I find them…
Finally
I don’t really understand why, yet, many users are asking for a “way” to ssh into the Linux subsystem. The pre-installed openBDS ssh server doesn’t allow a connection from clients like putty. Hence a re-installation of SSH server is required.
[code language="text" gutter="false"]
sudo apt-get remove --purge openssh-server
sudo apt-get install openssh-server
sudo service ssh --full-restart
Create a firewall inbound rule if required
[/code]
Now you should able to connect to WSL instance using a client like putty.