ubuntu

thinkphp在ubuntu中如何部署SSL证书

小樊
41
2025-08-19 19:10:12
栏目: 云计算

在Ubuntu系统中部署ThinkPHP应用的SSL证书,通常需要以下几个步骤:

  1. 获取SSL证书

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

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

    • 假设你的ThinkPHP应用运行在Nginx上,并且你的域名是example.com
    • 编辑Nginx配置文件,通常位于/etc/nginx/sites-available/example.com
    server {
        listen 80;
        server_name example.com www.example.com;
    
        location / {
            return 301 https://$host$request_uri;
        }
    }
    
    server {
        listen 443 ssl;
        server_name example.com www.example.com;
    
        ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
        include /etc/letsencrypt/options-ssl-nginx.conf;
        ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
    
        root /var/www/html/example.com;
        index index.php index.html index.htm;
    
        location / {
            try_files $uri $uri/ /index.php?$query_string;
        }
    
        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 ~ /\.ht {
            deny all;
        }
    }
    
  4. 启用SSL证书

    • 使用Certbot来启用SSL证书。
    sudo certbot --nginx -d example.com -d www.example.com
    
  5. 重启Nginx

    sudo systemctl restart nginx
    
  6. 配置ThinkPHP应用

    • 确保你的ThinkPHP应用配置文件中没有硬编码的HTTP协议,而是使用相对路径或HTTPS协议。
    • 例如,在config/app.php中,确保URL生成使用HTTPS:
      'url_domain_deploy' => true,
      'url_http_port' => '443',
      'url_https_port' => '443',
      
  7. 测试SSL证书

    • 打开浏览器,访问https://example.com,确保SSL证书正确安装并且页面可以正常访问。

通过以上步骤,你应该能够在Ubuntu系统中成功部署ThinkPHP应用的SSL证书。如果有任何问题,请检查Nginx配置文件和SSL证书路径是否正确。

0
看了该问题的人还看了