如何在ubuntu上为nginx配置ssl
在Ubuntu上为Nginx配置SSL,你需要遵循以下步骤:
- 安装Certbot: Certbot是一个自动化的工具,用于获取和部署Let’s Encrypt SSL证书。在终端中运行以下命令来安装Certbot和Nginx插件:
sudo apt update
sudo apt install certbot python3-certbot-nginx
- 获取SSL证书: 运行以下命令来获取SSL证书。Certbot会自动配置Nginx并重启服务。
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
将yourdomain.com
和www.yourdomain.com
替换为你的域名。Certbot会提示你输入电子邮件地址、同意服务条款并选择证书类型(通常选择RSA 2048位)。
- 配置Nginx:
Certbot会自动修改Nginx配置文件以启用SSL。打开Nginx配置文件,通常位于
/etc/nginx/sites-available/yourdomain.com
,确保以下内容已添加:
server {
listen 80;
server_name yourdomain.com www.yourdomain.com;
location /.well-known/acme-challenge/ {
root /var/www/certbot;
}
location / {
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;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
# 其他配置...
}
将yourdomain.com
和www.yourdomain.com
替换为你的域名。
- 重启Nginx: 保存更改并重启Nginx以应用新配置:
sudo systemctl restart nginx
- 设置自动续期: Let’s Encrypt证书有效期为90天。Certbot会自动设置一个定时任务来续期证书。你可以使用以下命令测试自动续期是否正常工作:
sudo certbot renew --dry-run
如果没有错误,你的SSL证书应该已经成功配置并启用。现在,访问你的网站时,浏览器地址栏应该显示一个绿色的锁图标,表示连接已加密。
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权请联系我们,一经查实立即删除!