在CentOS上部署Laravel项目的SSL证书,通常需要以下几个步骤:
- 获取SSL证书
首先,你需要获取一个SSL证书。你可以从Let’s Encrypt免费获取,或者从其他证书颁发机构购买。
以Let’s Encrypt为例,你可以使用Certbot来获取证书。首先,确保你的系统已经安装了Certbot和Nginx(或Apache)。
sudo yum install epel-release
sudo yum install certbot python2-certbot-nginx
然后,运行Certbot来获取证书:
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
按照提示操作,Certbot会自动配置Nginx并启用HTTPS。
- 配置Laravel项目
确保你的Laravel项目中的.env文件已经设置了正确的APP_URL,包括HTTPS协议:
APP_URL=https://yourdomain.com
此外,确保你的Laravel项目中的config/app.php文件中的url配置项也设置为相同的值:
'url' => env('APP_URL', 'https://yourdomain.com'),
- 配置Nginx
如果你使用的是Nginx,你需要修改Nginx的配置文件,以便正确处理HTTPS请求。通常,配置文件位于/etc/nginx/conf.d/目录下,文件名类似于yourdomain.com.conf。
以下是一个简单的Nginx配置示例:
server {
listen 80;
server_name yourdomain.com www.yourdomain.com;
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;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384';
ssl_prefer_server_ciphers on;
root /path/to/your/laravel/project;
index index.php index.html index.htm;
location /
{
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$
{
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
include fastcgi.conf;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
location ~ /\.ht
{
deny all;
}
}
修改配置文件后,重启Nginx:
sudo systemctl restart nginx
现在,你的Laravel项目应该可以通过HTTPS访问了。如果你遇到任何问题,请检查Nginx和Laravel的日志文件以获取更多信息。