在Ubuntu上配置Nginx以使用SSL证书涉及几个步骤。以下是一个基本的指南,帮助你完成这个过程:
如果你还没有安装Nginx,可以使用以下命令进行安装:
sudo apt update
sudo apt install nginx
你可以从Let’s Encrypt免费获取SSL证书。使用Certbot工具可以简化这个过程。
sudo apt install certbot python3-certbot-nginx
运行Certbot来获取并安装SSL证书:
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
Certbot会自动配置Nginx并重启服务。它会提示你输入电子邮件地址,并同意Let’s Encrypt的服务条款。
Certbot会自动验证你的域名所有权,并配置Nginx以使用SSL证书。你可以通过访问https://yourdomain.com来验证配置是否成功。
Let’s Encrypt证书通常有效期为90天。Certbot会自动设置一个cron任务来定期检查并续期证书。
你可以手动测试续期过程:
sudo certbot renew --dry-run
如果没有错误,证书将会被续期。
如果你不想使用Certbot,也可以手动配置Nginx。以下是一个基本的SSL配置示例:
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;
root /var/www/html;
index index.html index.htm;
location ~ /\.ht {
deny all;
}
}
无论你是通过Certbot还是手动配置,最后都需要重启Nginx以应用更改:
sudo systemctl restart nginx
通过以上步骤,你应该能够在Ubuntu上成功配置Nginx以使用SSL证书。