在Ubuntu上安装Nginx并配置SSL证书可以通过以下步骤完成。以下是一个基本的指南:
首先,确保你的系统包是最新的:
sudo apt update
sudo apt upgrade -y
使用以下命令安装Nginx:
sudo apt install nginx -y
启动Nginx服务并设置开机自启:
sudo systemctl start nginx
sudo systemctl enable nginx
你可以从Let’s Encrypt免费获取SSL证书。首先,安装Certbot和Nginx插件:
sudo apt install certbot python3-certbot-nginx -y
运行Certbot来获取并安装SSL证书:
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
将yourdomain.com
替换为你的实际域名。
Certbot会自动配置Nginx以使用SSL证书,并创建一个符号链接以便于管理证书文件。
打开浏览器并访问https://yourdomain.com
,你应该能看到一个安全的连接(锁形图标)。
Certbot会自动设置一个cron任务或systemd定时器来定期续期证书。你可以通过以下命令手动测试续期:
sudo certbot renew --dry-run
如果一切正常,Certbot会输出续期成功的信息。
如果你使用的是UFW(Uncomplicated Firewall),可以允许HTTP和HTTPS流量:
sudo ufw allow 'Nginx Full'
如果你手动修改了Nginx配置文件,需要重启Nginx以应用更改:
sudo systemctl restart nginx
通过以上步骤,你应该能够在Ubuntu上成功安装并配置Nginx SSL。