DDEV is a fantastic open-source tool that simplifies setting up PHP development environments using Docker. If you’re looking to set up Laravel, one of the most popular PHP frameworks, using DDEV, you've come to the right place. This article will guide you through the process step-by-step, ensuring you have a smooth Laravel development experience.
Prerequisites:
- Docker installed and running on your machine.
- DDEV command-line tool installed.
- Basic knowledge of Laravel and PHP.
1. Creating a New Laravel Project
Before we integrate with DDEV, let's set up a new Laravel project. Open your terminal or command line and run:
composer create-project --prefer-dist laravel/laravel laravel-ddev
This command creates a new Laravel project named laravel-ddev
.
2. Setting up DDEV for Laravel
Navigate to your project directory:
cd laravel-ddev
Now, configure a new DDEV project:
ddev config
This will prompt you with a series of questions. For most of them, the default values will suffice. When asked for the project type, choose php
.
Once that's done, you'll want to adjust the webserver_type
and php_version
to values that Laravel supports. Update .ddev/config.yaml
:
webserver_type: nginx-fpm
php_version: "8.0"
Note: Always cross-check the PHP version with Laravel's documentation to ensure compatibility.
3. Database Configuration
Laravel uses .env
for environment-specific configurations. DDEV provides dynamic database credentials, which we can inject into Laravel's configuration. In your .env
file, update the database credentials to:
DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3306
DB_DATABASE=db
DB_USERNAME=db
DB_PASSWORD=db
4. Configuring the Web Server for Laravel
Laravel has specific rewrite rules, especially for its public directory. Update the .ddev/nginx-site.conf
to properly serve Laravel:
Replace the existing server block with:
server {
listen 80;
server_name _;
root /var/www/html/public;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
5. Starting DDEV
Start your DDEV environment:
ddev start
Once it's up, you can access your Laravel application via the provided URL, usually something like http://laravel-ddev.ddev.site
.
6. Finalizing the Laravel Installation
After starting DDEV, run the following commands to complete your Laravel setup:
- Migration (Setting up Laravel's default tables):
ddev exec php artisan migrate
- Key Generation (Setting application key):
ddev exec php artisan key:generate
Wrapping Up
You now have a Laravel 9/10 environment running smoothly on DDEV. The setup leverages Docker, which ensures that the application will run consistently across different setups, making it easier for team collaborations.
Remember to periodically check both Laravel's and DDEV's official documentation for updates and best practices. This guide should serve as a solid starting point, but the tech landscape is always shifting. Happy coding!
Discussion about this post