溫馨提示×

thinkphp在centos上如何部署HTTPS

小樊
45
2025-08-30 17:19:49
欄目: 編程語言

在CentOS上部署ThinkPHP應用的HTTPS,你需要完成以下幾個步驟:

  1. 獲取SSL證書: 你可以從Let’s Encrypt免費獲取SSL證書,或者購買一個商業證書。使用Certbot是獲取Let’s Encrypt證書的一個流行工具。

  2. 安裝Certbot: 在CentOS上安裝Certbot,可以使用以下命令:

    sudo yum install epel-release
    sudo yum install certbot python2-certbot-nginx
    
  3. 配置Nginx: 假設你已經有一個運行中的Nginx服務器,你需要為你的ThinkPHP應用配置一個SSL站點。編輯Nginx配置文件(通常位于/etc/nginx/conf.d//etc/nginx/sites-available/目錄下),添加以下內容:

    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 HIGH:!aNULL:!MD5;
    
        root /path/to/your/thinkphp/app;
        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; # 或者使用127.0.0.1:9000
            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/thinkphp/app為你的ThinkPHP應用的根目錄。

  4. 啟用SSL站點: 如果你使用了sites-available目錄來存放配置文件,你需要創建一個到sites-enabled的符號鏈接:

    sudo ln -s /etc/nginx/sites-available/yourdomain.com.conf /etc/nginx/sites-enabled/
    
  5. 測試Nginx配置: 在重新加載Nginx之前,運行以下命令來測試配置文件是否有語法錯誤:

    sudo nginx -t
    
  6. 重新加載Nginx: 如果配置測試成功,重新加載Nginx以應用更改:

    sudo systemctl reload nginx
    
  7. 自動續期證書: Certbot會創建一個cron任務或systemd定時器來自動續期你的SSL證書。你可以使用以下命令來測試自動續期過程:

    sudo certbot renew --dry-run
    

確保你的防火墻允許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

完成以上步驟后,你的ThinkPHP應用應該可以通過HTTPS訪問了。記得定期檢查證書的有效期,并確保自動續期過程正常工作。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女