Categories
Beginner Tutorial

CakePHP on Windows with WSL 1

Not enough people use WSL. If you’re a web developer using windows I’m officially mandating that you use it! My pompous and unwarranted opinions aside I’ll show you how to set up CakePHP and WSL so that you’re no longer hindered by the bondage of Windows!

Enable WSL

In order to enable WSL, you need to have an admin account on your machine. Once you’ve contacted your IT department, or remembered your password you can:

  1. Open the powershell as admin
  2. Run the following command
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
  1. Restart the computer

After your computer is restarted you can install your preferred linux distro from the Microsoft store.

If you’ve never used linux before I’d recommend Ubuntu 20.04 and a quick tutorial on the linux command line.

Now that we have linux on our machine we can set up CakePHP!

Setting Up CakePHP

There are two possible paths when setting up CakePHP on our machine we can either use Docker or install all of the tools locally. We won’t cover how to set up Docker with WSL 1 as you need a windows pro license, and WSL 2 has much better support and can be done with a Windows Home license.

Installing PHP

If you’re proficient with the linux command line, then installing PHP is easy. Use the following command to install PHP.

sudo apt update
sudo apt-get install php
sudo apt-get install php-pear php-fpm php-dev php-zip php-curl php-xmlrpc php-gd php-mysql php-mbstring php-xml libapache2-mod-php

These commands will update your linux system and install PHP and its extensions. The extensions are necessary to run CakePHP.

Afterward you should be able to run php -v and see the current version of php!

Installing Composer

Composer is what we’ll use to initialize our CakePHP projects and install any plugins. You can install it using the following command.

curl -s <https://getcomposer.org/installer> | php

At this point you should be able to run this command, and get a CakePHP 4 application template.

php composer.phar create-project --prefer-dist cakephp/app:4.* application_name

If you wanted that application to be visible online I have a guide on how to host it on NameCheap!

Now that we’ve got Composer and PHP we just need the database!

Installing MySQL

Installing MySQL is only slightly more difficult than installing the other tools. The only difference is that you have to manually set up some security features.

To install MySQL run:

sudo apt install mysql-server

Run mysql —version to make sure all is well.

If you want to make your MySQL installation more secure then run:

sudo /etc/init.d/mysql start
sudo mysql_secure_installation

This will take you through updating the default security options for MySQL.

We’re all done setting up our WSL! Now let’s add some Window’s tools so we don’t have to spend all of our time learning Vim commands.

Installing VS Code

We’ll be using VS Code because it’s free and has plugins to make integrating with WSL easy.

You can install VS Code here!

After you install VSC you’ll need to install a plugin to be able to interface with your linux instance.

Open your WSL window again if you closed it, then type

code .

This will open a new VSC window and install the necessary plugin!

After that is finished installing you can now click on the green button on the bottom left of the screen to connect to WSL whenever you want. You can also use `ctrl+“ to open a CLI!

While you’re here you should also install some PHP linters and a theme that actually highlights the inline PHP tags.

Conclusion

Have any trouble with the guide? Let me know in the comments and I can help you out! Don’t forget I make new tutorials every Tuesday, and I do a bonus post on Thursdays! Hope to see you next week! While you’re here you should check out my post on how to get started in CakePHP.