在Ubuntu上为PHP配置SSL证书,主要步骤如下:
安装Certbot获取证书
sudo apt update && sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
配置PHP启用SSL
/etc/php/7.4/fpm/php.ini
):extension=openssl
已取消注释,启用OpenSSL扩展。if ($_SERVER['HTTPS'] != "on") {
header("Location: https://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
exit;
}
重启服务生效
sudo systemctl restart nginx && sudo systemctl restart php7.4-fpm
验证配置
https://yourdomain.com
,查看是否显示安全锁图标。sudo certbot renew --dry-run
(测试自动续期)。说明:
/etc/apache2/sites-available/yourdomain.com.conf
,添加SSL配置并重启Apache。/etc/letsencrypt/live/yourdomain.com/
)。