jon thompson

Windows 10 LAMP Stack Part 1 : Install & Setup

Posted on 2019-02-05 15:02:53
Difficulty level: Easy

Run a standard Linux Apache web server from inside Windows 10 with this quick step-by-step guide on installing & configuring Debian, Apache, MariaDB, and PHP7

Building a full LAMP stack in Windows 10

Ever wanted to run your own internal web server for development purposes, but never had a spare PC knocking about to use?

Well, if you've got Windows 10 installed, and you've added the Windows Subsystem for Linux from the Windows 10 Fall Creators Update, then you've already got the capability right there, probably without even knowing it

L is for Linux

The first thing you're going to need to do is to make sure that the Windows Subsystem for Linux is installed. See this Microsoft link for details on how to install it

We've chosen Debian as our flavour of Linux for this tutorial. Others are available in the App store, although installing a LAMP stack may be different from the guide here

Start the Debian instance from the Windows Start menu and log in. On first run, you'll be asked to set your username and password.

The first thing we need to do is update the current system:

sudo apt-get update
sudo apt-get -y upgrade

Because we're going to be downloading and installing packages over https, we need to make sure that the secure certificates we've got are up to date on our server.

Run the following command to get the latest CA certificates from the Debian repository:

sudo apt-get -y install ca-certificates apt-transport-https

This next command will install a few utilities that I use for personal preference:

sudo apt-get -y install vim dnsutils man

We're going to be installing Composer to manage our PHP package installs. To use this effectively, we need a few utilities installing first:

sudo apt-get -y install curl ssh git zip unzip

When the updates have finished, we're ready to start installing all the components of our LAMP stack.

A is for Apache

Apache is an Open Source web server, a program that lets you display basic web pages written in HTML and CSS

Installing this is simply a matter of running:

sudo apt-get -y install apache2

I prefer to use Apache's mod_vhost_alias module to allow me to run multiple sites under a directory, without having to create a new vhosts entry and restart Apache every time.

It's a powerful module, and if you're planning on designing and building multiple sites on your local LAMP server, then I'd recommend installing it.

We also need here to enable mod_rewrite, so we can use htaccess rules, and mod_headers 

sudo a2enmod rewrite headers vhost_alias

To check all is working, we need to start Apache, and then check that the test page comes up in your browser at http://localhost

sudo service apache2 restart

M is for MySQL/MariaDB

We're ready to install the database part of our LAMP stack now. The standard here is MySQL.  

However, because of issues with the license type that Oracle have given MySQL, and the company's history, Debian now ships MariaDB as its default database type.

There's no difference from a user viewpoint - MariaDB is designed as a direct drop-in replacement for MySQL, and Debian associates all mysql commands with MariaDB

We're going to install MariaDB here, and make sure that the databases are stored in the standard folder

sudo apt-get -y install mysql-server
sudo service mysql stop
sudo usermod -d /var/lib/mysql mysql sudo service mysql start

A default installation has a few security holes in it, but it's simple to fix with a built-in command. 

Simply follow the on-screen recommendations when prompted.

sudo mysql_secure_installation

P is for PHP

PHP is a server-side scripting language used mainly for dynamic websites. You've seen it used everywhere - this site is using Wordpress, which is written in PHP

Because Debian doesn't provide the latest versions of packages, preferring to maintain stable, known working versions, we need to do a bit more to get PHP running

We're after PHP7.3, which at the time of writing is the latest version. The package for this is maintained by a 3rd party, so we need to add their repository to our system, so that we can install it normally.

We need to run this as the root user directly, as we're adding to the apt repositories

sudo su
wget -q -O- | apt-key add -
echo "deb  stretch main" | tee /etc/apt/sources.list.d/php.list
sudo apt-get update

Now we've got their repository set up, we can install PHP and a few modules

sudo apt-get -y install php7.3 php7.3-cli php7.3-mysql php7.3-gd php7.3-curl php7.3-mbstring php7.3-xml php7.3-zip

Finally, we need to install Composer to manage our PHP packages.

This command will install composer in a global scope, so you don't need to specify the path to it when you use it

sudo curl -sS | sudo php -- --install-dir=/usr/local/bin --filename=composer

Almost there

We've installed all we need to get a working LAMP stack running under Windows. All we need to do now is do one final update, and then start our services

sudo apt-get update
sudo apt-get -y upgrade
sudo service apache2 restart
sudo service mysql restart

Your website files can now be added to /var/www/html and available at http://localhost