This repository includes a set of scripts useful for installing and configuring an initial set of tooling for PHP development on WSL using Ubuntu.
- Enable WSL on Windows.
- Install Ubuntu for WSL.
- Start Ubuntu to initialize it and setup your WSL user.
- Clone the repo.
git clone https://github.com/patrickcarlohickman/wsl-setup.git
- Go to the
setup
directory.cd wsl-setup/setup
- Copy
.env.example
to.env
.cp .env.example .env
- Update the
.env
values. - Go through the files in the
resources
directories and update them with your information. - Run
sudo -i $HOME/wsl-setup/setup/setup-wsl.sh
. This will take a while to run. - Exit WSL and reconnect, or just start a new shell with
exec $SHELL -l
. - Run
startup
to ensure all your services are up and running. - Copy the scripts in the
bin
directory to somewhere on the Windows host machine.
- Upgrades the Ubuntu distribution.
- Installs a
wsl.conf
file to enable filesystem metadata. - Sets the system timezone.
- Installs a startup script to start desired services (ex: apache, mysql, redis).
- Installs a sudoers file to allow the startup script to be run by the wsl user.
- Installs default user files to setup bash aliases, git configuration, git ignore, and a git commit message template.
- Creates new SSH keys or copies SSH keys from Windows host.
- Installs initial software.
- phpenv
- PHP (version set in .env)
- Apache
- MySQL 5.7
- Redis
- Composer
- NVM
- Yarn
- Ngrok
- FreeTDS
There is a startup script installed at /usr/local/bin/startup.sh
that is used to start up all the services (apache, mysql, redis, php-fpm, etc.). There is also a startup
alias defined that runs this script as sudo. When you connect to your WSL instance, just run startup
and it will (re)start all the services.
The setup/resources/mysql/create-databases-mysql.sh
script is available to create databases in MySQL. It reads from the create-databases-mysql.sql
file in the same directory. To add new databases, just update setup/resources/mysql/create-databases-mysql.sql
and run the setup/resources/mysql/create-databases-mysql.sh
script.
This setup is designed to allow multiple versions of PHP running at the same time. This is done through phpenv. While you can use phpenv directly to install new versions of PHP, it is better to use the install script at setup/resources/php/install-php.sh
. This script will:
- Automatically resolve certain build issues with building older versions of PHP (older dependencies, expecting files in certain locations, etc.)
- Setup a new PHP log file for the version installed
- Configure the PHP-FPM user and listener
- Install the PHP-FPM service script
When you setup a new website in your vhost directory (/var/www/vhost
or the value specified in the VHOST_DIRECTORY
variable in the .env
file), you'll need a new apache config file for the new website. To set this up, use the site install script at setups/resources/apache/make-site.sh
. This script will:
- Create a new apache site config file at
/etc/apache2/sites-available
. This config file will:- Enable a new virtual host for
your-domain.test
andwww.your-domain.test
- Enable http and https
- Configure the site to use the PHP version specified
- Configure new error and access logs for the site
- Add a forwarding host fix for ngrok
- Enable a new virtual host for
- Add the new site to the ngrok config file
- Generate the openssl private key if one doesn't exist
- (Re)generate the openssl self-signed cert with the new site added
Usage: make-site.sh < domain > < php_version > [ directory [ ngrok_start_name ] ]
Notes:
- The script assumes the document root directory is at
<domain>/public
. If that is not the document root, specify the directory using the third parameter. - The ngrok start name defaults to the domain. Use the fourth parameter to change this.
- Make sure to remember to update the host computer hosts file to access the new website at the new domain (
bin/add-host.bat
helps with this). - If using https, the SSL cert is self-signed, so the browser will complain about it, but you should be able to click through.
There are a couple useful helper scripts for Windows located in the bin
directory. These are meant to be run from the Windows host machine, so they will need to be copied somewhere onto the Windows host machine. Ex: cp -r bin /mnt/c/WSL
update-hosts-ips.bat
- With WSL2, you cannot access the domains hosted on the WSL machine using the loopback address (127.0.0.1) in the Windows hosts file. The Windows hosts file will need to be kept up-to-date with the IP address of the WSL machine.
- This script will get the current WSL IP address and update the hosts file with it for any host under the
.test
TLD.
add-host.bat
- When you add a new site inside of WSL, you'll need to add a new hosts entry in the Windows hosts file to point to the new site.
- This script will prompt for the new host name and add it to the end of the Windows hosts file with the WSL IP address already assigned.
Ignore wsl-setup.bat
. That was the start of attempting to install WSL from scratch and allow multiple WSL instances on one host machine, each with different software setups. Very early, and probably won't get much more attention. I think if you want to go that way, may as well use Docker.
Contributions are welcome. Please see CONTRIBUTING for details.
The MIT License (MIT). Please see License File for more information.