[WSL] PHP Development Environment by “Windows Subsystem for Linux” (Part 1)

This article requires Windows 10 (Build Version 1709 [The Fall Creators Update]) or above to work.

Although WSL exists since Version 1607 [Anniversary Update], it’s harder to setup and more buggy.

In this tutorial we will setup this development environment in WSL (In Part 1)
Apache 2.4
MySQL 5.7
PHP 7.2 (Run as FPM)
phpMyAdmin
Composer

With those tools under Windows 10 (In Part 2)
PHPStorm
Bash and Linux CLI tools
PHP Deployer

A. Installation

1. Enable “Windows Subsystem for Linux”

Start > Search “Turn Windows features on or off”
The result should show up after typing first two words

Check “Windows Subsystem for Linux”, Click “OK”, Reboot

2. Install Ubuntu 18.04 on WIndows

Start > Search”Microsoft Store”, open it and search for “Ubuntu 18.04”
Install and launch it first time, it takes some time to initialize.
Enter desired username and password for the Linux subsystem.

3. Start enjoying your Linux subsystem!

B. Install and Configure Packages

Part B works exactly the same way as Ubuntu 18.04.
If you are familiar with Ubuntu 18.04, you may skip this part and use your own method for installation.

1. Update all installed packages
Let’s do a apt dist-upgrade first

sudo -i
apt update
apt dist-upgrade

2. Install Apache, PHP, MySQL, git

apt install -y php7.2-cli php7.2-fpm php7.2-opcache php7.2-mysql php7.2-mbstring php7.2-xml php7.2-zip php7.2-bz2 php7.2-curl php7.2-gd
apt install -y apache2
apt install -y mysql-server
apt install -y git

3. Configure PHP

Enable PHP FPM for Apache

a2enconf php7.2-fpm
a2enmod proxy_fcgi

Increase PHP resources limits for development

sed -i 's|post_max_size = [0-9]\+M|post_max_size = 128M|g' /etc/php/7.2/fpm/php.ini
sed -i 's|upload_max_filesize = [0-9]\+M|upload_max_filesize = 128M|g' /etc/php/7.2/fpm/php.ini
sed -i 's|memory_limit = [0-9]\+M|memory_limit = 512M|g' /etc/php/7.2/fpm/php.ini
sed -i 's|max_execution_time = [0-9]\+|max_execution_time = 300|g' /etc/php/7.2/fpm/php.ini
sed -i 's|gc_maxlifetime = [0-9]\+|gc_maxlifetime = 86400|g' /etc/php/7.2/fpm/php.ini

sed -i 's|pm.max_children = [0-9]\+|pm.max_children = 25|g' /etc/php/7.2/fpm/pool.d/www.conf
sed -i 's|pm.start_servers = [0-9]\+|pm.start_servers = 5|g' /etc/php/7.2/fpm/pool.d/www.conf
sed -i 's|pm.min_spare_servers = [0-9]\+|pm.min_spare_servers = 5|g' /etc/php/7.2/fpm/pool.d/www.conf
sed -i 's|pm.max_spare_servers = [0-9]\+|pm.max_spare_servers = 10|g' /etc/php/7.2/fpm/pool.d/www.conf

4. Configure Apache2

Enable rewrite module

a2enmod rewrite

Enable .htaccess in all directories

sed -i ':a;N;$!ba;s/AllowOverride None/AllowOverride All/3' /etc/apache2/apache2.conf

5. Configure MySQL

Remove test user and databases

mysql_secure_installation

It will make several changes to MySQL login crediitcal
(Press ENTER multiple time until finish if you don’t care)

Enable MySQL root password login
MySQL 5.7 under Ubuntu 18.04 do not allow password login for security reason,
But it doesn’t matter much in our development machine, so we change it in order to use phpMyAdmin.

mysql
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'some_strong_password';
FLUSH PRIVILEGES;
exit

6. Install and enable phpMyAdmin (From git)

git clone --depth=1 --branch=STABLE git://github.com/phpmyadmin/phpmyadmin.git /var/www/phpMyAdmin
randomBlowfishSecret=`openssl rand -base64 32`;
sed -e "s|cfg\['blowfish_secret'\] = ''|cfg['blowfish_secret'] = '$randomBlowfishSecret'|" /var/www/phpMyAdmin/config.sample.inc.php > /var/www/phpMyAdmin/config.inc.php
cd /var/www/phpMyAdmin
composer install --no-dev
echo 'Alias /phpMyAdmin /var/www/phpMyAdmin' > /etc/apache2/conf-available/phpmyadmin.conf
a2enconf phpmyadmin

7.  Install composer (From official installer)

wget https://getcomposer.org/installer
php installer --filename=composer --install-dir=/usr/bin

8. Start everything!

service apache2 start
service php7.2-fpm start
service mysql start

Yes, something unusual start appear from here.
Service under WSL do not start automatically, after installation or after reboot.

You may also encounter “Invalid argument: AH00076: Failed to enable APR_TCP_DEFER_ACCEPT” when starting Apache2.
It doesn’t harm anything, we will remove that later, however.

9. Basic installation complete

You now have PHP, Apache, MySQL working, and phpMyAdmin under http://127.0.0.1/phpMyAdmin
All ports listened by applications inside WSL are accessible in Windows.

In next part we will go though the special attributes and gotchas of the Linux subsystem.
Make it share files and play nice with your favorite Windows tools and IDEs!

 

 

 

One Reply to “[WSL] PHP Development Environment by “Windows Subsystem for Linux” (Part 1)”

  1. If you are macOS user, ServBay.dev is a good tool, especially for the beginners. It handles all PHP, MariaDB, PostgreSQL versions, plus Redis and Memcached. Run multiple PHP instances simultaneously and switch easily. This tool has made my PHP dev simpler. Worth a shot!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.