Skip to content

Instantly share code, notes, and snippets.

@laacz
Last active July 27, 2023 15:48
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save laacz/946861f541d226dcd6029bf1f1269fb7 to your computer and use it in GitHub Desktop.
Save laacz/946861f541d226dcd6029bf1f1269fb7 to your computer and use it in GitHub Desktop.
PHP 7.x (FPM), Apache2 on WSL

PHP-FPM, Apache with *.localhost virtualhosts on WSL

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.

PHP 7.x (FPM)

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.

Apache

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.

Webroot from Windows to WSL

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
@tassoman
Copy link

Nice snippet, can be upgraded to php7.4 now 🍻

@tassoman
Copy link

I wouldn't edit default virtual host but adding a new one by copying the default. Then the command a2ensite 010-test.conf and restart apache2

@laacz
Copy link
Author

laacz commented Feb 24, 2022

Avtually I'll have to take a time to update this to nginx and any PHP version. Currently my go-to is 8.1.

@tassoman
Copy link

I liked the mount part too. I'm actually doing nearly the same with symbolic links. Then developing code inside regular user directory. It's my fast-start for a dev machine.
In WSL2 you can run vscode and will run seamless in the windows environment. I'm just unsure about the newline handling... 🤔

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment