在 Linux + Laravel 环境下部署 SSL,通常分为 申请证书 和 Web 服务器配置 两部分。下面以最常见的 Nginx + Let’s Encrypt(免费 SSL) 为例说明,也会补充 Apache 方式。
Ubuntu / Debian
sudo apt update
sudo apt install certbot python3-certbot-nginx -y
CentOS
sudo yum install epel-release -y
sudo yum install certbot python3-certbot-nginx -y
sudo certbot --nginx -d example.com -d www.example.com
按提示输入邮箱、同意协议
✅ Certbot 会自动:
sudo certbot renew --dry-run
Let’s Encrypt 证书 90 天 有效期,Certbot 会自动续期。
如果你已有 .crt 和 .key 文件:
server {
listen 443 ssl;
server_name example.com;
ssl_certificate /etc/ssl/example.com/fullchain.crt;
ssl_certificate_key /etc/ssl/example.com/private.key;
root /var/www/laravel/public;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
server {
listen 80;
server_name example.com;
return 301 https://$host$request_uri;
}
重启 Nginx:
sudo systemctl restart nginx
sudo a2enmod ssl
sudo a2ensite default-ssl
<VirtualHost *:443>
ServerName example.com
DocumentRoot /var/www/laravel/public
SSLEngine on
SSLCertificateFile /path/fullchain.crt
SSLCertificateKeyFile /path/private.key
</VirtualHost>
sudo systemctl restart apache2
.env
APP_URL=https://example.com
App/Providers/AppServiceProvider.php
public function boot()
{
if (env('APP_ENV') === 'production') {
\URL::forceScheme('https');
}
}
✅ mixed content 错误
→ 静态资源使用 asset() 或 secure_asset()
✅ 证书不生效
→ 检查防火墙、域名解析、Nginx 配置
✅ 本地开发
→ 可用 mkcert 或 laravel valet
如果你用的是 具体发行版(Ubuntu/CentOS)或面板(宝塔/1Panel),可以告诉我,我可以给你更精确的部署步骤。