Many developers use Windows for PHP projects, we will cover a basic XAMPP Setup for a Laravel project. Following these easy steps will help you run your PHP projects and install Laravel on Windows in a matter of minutes.
Installing XAMPP for Laravel
- Download XAMPP for Windows from their official site: https://www.apachefriends.org/download.html
- For our example, we’ve chosen PHP 7.1 and XAMPP 32 bit
- Run the installer and select Apache, MySQL and phpMyAdmin
- It’s recommended to make the installation in C:\ to avoid issues with Windows User permissions (or the drive where your Windows is installed)
- After it’s complete do not start anything just yet.
Setting PHP from Windows CMD
- Open “Pannello di controllo” -> “Impostazioni di sistema avanzate”
- Open “Variabili d’ambiente”
- On the top window select Path and click on Edit
- We need to enter “;” and the path to XAMPP’s PHP
C:\xampp\php
- We will also add MySQL’s Path, click on New again and enter
C:\xampp\mysql\bin
Composer from Windows CMD
- Download Composer’s Windows .exe installer: https://getcomposer.org/download/
- Place the installer and run it from the added to Path directory – C:\xampp\php
Now that we have Apache, PHP, MySQL and Composer installed, we’re ready to install Laravel, set a Virtual Host with a custom domain for local development. XAMPP’s Apache uses the htdocs folder for serving HTTP requests, we’ll set our custom Laravel Virtual Host under C:\xampp\htdocs\example\ which will be opened locally from example.com.
Install Laravel on Windows with Composer
- Open Window’s CMD or PowerShell
cd C:\xampp\htdocs
- Install Laravel using Composer
composer create-project --prefer-dist laravel/laravel example
Creating MySQL Database for Laravel
- Open the CMD or PowerShell again
- Access the MySQL CLI
mysql -u root -p
- Create Database for Laravel
CREATE DATABASE laravel DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
Setting Laravel’s .env file
- Navigate to C:\xampp\htdocs\example
- Open the config/database.php file and edit the MySQL Connection settings
'mysql' => [ 'read' => [ 'host' => [ '192.168.1.1', '196.168.1.2', ], ], 'write' => [ 'host' => [ '196.168.1.3', ], ], 'sticky' => true, 'driver' => 'mysql', 'database' => 'laravel', 'username' => 'root', 'password' => 'THE ONE YOU CREATED THE USER WITH', 'charset' => 'utf8mb4', 'collation' => 'utf8mb4_unicode_ci', 'prefix' => '', ],
Virtual Host with XAMPP
- Navigate to C:\xampp\apache\conf\extra
- Open httpd-vhosts.conf and append:
NameVirtualHost *:80 <VirtualHost example.com:80> DocumentRoot "C:\xampp\htdocs\example\public" ServerName example.com ServerAlias www.example.com <Directory "c:/xampp/htdocs/example/public"> Require all granted </Directory> </VirtualHost>
Enable mod_rewrite for Laravel
- Open the httpd.conf file in C:\xampp\apache\conf\extra
- Look for LoadModule rewrite_module modules/mod_rewrite.so and remove the ; at the start
Setting local domain with hosts file
- Navigate to c:\Windows\System32\Drivers\etc
- Open the hosts file with Administrator Privileges
- Append the line at the bottom
127.0.0.1 www.example.com example.com
Start Apache and MySQL, your local Laravel project will be opened from example.com and you’re ready to start working on it.