溫馨提示×

thinkphp在ubuntu中如何部署SSL證書

小樊
46
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
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女