备份镜像
1 | mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak |
下载镜像
1 | sudo curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo |
刷新缓存
1 | yum clean all # 清除旧缓存 |
1 | mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak |
1 | sudo curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo |
1 | yum clean all # 清除旧缓存 |
在 CentOS 7 上使用 certbot
自动为 Nginx 续期 SSL 证书的步骤如下:
Certbot 需要通过 EPEL 仓库安装,首先确保 EPEL 仓库已启用:
1 | sudo yum install epel-release |
安装 certbot
和 certbot-nginx
插件:
1 | sudo yum install certbot python3-certbot-nginx |
如果尚未获取 SSL 证书,可以使用以下命令为 Nginx 配置证书:
1 | sudo certbot --nginx |
按照提示输入域名和邮箱地址,Certbot 会自动配置 Nginx 并获取证书。
Certbot 默认会自动配置续期任务。你可以通过以下命令测试续期是否正常工作:
1 | sudo certbot renew --dry-run |
如果测试成功,说明自动续期配置正确。
如果需要立即续期证书,可以运行以下命令:
1 | sudo certbot renew --force-renewal |
Certbot 会自动创建一个定时任务来续期证书。你可以通过以下命令查看定时任务:
1 | sudo systemctl list-timers |
或者查看 Certbot 的日志文件:
1 | sudo cat /var/log/letsencrypt/letsencrypt.log |
Certbot 通常会自动重启 Nginx 以加载新证书。如果需要手动重启 Nginx,可以运行:
1 | sudo systemctl restart nginx |
确保防火墙允许 HTTPS 流量(端口 443):
1 | sudo firewall-cmd --permanent --add-service=https |
在 CentOS 7 上,Certbot 会自动配置 SSL 证书的续期任务。你只需确保 Certbot 和 Nginx 插件已安装,并定期检查续期任务是否正常运行即可。
在 CentOS 7 上,python3-certbot-nginx
包可能不可用,因为 CentOS 7 默认使用 Python 2.x,而不是 Python 3.x。不过,你可以通过以下方法解决这个问题,并成功安装 Certbot 和 Nginx 插件。
certbot-nginx
(Python 2.x 版本)安装 Certbot 和 Nginx 插件(Python 2.x 版本)
运行以下命令安装 Certbot 和 Nginx 插件:
1 | sudo yum install certbot python2-certbot-nginx |
如果 python2-certbot-nginx
也不可用,可以直接安装 certbot
,然后手动配置 Nginx:
1 | sudo yum install certbot |
获取 SSL 证书
使用以下命令为 Nginx 配置 SSL 证书:
1 | sudo certbot --nginx |
如果 certbot --nginx
不可用(因为缺少 python3-certbot-nginx
),可以使用以下命令手动获取证书:
1 | sudo certbot certonly --webroot -w /usr/share/nginx/html -d example.com |
其中:
-w /usr/share/nginx/html
是 Nginx 的默认根目录,确保替换为你的网站根目录。-d example.com
是你的域名。手动配置 Nginx
如果使用 certbot certonly
获取证书,需要手动配置 Nginx 以使用 SSL 证书。编辑 Nginx 配置文件(通常位于 /etc/nginx/nginx.conf
或 /etc/nginx/conf.d/your-site.conf
),添加以下内容:
1 | server { |
然后重启 Nginx:
1 | sudo systemctl restart nginx |
测试自动续期
运行以下命令测试自动续期是否正常工作:
1 | sudo certbot renew --dry-run |
设置自动续期
Certbot 会自动创建一个定时任务来续期证书。你可以通过以下命令查看定时任务:
1 | sudo systemctl list-timers |
或者手动检查 Certbot 的续期脚本:
1 | sudo cat /etc/cron.d/certbot |
在 CentOS 7 上,如果 python3-certbot-nginx
不可用,可以安装 python2-certbot-nginx
或直接使用 certbot certonly
手动获取证书并配置 Nginx。Certbot 的自动续期功能仍然可以正常工作。
要在CentOS 7上创建具有root权限的新用户,您可以按照以下步骤操作:
adduser
命令创建新用户。将<username>
替换为所需的用户名。1 | adduser <username> |
1 | passwd <username> |
wheel
组以赋予他们sudo(root)权限。1 | usermod -aG wheel <username> |
wheel
组。1 | groups <username> |
wheel
组的成员使用sudo执行命令。1 | visudo |
通过删除以下行开始的#
取消注释:
1 | %wheel ALL=(ALL) ALL |
保存并退出sudoers文件。
现在新用户应该在CentOS 7上具有root权限。您可以使用su
命令切换到新用户:
1 | su - <username> |
请确保仔细遵循这些步骤,并将<username>
替换为您要创建的实际用户名。如果需要进一步帮助,请告诉我。
要在CentOS 7上禁用root用户的远程登录,您可以按照以下步骤操作:
以已有具有sudo权限的用户登录到CentOS 7服务器。
编辑SSH配置文件/etc/ssh/sshd_config
。您可以使用文本编辑器(如vi
或nano
)打开文件进行编辑。
寻找以下行:
1 | #PermitRootLogin yes |
1 | PermitRootLogin no |
保存修改并关闭文件。
重新加载SSH服务以使更改生效。您可以使用以下命令重新启动SSH服务:
1 | sudo systemctl restart sshd |
现在,root用户的远程登录已被禁用。如果您需要通过SSH访问服务器,请使用其他有sudo权限的用户登录,然后可以使用su - root
切换到root用户执行需要特权的操作。
请牢记,禁用root远程登录有助于增加服务器的安全性,因为攻击者必须先破解普通用户的密码才能尝试登录为root。如果您有任何疑问或需要进一步协助,请告诉我。
要在Centos 7上使用yum安装nginx,您可以按照以下步骤操作:
1 | sudo yum update |
1 | sudo yum install nginx |
1 | sudo systemctl start nginx |
1 | sudo systemctl status nginx |
1 | sudo firewall-cmd --permanent --add-service=http |
现在,您已经成功在Centos 7上使用yum安装了nginx。您可以通过访问服务器的IP地址或域名来验证nginx是否已成功安装并正常运行。