SSL و Let's Encrypt برای Laravel — HTTPS رایگان روی Nginx
بدون HTTPS مرورگر «ناامن» نشان میدهد، cookie session در خطر است و SEO ضربه میخورد. Let us Encrypt گواهی رایگان ۹۰ روزه میدهد — با Certbot تمدید خودکار میشود. Laravel بعد از SSL باید APP_URL و trusted proxy درست باشد.
پیشنیاز
- دامنه به IP سرور point شده (A record)
- Nginx با server_name صحیح — پیکربندی Nginx
- پورت 80 و 443 باز در firewall
نصب Certbot (Ubuntu)
sudo apt update
sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d example.com -d www.example.com
Certbot block SSL را به Nginx اضافه و redirect HTTP→HTTPS میسازد.
تمدید خودکار
sudo certbot renew --dry-run
timer systemd معمولاً نصب میشود — cron لازم نیست.
تنظیم Laravel
APP_URL=https://example.com
SESSION_SECURE_COOKIE=true
// AppServiceProvider — اگر پشت load balancer
URL::forceScheme('https');
فقط وقتی proxy SSL terminate میکند — TrustProxies middleware.
HSTS (اختیاری پس از اطمینان)
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
قبل از فعالسازی مطمئن شوید HTTPS پایدار است — rollback سخت.
mixed content
asset با http:// — از asset() و APP_URL https استفاده کنید. Vite ASSET_URL در production.
wildcard certificate
DNS challenge — certbot certonly --manual یا plugin DNS provider. برای subdomain زیاد.
امنیت تکمیلی
چکلیست امنیت — headerها، CSP.
اشتباهات
- APP_URL هنوز http
- گواهی منقضی — renew fail بدون alert
- staging روی production cert
جمعبندی
SSL گام اجباری production. Deploy کامل: Ubuntu + Nginx. Docker: Compose با reverse proxy جدا.