HOW TO: Setup WSL to run a FULL LAMP stack in win10

   (0 reviews)

Michael

yes you read that right, a full LAMP stack in windows. If you are like me, you'd much rather develop in a an environment much closer to that of a server than in WAMP or any of the other wamp stacks available. virutal machines can accomplish but there are problems with this, resources being one of them, the other is slow down as if you are doing shared folders, they are being transferred over your network between the host and guest OS. Now with windows 10 (anniversary edition and alter) you can install the windows subsytem for linux, which provides an environment for a linux userspace to operate.

requirements:

  • windows 10 (64 bit) anniversary edition or later
  • familiarity with windows command prompt/powershell and bash in linux.

how to check you windows version: open Settings > System > About. Look for the OS Build and System Type fields.

system.png

if your build is below 14393, check for updates.

Install:

Open Settings > Update and Security -> For Developers, select the "Developer mode radio button".

updateandsecurity.png

once you have this enabled, from the search for "Turn windows features on or off" (turn is usually enough), in the list look for "Windows Subsystem for Linux (beta)", select it and click okay.

windowsfeatures.png

it will do a bit of windows voodoo magic and it will then prompt you to restart, do as it says. (you need to restart, or the bash infrastructure will not be running as that can only be loaded during boot up)

after you have restarted, open a command prompt/powershell (right click on the start button, and select it from there). type in "bash" and hit enter.

bashshellinstall.png

accept the license, read it if you feel compelled too. after you have accepted it, the Ubuntu user mode image will be download and "bash on ubuntu on windows" shortcut will be added to your start menu. the image can take awhile to download, it's under 1gb. once it has installed, you can now enter bash by typing "bash" from a command prompt/powershell or using the shortcut created.

The first time you run bash, it will ask you to create a new user and enter in a new password (remember these two things, as the user here will be independent of your user account). 

bashshellinstall.png

most of the above information came from: https://msdn.microsoft.com/en-us/commandline/wsl/install_guide

Now onto the fun stuff, install a LAMP stack:

Step One:

next you will want to change the colors for the text in bash, as it is horrible on the default.

nano ~/.bashrc

(you don't need sudo in front of the above command as your user owns the file) go to the very end of the file and add this:

alias codolor='LS_COLORS="ow=01;36;40" && export LS_COLORS'

ctrl+o (to save) and ctrl+x (to exit)

next thing you want to do is add your system to host file (for some reason this isn't done, and it causes issues). in bash type:

cat /etc/hostname

hostname.png

it will show the systems hostname, to the super observant, you will have noticed this is what appears after your user name in bash.

next type:

sudo nano /etc/hosts

below:

127.0.0.1 localhost 

add:

127.0.1.1 <hostname>

<hostname> being the host named you got from cat /etc/hostname. 

addtohosts.png

once you have don this, do ctrl+o this will prompt you to save the file, to exit, ctrl+x will exit nano. (get familiar with these two steps, you can only edit these files via bash and nano is a lot easier to learn than vi :) )

Step Two: Update System

next, lets update the system:

sudo apt-get update && sudo apt-get upgrade

this will download a list of packages from the repo's and upgrade them. remember this one too, its a great tool to have :) 

Installing the LAMP stack: 

now if you know how to do this or have your own method or want to install a different setup (like nginx or one of the packaged stacks you can skip this). first thing we will want to do, this is ubuntu 14.04 which is almost 3 years out of date, so we are gonna add a repo to get the latest PHP. on bash, it will give you a description of the ppa and a prompt to continue, press enter.

sudo add-apt-repository ppa:ondrej/php

and then at bash:

sudo apt-get update 

(i know a bit tedious...) after it is done, on bash do this:

sudo apt-get install apache2  mysql-server-5.6 libapache2-mod-auth-mysql php7.0 php7.0-mysql php7.0-zip php7.0-mbstring php7.0-dom php7.0-xml php7.0-mysql php7.0-gd php7.0-curl

(you can install php 5.6 instead or test out php7.1, just replace the 7.0 with 5.6 or 7.1 on the php packages). you can also copy and past this into bash :) you don't have to type it all out.

once it has installed, if you weren't prompted to set a password for mysql, run the following two commands:

sudo mysql_install_db
sudo /usr/bin/mysql_secure_installation

(just answer yes to the prompts)

there are a few things we need to do first:

sudo nano /etc/apache2/apache2.conf

add these two lines to the conf:

Servername localhost
AceptFilter http none

ctrl+o then ctrl+x.

now we will setup homdir in apache:

sudo a2enmod userdir

next:

sudo nano /etc/apache2/mods-available/php7.0.conf

find:

<IfModule mod_userdir.c>
    <Directory /home/*/public_html>
       php_admin_flag engine Off
    </Directory>
</IfModule>

and comment out php_admin_flag engine Off, make it look like this:

<IfModule mod_userdir.c>
    <Directory /home/*/public_html>
#        php_admin_flag engine Off
    </Directory>
</IfModule>

ctrl+o then ctrl+x.

now if you are like me, you wont want to shoot yourself in the head by being forced to edit any and all IPS files via nano...or worst vi (unless you like that sort of thing, who am i to judge). we are going to symlink a folder on our C:\ drive to our home folder in bash.

first thing, create a folder on your C:\ Drive (in windows explorer), call it public_html (you can create this anywhere you'd like, even on another drive as bash is mounted with all your windows drives), the name of the folder has to be public_html or other wise userdir will not work.. 

in bash do the following:

ln -s "/mnt/c/public_html" /home/<username>/public_html

replace <username> with the username you created when you installed bash. now if you went to that folder:

cd ~/public_html

ls

it will display the contents of the folder (unless its empty then it wont show anything).

next in bash, type in:

touch ~/init

this will create a new file in your home directory, called init.

next:

nano ~/init

add this to it:

#!/bin/bash
service apache2 start
service mysql start

ctrl+o then ctrl+x

in bash:

sudo sh ~/init

this will now start up apache and mysql for you. you can now go to your browser in windows and type in:

http://localhost/~myusername

and it will show this all wasn't in vain, if there is nothing or an error...i might be able to help, but this worked for me :)  

NOTE: you will need to leave the bash window open, as soon as you close your apache is exited as well. so after restarts you will need to start bash again execute :

sudo sh ~/init

 

Edited by Michael





User Feedback

Create an account or sign in to leave a review

You need to be a member in order to leave a review

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now

There are no reviews to display.