一. 安装 Nginx
首先,安装 Nginx 服务器。可以通过以下命令进行安装(以 Ubuntu 为例):
apt update
apt install nginx -y安装完成后,启动 Nginx 服务:
systemctl start nginx并设置 Nginx 开机启动:
systemctl enable nginx二. 检查 Nginx 是否正常工作
访问你的服务器 IP 地址(例如 http://your-server-ip),如果看到Nginx 的欢迎页面,说明安装成功。
三. 绑定域名
假设你的域名是 example.com,首先确保域名已经指向你的 VPS IP 地址。在域名服务商的控制面板中,将 A 记录指向你的 VPS IP 地址。
然后在 Nginx 配置中绑定域名。编辑 Nginx 配置文件:
nano /etc/nginx/sites-available/default找到以下部分:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.html index.htm;
server_name _; # 这里修改为你的域名
location / {
try_files $uri $uri/ =404;
}
}或者清空,直接替换你的地址
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
# 绑定你的域名
server_name 88888.xyz www.88888.xyz;
location / {
try_files $uri $uri/ =404;
}修改 server_name 为你的域名:
server_name example.com www.example.com;保存并退出编辑器(按 Ctrl+X,然后按 Y 确认保存,最后按 Enter)。
重新加载 Nginx 配置:
systemctl reload nginx四. 使用 Certbot 和 Let’s Encrypt 为你的域名生成免费配置 SSL 证书。
首先安装 Certbot 和 Nginx 插件:
apt install certbot python3-certbot-nginx -y运行 Certbot 来获取和安装 SSL 证书:
certbot --nginx -d example.com -d www.example.com- (Enter 'c' to cancel): admin@gmail.com
- (Y)es/(N)o: Y
- (Y)es/(N)o: N
五. 自动续期 SSL 证书
Let’s Encrypt 证书有效期为 90 天,Certbot 提供了自动续期功能。你可以通过以下命令测试自动续期:
certbot renew --dry-run如果一切正常,你可以通过 cron 自动进行证书续期。Certbot 默认会将这个任务设置为定期自动运行。