Laravel Herd Deep Dive: Môi Trường Development Native Hoàn Hảo
Giới Thiệu
Laravel Herd là một công cụ development environment native cho macOS và Windows, được phát triển bởi Beyond Code (team đứng sau Expose, Tinkerwell). Không giống như Docker hay XAMPP, Herd chạy PHP và các services trực tiếp trên máy của bạn với hiệu năng tối ưu.
Tại Sao Chọn Herd?
| Tiêu chí | Herd | Docker/Sail | Valet | XAMPP |
|---|---|---|---|---|
| Hiệu năng | ⭐⭐⭐⭐⭐ | ⭐⭐⭐ | ⭐⭐⭐⭐⭐ | ⭐⭐⭐⭐ |
| Setup time | 1 phút | 5-15 phút | 5 phút | 10 phút |
| Resource usage | Thấp | Cao | Thấp | Trung bình |
| Multiple PHP | ✅ Dễ dàng | ✅ | ✅ | ❌ |
| GUI | ✅ Đẹp | ❌ | ❌ | ✅ |
| Giá | Free/Pro | Free | Free | Free |
Cài Đặt
macOS
- Tải từ herd.laravel.com
- Mở file
.dmgvà kéo vào Applications - Khởi động Herd - Done!
Windows
- Tải installer từ website
- Chạy installer
- Khởi động lại máy (nếu cần)
- Xong!
Sau khi cài đặt, Herd tự động setup:
- PHP (phiên bản mới nhất)
- Nginx
- dnsmasq (cho
.testdomains) - Composer
- Laravel Installer
Giao Diện Và Các Tính Năng Cơ Bản
Sites
Herd quản lý các sites thông qua "parked" directories. Mặc định là ~/Herd:
# Mọi folder trong ~/Herd sẽ tự động có domain .test
~/Herd/
├── blog/ → blog.test
├── shop/ → shop.test
└── api-backend/ → api-backend.test
Thêm thư mục khác:
# Terminal
herd park ~/Projects
# Hoặc qua GUI: Sites → Park Path
PHP Versions
Một trong những tính năng mạnh nhất của Herd là quản lý multiple PHP versions:
# Xem phiên bản hiện tại
herd php -v
# Liệt kê các phiên bản có sẵn
herd php:list
# Chuyển PHP global
herd use php@8.3
# Chuyển PHP cho một site cụ thể
herd isolate php@8.2 --site=legacy-project
Qua GUI:
- Click vào icon Herd
- Sites → Chọn site
- PHP Version → Chọn version
SSL/HTTPS
# Bật SSL cho một site
herd secure blog
# Tắt SSL
herd unsecure blog
# Bật SSL cho tất cả sites
herd secure --all
Sau khi secure, truy cập https://blog.test - certificate được tự động trust!
Herd Pro: Các Tính Năng Nâng Cao
Herd Pro ($99/năm) bổ sung nhiều tính năng quan trọng cho professional development:
1. Database Services
# MySQL
herd services mysql start
herd services mysql stop
# PostgreSQL
herd services postgres start
# Redis
herd services redis start
# Kiểm tra trạng thái
herd services
Kết nối database:
MySQL:
- Host: 127.0.0.1
- Port: 3306
- User: root
- Password: (empty)
PostgreSQL:
- Host: 127.0.0.1
- Port: 5432
- User: postgres
- Password: (empty)
2. Xdebug Integration
# Bật Xdebug
herd xdebug on
# Tắt Xdebug (tăng performance)
herd xdebug off
VS Code Configuration:
// .vscode/launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Listen for Xdebug",
"type": "php",
"request": "launch",
"port": 9003,
"pathMappings": {
"/Users/you/Herd/blog": "${workspaceFolder}"
}
}
]
}
3. Mail Catcher (Mailpit)
Herd Pro tích hợp Mailpit để bắt emails trong development:
# Khởi động Mailpit
herd services mailpit start
# Mở Mailpit UI
herd open mailpit
# Hoặc truy cập http://localhost:8025
Laravel configuration:
# .env
MAIL_MAILER=smtp
MAIL_HOST=127.0.0.1
MAIL_PORT=1025
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
4. Node.js Integration
# Xem Node version
herd node -v
# Sử dụng Node version cụ thể
herd node use 20
# Chạy npm/yarn thông qua Herd
herd npm install
herd yarn dev
5. MinIO (S3-compatible Storage)
herd services minio start
# Truy cập MinIO Console
# http://localhost:9001
# Username: minioadmin
# Password: minioadmin
Laravel configuration:
FILESYSTEM_DISK=s3
AWS_ACCESS_KEY_ID=minioadmin
AWS_SECRET_ACCESS_KEY=minioadmin
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=local
AWS_ENDPOINT=http://localhost:9000
AWS_USE_PATH_STYLE_ENDPOINT=true
Cấu Hình Nâng Cao
Custom Nginx Configuration
Herd cho phép custom Nginx config per-site:
# Tạo custom config
herd nginx:config blog
Điều này tạo file ~/.config/herd/config/valet/Nginx/blog.test:
server {
listen 127.0.0.1:80;
server_name blog.test;
root /Users/you/Herd/blog/public;
# Custom configuration
client_max_body_size 100M;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
PHP Configuration
# Xem PHP config
herd php:ini
# Mở php.ini để edit
herd php:ini --edit
Common adjustments:
; ~/.config/herd/config/php/82/php.ini
memory_limit = 512M
upload_max_filesize = 100M
post_max_size = 100M
max_execution_time = 300
; Opcache settings
opcache.enable = 1
opcache.memory_consumption = 256
opcache.interned_strings_buffer = 16
opcache.max_accelerated_files = 10000
Environment Variables
Thiết lập environment variables cho tất cả PHP processes:
# Mở file config
herd php:ini --edit
# Hoặc tạo file ~/.config/herd/config/php/environment
CUSTOM_VAR=value
API_KEY=secret
CLI Commands Reference
Site Management
# Liệt kê tất cả sites
herd sites
# Link một thư mục với tên custom
herd link ~/Projects/my-app my-custom-name
# Truy cập tại my-custom-name.test
# Unlink
herd unlink my-custom-name
# Mở site trong browser
herd open blog
# Xem logs
herd log blog
# Tail logs realtime
herd log blog --tail
PHP Management
# PHP REPL
herd tinker
# Chạy PHP code
herd php -r "echo phpinfo();"
# Composer commands
herd composer install
herd composer require laravel/sanctum
# Artisan commands
herd artisan migrate
herd artisan queue:work
Service Management
# Xem tất cả services
herd services
# Restart tất cả
herd restart
# Restart Nginx
herd restart nginx
# Restart PHP
herd restart php
Workflows Thực Tế
Workflow 1: New Laravel Project
# 1. Tạo project mới
cd ~/Herd
laravel new my-project
# 2. Secure với HTTPS
herd secure my-project
# 3. Mở trong browser
herd open my-project
# 4. Start services cần thiết
herd services mysql start
herd services redis start
# 5. Setup database
herd artisan migrate
Workflow 2: Clone Existing Project
# 1. Clone vào Herd directory
cd ~/Herd
git clone git@github.com:company/project.git
# 2. Install dependencies
cd project
herd composer install
herd npm install
# 3. Copy environment
cp .env.example .env
herd artisan key:generate
# 4. Nếu project cần PHP cũ
herd isolate php@8.1
# 5. Setup database
herd artisan migrate --seed
# 6. Build assets
herd npm run build
Workflow 3: Multiple Projects với PHP Khác Nhau
# Project 1: Laravel 11 (PHP 8.3)
herd isolate php@8.3 --site=modern-app
# Project 2: Legacy Laravel 8 (PHP 8.0)
herd isolate php@8.0 --site=legacy-app
# Project 3: Symfony 6 (PHP 8.2)
herd isolate php@8.2 --site=symfony-app
# Kiểm tra
herd sites
# modern-app.test PHP 8.3
# legacy-app.test PHP 8.0
# symfony-app.test PHP 8.2
Tích Hợp IDE
VS Code
Cài đặt extension "Laravel Herd" để có:
- Site switching
- PHP version switching
- Service management
- Quick commands
Settings:
{
"php.validate.executablePath": "/Applications/Herd.app/Contents/Resources/herd/bin/php",
"php.executablePath": "/Applications/Herd.app/Contents/Resources/herd/bin/php"
}
PHPStorm
- Settings → PHP → CLI Interpreter
- Add → Local Path
- Path:
/Applications/Herd.app/Contents/Resources/herd/bin/php
Xdebug:
- Settings → PHP → Debug
- Port: 9003
- Start Listening for PHP Debug Connections
Troubleshooting
Site Không Load
# Kiểm tra Herd đang chạy
herd status
# Restart services
herd restart
# Kiểm tra DNS
ping blog.test
# Phải trả về 127.0.0.1
502 Bad Gateway
# Kiểm tra PHP đang chạy
ps aux | grep php-fpm
# Restart PHP
herd restart php
# Xem error log
herd log blog
Database Connection Refused
# Kiểm tra service đang chạy
herd services
# Start MySQL
herd services mysql start
# Kiểm tra port
lsof -i :3306
SSL Certificate Issues
# Regenerate certificates
herd unsecure blog
herd secure blog
# Trust certificate manually (macOS)
herd trust
So Sánh Với Các Giải Pháp Khác
Herd vs Laravel Sail (Docker)
Khi nào dùng Herd:
- Development cá nhân
- Cần performance cao
- Không muốn learn Docker
- Multiple PHP versions cho các projects khác nhau
Khi nào dùng Sail:
- Team development (consistent environment)
- Production parity quan trọng
- Complex service dependencies
- CI/CD integration
Herd vs Valet
Herd thực chất là evolution của Valet với:
- GUI đẹp hơn
- Dễ quản lý hơn
- Pro features (databases, services)
- Windows support
Nếu bạn đang dùng Valet và hài lòng, không nhất thiết phải chuyển. Nhưng cho người mới, Herd là lựa chọn tốt hơn.
Tips & Tricks
1. Alias Hữu Ích
# ~/.zshrc hoặc ~/.bashrc
alias h="herd"
alias hp="herd php"
alias hc="herd composer"
alias ha="herd artisan"
alias hs="herd services"
alias ho="herd open"
2. Quick Database Reset
# Reset và re-seed
ha migrate:fresh --seed
# Với specific seeder
ha migrate:fresh --seed --seeder=TestDataSeeder
3. Temporary PHP Version
# Chạy command với PHP version cụ thể mà không isolate
herd php@8.1 artisan migrate
herd php@8.2 composer install
4. Performance Monitoring
# Xem memory usage
herd php -r "echo memory_get_usage(true) / 1024 / 1024 . ' MB';"
# Opcache status
herd php -r "print_r(opcache_get_status());"
Kết Luận
Laravel Herd là công cụ development environment tốt nhất hiện nay cho PHP developers:
- Zero configuration: Cài đặt và chạy trong 1 phút
- Native performance: Nhanh hơn Docker đáng kể
- Multiple PHP versions: Dễ dàng chuyển đổi
- Beautiful GUI: Không cần terminal cho mọi thứ
- Pro features: Database, Xdebug, Mail catcher...
Nếu bạn là Laravel developer và chưa dùng Herd, hãy thử ngay - bạn sẽ không muốn quay lại!