我的运维笔记

第1讲:Rocky9服务器初始化

目标:初始化阿里云ECS,创建普通study用户,关闭root远程SSH登录,提升服务器安全。

1.阿里云控制台创建Rocky9实例,服务器公网IP:114.55.208.118。

2.MobaXterm以root登录服务器,创建普通用户study:

useradd study
passwd study

作用:新建普通运维账号,日常操作尽量不使用root。

3.给study用户sudo管理员权限:

usermod -aG wheel study

4.修改ssh配置禁止root远程登录:

vi /etc/ssh/sshd_config
# 设置 PermitRootLogin no
systemctl restart sshd

5.测试:MobaXterm切换study账号登录,测试sudo命令。

遇到问题:修改完配置root依旧可以登录。
解决:修改sshd_config后必须执行重启sshd服务。

第2讲:Nginx基础部署静态网站

目标:安装Nginx,部署静态网站,理解403权限报错原理。

1.安装Nginx软件:

dnf install nginx -y

2.启动nginx并设置开机自启:

systemctl start nginx
systemctl enable nginx
systemctl status nginx

3.防火墙放行http80端口:

firewall-cmd --add-service=http --permanent
firewall-cmd --reload

4.创建网站目录,修改属主和权限:

mkdir -p /var/www/mysite
chown -R study:study /var/www/mysite
chmod 755 /var/www/mysite

5.编写Nginx站点配置文件 /etc/nginx/conf.d/mysite.conf

6.检查配置语法,平滑重载nginx:

nginx -t
systemctl reload nginx
遇到问题:浏览器访问出现403 Forbidden
原因:nginx工作进程nginx用户没有网页目录读取权限
解决:设置目录权限755,网页文件权限644,修改正确属主。

第3讲:Certbot部署HTTPS证书

目标申请Let’s Encrypt免费SSL证书,配置http自动跳转https。

1.安装certbot工具:

dnf install certbot python3-certbot-nginx -y

2.阿里云域名解析添加A记录指向服务器公网IP 114.55.208.118,本地nslookup验证解析 fengqinqin.bond。

3.Nginx配置80端口,增加/.well-known/acme-challenge验证目录。

4.演练模拟申请证书(不真正下发证书)

certbot certonly --webroot -w /var/www/mysite -d fengqinqin.bond --dry-run

5.模拟成功后去掉--dry-run正式申请证书。

6.编写443 ssl服务块,填入证书全链、私钥路径。配置301跳转,访问http全部跳转https。

7.测试证书自动续期:

certbot renew --dry-run
遇到问题1:certbot报unauthorized 404
解决:检查acme‑challenge对应的root目录和certbot -w参数保持一致。

遇到问题2:nginx -t 报cannot load certificate
解决:证书没有申请成功就填写443配置,先完成证书申请。