This will help installing simple PHP stack with *.localhost domains on Windows Subsystem for Linux in Windows 10. You'll have to add sudo
where appropriate.
You'll need to add an unofficial repository by awesome Ondřej Surý, but it's been stable for years and can be used in production.
sudo apt-get install -y python-software-properties
sudo add-apt-repository -y ppa:ondrej/php
sudo apt-get update -y
Install 7.1 (CLI and FPM):
sudo apt-get install php7.1 php7.1-cli php7.1-common php7.1-json php7.1-opcache php7.1-mysql php7.1-mbstring php7.1-mcrypt php7.1-zip php7.1-fpm
Install additional extensions with apt-get install php-7.1-*extension*
. You can get a list of all available PHP packages with sudo apt-cache search php7.1
.
You'll probably want composer. You know where to get it and what to do with it. It will hint if any PHP packages are missing.
You'll have to start FPM manually each time system is rebooted, however: service php7.1-fpm start
. Also, remember to restart it if adding or removing extensions.
Moving on to Apache. Trivial apt-get install apache2
will do. Enable modules:
a2enmod rewrite php7.1-fcgi proxy proxy_fcgi
a2enconf php7.1-fpm
We'll setup our virtualhost in a such way, that a folder test
maps to URL http://test.localhost
. Edit default virtualhost (probably at /etc/apache2/sites-enabled/000-default.conf
by adding following lines:
RewriteEngine On
RewriteMap lowercase int:tolower
RewriteCond %{HTTP_HOST} ^(.*)\.localhost$
RewriteRule ^(.*)$ "/your/webroot/with/hosts/${lowercase:%1}/$1"
Restart apache with service apache2 restart
.
Warning. These rewrite rules might (most probably will) break any local rewrites you might have in .htaccess.
If you'r development directory is on windows, no biggie. Just mount it under linux.
mkdir /var/www/webroot
mount /mnt/[disk]/[your/webroot] /var/www/webroot
Nice snippet, can be upgraded to php7.4 now 🍻