debian

Debian上如何为Laravel项目配置SSL证书

小樊
52
2025-08-10 02:21:57
栏目: 云计算

在Debian系统上为Laravel项目配置SSL证书,通常需要以下几个步骤:

  1. 获取SSL证书

    • 你可以从Let’s Encrypt免费获取SSL证书。
    • 使用Certbot工具来获取和自动续订证书。
  2. 安装Certbot

    sudo apt update
    sudo apt install certbot python3-certbot-nginx
    
  3. 配置Nginx

    • 编辑Nginx配置文件,通常位于/etc/nginx/sites-available/目录下。
    • 创建一个新的配置文件或编辑现有的配置文件。

    示例配置:

    server {
        listen 80;
        server_name yourdomain.com www.yourdomain.com;
    
        location /.well-known/acme-challenge/ {
            root /var/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$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; # 根据你的PHP版本调整
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }
    
        location / {
            try_files $uri $uri/ /index.php?$query_string;
        }
    }
    
  4. 启用配置

    • 创建符号链接以启用新的Nginx配置:
      sudo ln -s /etc/nginx/sites-available/yourdomain.com /etc/nginx/sites-enabled/
      
  5. 测试Nginx配置

    sudo nginx -t
    
  6. 重新加载Nginx

    sudo systemctl reload nginx
    
  7. 获取SSL证书

    • 使用Certbot获取证书:
      sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
      
  8. 自动续订证书

    • Certbot会自动设置一个cron任务或systemd定时器来定期续订证书。
    • 你可以手动测试续订过程:
      sudo certbot renew --dry-run
      

完成以上步骤后,你的Laravel项目应该已经通过HTTPS提供服务了。确保你的防火墙允许443端口的流量,并且你的DNS设置正确指向你的服务器。

0
看了该问题的人还看了