LAMP Dev Stack in Windows

Right away, just a note that this tutorial of course assumes you’ve already installed WSL on your Windows 10 machine, which we discussed here. Also, another note, this is not a fancy polished tutorial. We’re not going to have a bunch of screenshots, instead just info you need to get the job done. With that out of the way, let’s go…

STEP 1 – Initial prep work

First, make sure you are running Ubuntu 18 not Ubuntu 16 using this command: lsb_release -a

If you aren’t on Ubuntu 18, you’ll need to either install the Ubuntu 18 app or if you are using the general Ubuntu app, you have to use this command to upgrade: sudo do-release-upgrade. There are more details here regarding how to upgrade your Ubuntu distro.

If you want to update your bash theme to make it look exactly as it would if you were running Ubuntu machine, there are tips for how to do so. You'll end up with something looking like this:

Back on track, it's time to run this command to make sure your Ubuntu install is all up to date:

sudo apt update && sudo apt upgrade

Now before we proceed, you should know you could just skip almost all of the step below and just do: sudo apt install lamp-server^ phpmyadmin. However really it’s not much more work to do this properly and have full control over everything. With that caveat out of the way, let’s get rolling…

Step 2 – Install Apache2

First run these commands, one at a time:

sudo apt install apache2
sudo service apache2 start

If you encounter error / warning message, then use this command sudo nano /etc/apache2/apache2.conf and add the following to the bottom of the .conf file you are loading:

#
# fix error in WSL (Protocol not available: AH00076: Failed to enable APR_TCP_DEFER_ACCEPT)
# https://github.com/Microsoft/WSL/issues/1953#issuecomment-295370994
#
AcceptFilter http none
AcceptFilter https none

Enable Apache modules you need, such as SSL rewrite and SSL (one command at a time):

sudo a2enmod rewrite
sudo a2enmod ssl
sudo service apache2 restart

Now we need to create a symlink to your main web dev (websites) folder in your Windows file system to your Linux system. So do something like this on the command line in Ubuntu:
sudo ln -s /mnt/c/websites /var/www/devroot

Step 3 – Install MySQL Server + PHP

We get started by installing MySQL like so:

sudo apt install mysql-server

Note: You will not be asked to create a password, but on a local install that should be fine.

Next up run the following commands:

sudo usermod -d /var/lib/mysql mysql
sudo apt install libapache2-mod-php php php-curl php-gd php-mbstring php-mysql php-xml php-zip
sudo phpenmod mbstring

Now we need to make changes to the default PHP settings, which are found in your php.ini file. Search and find these three items below and put in whatever value you want (if you want to change them at all):

sudo nano /etc/php/7.2/apache2/php.ini
  • max_execution_time = 120
  • post_max_size = 64M
  • upload_max_filesize = 64M

In your Windows file systems (specifically in your “websites” folder we talked about above and created a symlink for), create a root localhost folder called “htdocs”, or whatever you want to call it.

sudo nano /etc/apache2/sites-enabled/000-default.conf

In this file above, update document root to the above symlink and add in the code below to enable htaccess file processing. So it should look something like this:

DocumentRoot /var/www/devroot/htdocs

AllowOverride All
Require all granted

Lastly, we need to start up the MySQL server like so:

sudo service mysql start

Step 4 – Install PHPMyAdmin

Technically you don't need this but it's definitely nice to have, so let's do it.

sudo apt install phpmyadmin
  • Make sure apache2 is selected for configuration
  • Yes, configure db. Setup a password.

Now add the phpMyAdmin configuration to apache2. Edit apache2.conf file (like so if you recall, sudo nano /etc/apache2/apache2.conf and add the following line at the bottom:

Include /etc/phpmyadmin/apache.conf

Now, restart Apache2, with a command you’ll end up being familiar with:

sudo service apache2 restart

Unfortunately we’re not done yet since MySQL 5.7, MySQL root login requires some permissions edits, so run this command, changing password as to what you want:

sudo mysql --user=root mysql

Then enter this on the MySQL command line:

CREATE USER 'phpmyadmin'@'localhost' IDENTIFIED BY 'some_pass';
GRANT ALL PRIVILEGES ON *.* TO 'phpmyadmin'@'localhost' WITH GRANT OPTION;
FLUSH PRIVILEGES;

Great news!! We can now login at: http://localhost/phpmyadmin/ using username of: phpmyadmin and of course the password you just edited above.

Step 4.5 – Virtual Hosts and SSL

First create this file for whatever your sitename is:

sudo nano /etc/apache2/sites-available/mysitename.localhost.conf

Now paste this in and edit as need be. Note: this is for an SSL site, if you aren’t using SSL on a particular local site, you can take some things out.

ServerName tester.localhost

ServerAdmin webmaster@localhost
DocumentRoot /var/www/devroot/tester

SSLEngine On
SSLCertificateFile "/mnt/c/Users/onetr/.localhost-ssl/tester.localhost.crt"
SSLCertificateKeyFile "/mnt/c/Users/onetr/.localhost-ssl/tester.localhost.key"

Options Indexes FollowSymLinks
AllowOverride All
Require all granted

ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined

With are the prep work done, you just need to know how to enable / disable your virtual host site. You do that like so, but NOTE you do not included .conf part of the filename, so something like “mysitename.localhost” is the filename.

Then you just need to restart Apache. You should know this command by now, but just in case here it is:

sudo service apache2 restart

Now you can enable and disable your site anytime using these two commands (you dissin' my site man!?):

sudo a2ensite filename
sudo a2dissite filename

==========
REFERENCES:

  • http://www.codza.com/blog/lamp-in-wsl-win10
  • https://www.freecodecamp.org/news/setup-a-php-development-environment-on-windows-subsystem-for-linux-wsl-9193ff28ae83/
© 2019 Creative Logic Tech Solutions.