阅读量:3
在Linux上为Laravel项目配置HTTPS,你需要遵循以下步骤:
1. 获取SSL证书
你可以从多个来源获取SSL证书,例如:
- Let’s Encrypt:提供免费的SSL证书。
- 购买商业证书:从证书颁发机构(CA)购买。
使用Let’s Encrypt获取证书
-
安装Certbot:
sudo apt update sudo apt install certbot -
运行Certbot获取证书:
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com这将自动配置Nginx并生成SSL证书。
2. 配置Nginx
假设你已经安装并配置了Nginx,以下是一个基本的Nginx配置示例:
server {
listen 80;
server_name yourdomain.com www.yourdomain.com;
location / {
return 301 https://$host$request_uri;
}
}
server {
listen 443 ssl;
server_name yourdomain.com www.yourdomain.com;
ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
root /var/www/yourdomain.com/public;
index index.php index.html index.htm;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; # 根据你的PHP版本调整
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location / {
try_files $uri $uri/ /index.php?$query_string;
}
}
3. 配置Laravel
确保你的Laravel项目配置正确,特别是数据库连接和其他环境变量。
4. 重启Nginx
应用新的Nginx配置:
sudo systemctl restart nginx
5. 测试HTTPS连接
打开浏览器并访问https://yourdomain.com,确保一切正常工作。
6. 自动续期证书
Let’s Encrypt证书通常每90天过期一次。你可以使用Certbot的自动续期功能:
sudo certbot renew --dry-run
如果一切正常,你可以设置一个cron作业来自动续期:
sudo crontab -e
添加以下行:
0 0,12 * * * certbot renew --post-hook "systemctl reload nginx"
通过以上步骤,你应该能够在Linux上成功为Laravel项目配置HTTPS。
以上就是关于“如何在Linux上为Laravel项目配置HTTPS”的相关介绍,筋斗云是国内较早的云主机应用的服务商,拥有10余年行业经验,提供丰富的云服务器、租用服务器等相关产品服务。云服务器资源弹性伸缩,主机vCPU、内存性能强悍、超高I/O速度、故障秒级恢复;电子化备案,提交快速,专业团队7×24小时服务支持!
简单好用、高性价比云服务器租用链接:https://www.jindouyun.cn/product/cvm