How to set up a high-performance local Laravel development environment for Windows with pretty URLs (without Docker)

August 27, 2023
Zacharias Creutznacher
Zacharias Creutznacher
Chief Technology Officer (CTO)

Slow setups

We at Laracraft tried a lot of Windows based local Laravel development environments, but we were not happy with the performance and usability of the known environments like: php artisan serve, Vagrant/Homestead, Laravel Sail, Laragon etc.. These tools are for sure very good, if you maybe setup a new Laravel project, which has not jet very complex operations and requests and you only work with a small database. But if you need more performance for you heavy requests, these tools are not sufficient:

php artisan serve:

  • slow - no pretty url - can only handle one request at a time (single-threaded)

Homestead/Vagrant:

  • slow - complex setup - resource intensive (CPU and Memory)

Laravel Sail:

  • slow - no pretty urls - very exhausting configuration when you need to run multiple apps at a time (port forwarding) - can only handle one request at a time (single-threaded)

Laragon:

  • Laragon is quite good, but not as fast as our solution - you can't run multiple apps with different php versions at a time

Let's create a fast setup!

So we decided to go for a Laravel setup with WSL and native php, mysql and nginx installation and configuration. Though it might be intimidating to set it up, we are really impressed by the very fast performance and the very flexible configuration. With this guide it should be easy to setup, so this is definitely worth it!

We managed to reduce execution time on complex operations from 13s down to 1.5s, which is an increase of about 800%!

Requirements

Make sure your Windows version is compatible with WSL: Windows 10, Version 2004 or higher(Build 19041 or higher), or Windows 11

Install WSL

Open your PowerShell as an Administrator and hit:

wsl.exe --install

Wait for the installation to be finished, this may take a while. After that, restart your computer and when you login to your windows account, the command prompt should open automatically and finish the WSL installation. After that you will be asked to enter your user credentilas for WSL.

Install PHP

Now open your WSL terminal and execute these commands one by one:

sudo apt-get update
sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo apt install openssl unzip php8.2-cli php8.2-bcmath php8.2-curl php8.2-mbstring php8.2-mysql php8.2-tokenizer php8.2-xml php8.2-zip php8.2-fpm

Install Composer

To install composer, execute these commands one by one:

cd ~
curl -sS https://getcomposer.org/installer -o /tmp/composer-setup.php
HASH=`curl -sS https://composer.github.io/installer.sig`
php -r "if (hash_file('SHA384', '/tmp/composer-setup.php') === '$HASH') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
sudo php /tmp/composer-setup.php --install-dir=/usr/local/bin --filename=composer

Install MySQL

Now lets install MySQL:

sudo apt-get install mysql-server

After the installation is finished, we change the root password for MySQL to <code>password<code>, or what ever you prefer. Therefor you have to execute mysql as root:

sudo mysql

With this command you can change the password:

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';

Now leave mysql with enter <code>exit;<code> and test the login as root with the password you set:

mysql -u root -p

If this works, go one with the next step!

Install Laravel

You may already have a project, but for the sake of this tutorial, we install a fresh new Laravel application:

cd ~
mkdir code
cd code
composer create-project laravel/laravel example-app

We need a database, let's create one:

mysql -uroot -p -e "create database example_app"

Adjust your <code>.env<code> file and set:

DB_DATABASE=example_app
DB_USERNAME=root
DB_PASSWORD=password

Now lets migrate the database:

cd example-app
php artisan migrate

Install and configure NGINX

The last thing we need now is a webserver, let's install NGINX:

sudo apt-get install nginx

As we want to use a pretty url, make sure to add example-app.test to your hosts file, so that when we enter it in the browser, it resolves to our local nginx. Therefor open the Editor as Administrator and open: <code>C:\Windows\System32\drivers\etc\hosts<code> (make sure to list "All files", in the open file dialog, else the host file will not be shown).

Now add the following new line at the end of the file:

127.0.0.1 example-app.test

Let's now configure nginx! Create the following file:

sudo nano /etc/nginx/sites-available/example-app

And paste the default Laravel Nginx config:

Adjust the <code>server_name<code>, <code>root<code> and maybe the <code>fastcgi_pass<code> sockets (if you installed a differenct php version) directives to your needs!

Now enable this site:

sudo ln -s /etc/nginx/sites-available/example-app /etc/nginx/sites-enabled/example-app

Check if nginx config syntax is correct and reload:

sudo nginx -t
sudo nginx -s reload

We experienced that it's the best to change nginx and php-fpm user to your current user, so you will never get file permission issues. The thing is, if you just change the rights of your curent folder to the <code>ww-data<code> group, you will still get issues, if new files gets created on a git pull, for instance.

So let's change nginx to your current user at the very top of the file:

sudo nano /etc/nginx/nginx.conf

Also change fpm user and group to your current user:

sudo nano /etc/php/8.2/fpm/pool.d/www.conf

Now restart fpm and nginx:

sudo service php8.2-fpm restart
sudo service nginx restart

Done

Thats it, now hit <code>http://example-app.test<code> in your browser and enjoy these very fast response times!

We managed to reduce execution time on complex operations from 13s down to 1.5s, which is an increase of about 800%!

code

What's a Rich Text element?

The rich text element allows you to create and format headings, paragraphs, blockquotes, images, and video all in one place instead of having to add and format them individually. Just double-click and easily create content.

Static and dynamic content editing

A rich text element can be used with static or dynamic content. For static content, just drop it into any page and begin editing. For dynamic content, add a rich text field to any collection and then connect a rich text element to that field in the settings panel. Voila!

How to customize formatting for each rich text

Headings, paragraphs, blockquotes, figures, images, and figure captions can all be styled after a class is added to the rich text element using the "When inside of" nested selector system.

H4

H5
H6

http://google.de

<code>test123<code>

<span style="color:red;">asdfasdf</span>

composer require spatie/laravel-artisan-dispatchable

Put your ideas into practice!
Today is the day to build the business of your dreams. Share your mission with the world - and inspire your customers.
Start now