在Ubuntu LAMP环境中实现SSL加密通信,可参考以下步骤:
- 安装Certbot工具:用于获取Let’s Encrypt免费证书。
sudo apt update sudo apt install certbot python3-certbot-apache - 获取SSL证书:通过Certbot为域名申请证书(需已解析域名且服务器开放80/443端口)。
sudo certbot --apache -d example.com -d www.example.com - 配置Apache启用SSL:Certbot会自动修改配置文件(如
/etc/apache2/sites-available/000-default.conf),添加以下内容::443> ServerName example.com DocumentRoot /var/www/html SSLEngine on SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem # 可选:指定证书链文件(若有) # SSLCertificateChainFile /etc/letsencrypt/live/example.com/chain.pem # 强制HTTP跳转HTTPS Redirect permanent / https://example.com/ - 重启Apache服务:使配置生效。
sudo systemctl restart apache2 - 验证SSL配置:通过浏览器访问
https://example.com,查看锁标志;或使用命令行工具openssl s_client -connect example.com:443测试。 - 设置证书自动续期:Let’s Encrypt证书有效期90天,Certbot默认会创建定时任务自动续期,可手动测试:
sudo certbot renew --dry-run
说明:若需手动配置证书(非Let’s Encrypt),需生成自签名证书或购买商业证书,步骤类似,但需自行处理证书有效期和续期问题。