在 Ubuntu + ThinkPHP 环境下配置 SSL,通常有两种常见做法:
下面以 Ubuntu + Nginx + ThinkPHP(5.x / 6.x / 8.x 通用) 为例说明。
sudo apt update
sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
证书位置(自动生成):
/etc/letsencrypt/live/yourdomain.com/fullchain.pem
/etc/letsencrypt/live/yourdomain.com/privkey.pem
server {
listen 80;
server_name yourdomain.com www.yourdomain.com;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl http2;
server_name yourdomain.com www.yourdomain.com;
root /var/www/html/thinkphp/public;
index index.php index.html;
ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
⚠️ PHP 版本按你服务器实际修改(如 php8.2-fpm)
编辑:
config/app.php
'https' => true,
或中间件方式(ThinkPHP 6/8):
// app/middleware.php
return [
\think\middleware\CheckHost::class,
];
'url_domain_root' => 'https://yourdomain.com',
sudo a2enmod ssl
sudo a2ensite default-ssl
SSLCertificateFile /etc/letsencrypt/live/yourdomain.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/yourdomain.com/privkey.pem
return 301__URL__ / asset()http://申请证书 → Nginx 配 443 → 301 跳转 → ThinkPHP 开 https
如果你用的是 ThinkPHP 版本 / Nginx or Apache / 是否用宝塔,我可以给你更精确配置。