VPS搭建过程中遇到的问题及具体解决方法

买了人生第一台VPS,满怀期待地开始折腾,结果一路踩坑。这篇文章记录了我在搭建过程中遇到的真实问题和解决方法,希望能帮到同样踩坑的你。


一、连接问题

1. SSH 连接超时 / 无法连接

现象: ssh root@your_ip 一直卡着,或者报 Connection timed out

原因排查:

  • 服务商防火墙/安全组没有开放 22 端口
  • VPS 系统未正常启动
  • 本地网络被限制

解决方法:

1
2
3
4
5
# 检查本地能否 ping 通
ping your_ip

# 用 telnet 测试端口是否开放
telnet your_ip 22

登录 VPS 控制面板,检查”安全组/防火墙规则”,确保入站规则允许 TCP 22 端口。如果是国内机器,还需确认没有被运营商封端口。


2. SSH 密钥认证失败

现象: 提示 Permission denied (publickey)

原因: 公钥没有正确写入服务器,或权限设置不对。

解决方法:

1
2
3
4
5
6
# 在服务器上检查权限(必须严格)
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys

# 将本地公钥追加到服务器
cat ~/.ssh/id_rsa.pub | ssh root@your_ip "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"

同时确认 /etc/ssh/sshd_config 中以下配置已开启:

1
2
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys

修改后重启 SSH:systemctl restart sshd


3. SSH 频繁断开 / 超时断连

现象: 几分钟不操作就掉线。

解决方法(客户端配置):

编辑 ~/.ssh/config,添加:

1
2
3
Host *
ServerAliveInterval 60
ServerAliveCountMax 3

或在服务端 /etc/ssh/sshd_config 中设置:

1
2
ClientAliveInterval 60
ClientAliveCountMax 3

二、系统与环境配置

4. apt/yum 更新极慢,下载包超时

现象: apt update 卡死或速度 1KB/s。

解决方法(以 Ubuntu/Debian 为例,换国内镜像源):

1
2
3
4
5
6
7
8
9
10
11
# 备份原始源
cp /etc/apt/sources.list /etc/apt/sources.list.bak

# 替换为阿里云镜像(Ubuntu 22.04 示例)
cat > /etc/apt/sources.list << 'EOF'
deb http://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse
EOF

apt update

5. 时区错误,时间对不上

现象: 服务器时间比本地早/晚几小时,日志时间戳混乱。

解决方法:

1
2
3
4
5
6
7
8
# 查看当前时区
timedatectl

# 设置为中国时区
timedatectl set-timezone Asia/Shanghai

# 验证
date

6. 磁盘空间莫名被占满

现象: 明明没装多少东西,df -h 显示磁盘快满了。

排查方法:

1
2
3
4
5
6
7
8
# 找出占用最大的目录
du -sh /* 2>/dev/null | sort -rh | head -20

# 常见罪魁祸首:系统日志
du -sh /var/log/*

# 清理旧日志
journalctl --vacuum-size=100M

三、Web 服务相关

7. Nginx 启动报错:端口被占用

现象: nginx -t 通过,但 systemctl start nginx 失败,报 Address already in use

解决方法:

1
2
3
4
5
6
7
8
9
10
# 查看谁在占用 80/443
lsof -i :80
lsof -i :443

# 找到 PID 后 kill 掉
kill -9 <PID>

# 或直接
fuser -k 80/tcp
systemctl start nginx

8. 网站可以访问但 HTTPS 证书申请失败

现象: 用 Certbot 申请 Let’s Encrypt 证书时报错 Connection refusedTimeout

常见原因与解决方法:

1
2
3
4
5
6
7
8
9
10
# 确保 80 端口对外开放(Let's Encrypt 验证需要)
curl -I http://your_domain.com

# 申请时指定 webroot 方式(更稳定)
certbot certonly --webroot -w /var/www/html -d your_domain.com

# 如果是 standalone 模式,先停掉 nginx
systemctl stop nginx
certbot certonly --standalone -d your_domain.com
systemctl start nginx

检查域名 DNS 是否已正确解析到服务器 IP:

1
nslookup your_domain.com

9. 反向代理后获取不到真实 IP

现象: 后端应用拿到的 IP 全是 127.0.0.1

Nginx 配置修复:

1
2
3
4
5
6
7
location / {
proxy_pass http://127.0.0.1:3000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}

四、安全相关

10. 服务器被暴力破解 SSH,日志里全是失败登录

现象: lastbcat /var/log/auth.log | grep Failed 看到大量陌生 IP 的登录尝试。

加固方案:

① 禁止密码登录,只允许密钥

1
2
3
# /etc/ssh/sshd_config
PasswordAuthentication no
PermitRootLogin prohibit-password

② 修改默认 SSH 端口

1
2
# /etc/ssh/sshd_config
Port 2222 # 改成非常用端口

③ 安装 Fail2Ban 自动封 IP

1
2
apt install fail2ban -y
systemctl enable fail2ban --now

11. iptables 规则重启后丢失

现象: 手动配置的防火墙规则,重启后全没了。

解决方法:

1
2
3
4
5
6
7
8
9
# 安装持久化工具
apt install iptables-persistent -y

# 保存当前规则
netfilter-persistent save

# 或者手动保存
iptables-save > /etc/iptables/rules.v4
ip6tables-save > /etc/iptables/rules.v6

五、Docker 相关

12. Docker 拉取镜像失败 / 极慢

现象: docker pull 卡死,或者报 Error response from daemon: Get ... timeout

解决方法(配置国内镜像加速):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 编辑 Docker 配置
mkdir -p /etc/docker
cat > /etc/docker/daemon.json << 'EOF'
{
"registry-mirrors": [
"https://docker.mirrors.ustc.edu.cn",
"https://hub-mirror.c.163.com",
"https://mirror.baidubce.com"
]
}
EOF

systemctl daemon-reload
systemctl restart docker

13. Docker 容器启动后立即退出

现象: docker ps 看不到容器,docker ps -a 显示状态为 Exited

排查方法:

1
2
3
4
5
6
7
8
# 查看容器日志
docker logs <container_id>

# 以交互模式启动排查
docker run -it --entrypoint /bin/sh your_image

# 检查容器退出码
docker inspect <container_id> | grep ExitCode

常见原因:入口命令路径错误、环境变量缺失、端口冲突。


总结

VPS 折腾之路永无止境,踩坑才是成长。遇到问题不要慌,按这个思路排查:

  1. 看日志journalctl -xe/var/log/ 目录是好朋友
  2. 测连通性pingcurltelnet 先确认网络
  3. 看端口ss -tlnpnetstat -tlnp
  4. 看权限 — Linux 很多问题都是权限导致的
  5. 重启服务 — 改完配置别忘了 systemctl restart

有什么没覆盖到的问题,欢迎留言交流!


本文持续更新,遇到新坑会第一时间补充。