温馨提示×

laravel项目在centos上如何部署HTTPS

小樊
41
2025-12-07 13:11:08
栏目: 智能运维

在CentOS上部署Laravel项目的HTTPS,你需要遵循以下步骤:

  1. 安装Nginx和SSL证书

首先,确保你已经安装了Nginx。如果没有,请运行以下命令来安装:

sudo yum install epel-release
sudo yum install nginx

接下来,为你的域名获取SSL证书。你可以使用Let’s Encrypt免费获取证书。安装Certbot及其Nginx插件:

sudo yum install certbot python2-certbot-nginx

运行Certbot以获取并配置SSL证书:

sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com

按照提示操作,Certbot将自动配置Nginx以使用HTTPS。

  1. 配置Nginx

打开Nginx配置文件,通常位于/etc/nginx/nginx.conf/etc/nginx/conf.d/default.conf。确保以下设置已添加或修改:

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 TLSv1.1 TLSv1.2;
    ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256: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/public;
    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;
    }
}

yourdomain.com替换为你的域名,将/path/to/your/laravel/project/public替换为你的Laravel项目的实际路径。

  1. 重启Nginx

保存更改并重启Nginx以应用新配置:

sudo systemctl restart nginx

现在,你的Laravel项目应该可以通过HTTPS访问了。请确保防火墙允许HTTP(端口80)和HTTPS(端口443)流量。如果你使用的是firewalld,可以运行以下命令来允许这些端口:

sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload

至此,你已经在CentOS上成功部署了Laravel项目的HTTPS。

0