VPS到手后第一件事
恭喜你拿到了自己的VPS!不管你是拿来建站、学Linux还是跑项目,接下来这篇教程会带你走通从登录到建站的全流程。
如果你还没有VPS,可以看看这篇搬瓦工新手教程,写得很详细。
第一步:SSH登录服务器
VPS供应商会给你三个信息:IP地址、端口、root密码。
Windows用户
下载PuTTY或者Termius,输入IP和端口连接即可。
Mac/Linux用户
直接打开终端:
1
2
3
4
5
6
7
8
9
10
11
12
| $ ssh root@你的IP地址
root@你的IP地址's password: (输入密码,不会显示)
Welcome to Ubuntu 22.04.3 LTS (GNU/Linux 5.15.0-91-generic x86_64)
System information as of Wed Jan 15 10:00:00 UTC 2026
System load: 0.08 Processes: 102
Usage of /: 8.2% of 39.25GB Users logged in: 0
Memory usage: 12% IPv4 address: 203.0.113.10
Swap usage: 0%
root@vps:~#
|
看到root@vps:~#这个提示符,说明你已经成功登录了。
第二步:系统安全配置
裸奔的VPS就像没锁门的房子,先把安全搞好。
更新系统
1
2
3
4
5
| $ apt update && apt upgrade -y
Hit:1 http://archive.ubuntu.com/ubuntu jammy InRelease
Get:2 http://archive.ubuntu.com/ubuntu jammy-updates InRelease [119 kB]
...
64 upgraded, 3 newly installed, 0 to remove and 0 not upgraded.
|
修改SSH端口
1
2
3
4
5
| $ vim /etc/ssh/sshd_config
# 找到 #Port 22,改为:
Port 2222
$ systemctl restart sshd
|
修改后重新连接要指定端口:
1
| $ ssh -p 2222 root@你的IP地址
|
创建普通用户
不要一直用root操作,很危险:
1
2
3
4
5
6
7
8
9
10
11
12
13
| $ adduser deploy
Adding user 'deploy' ...
New password:
Retype new password:
passwd: password updated successfully
# 赋予sudo权限
$ usermod -aG sudo deploy
# 测试
$ su - deploy
$ sudo whoami
root # 说明sudo权限配置成功
|
配置SSH密钥登录
这一步非常重要,参考我之前写的SSH远程连接完全指南。
配置防火墙
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| $ sudo ufw allow 2222/tcp # SSH端口
$ sudo ufw allow 80/tcp # HTTP
$ sudo ufw allow 443/tcp # HTTPS
$ sudo ufw enable
Command may disrupt existing ssh connections. Proceed with operation (y|n)? y
Firewall is active and enabled on system startup
$ sudo ufw status
Status: active
To Action From
-- ------ ----
2222/tcp ALLOW Anywhere
80/tcp ALLOW Anywhere
443/tcp ALLOW Anywhere
|
安全配置清单
| 配置项 | 操作 | 重要程度 |
|---|
| 更新系统 | apt update && apt upgrade | 必做 |
| 修改SSH端口 | 改非22端口 | 强烈建议 |
| 创建普通用户 | 不用root日常操作 | 强烈建议 |
| SSH密钥登录 | 禁用密码登录 | 强烈建议 |
| 配置防火墙 | UFW只开必要端口 | 必做 |
| 安装fail2ban | 防暴力破解 | 建议 |
第三步:安装常用软件
1
2
3
4
5
6
7
8
| # 基础工具
$ sudo apt install -y curl wget git vim htop unzip
# 确认安装成功
$ git --version
git version 2.34.1
$ vim --version | head -1
VIM - Vi IMproved 8.2
|
第四步:安装Web环境
有两条路可选:
方案A:Nginx + 手动配置
适合想学习的人,参考Nginx安装与配置入门。
1
2
3
| $ sudo apt install -y nginx
$ sudo systemctl enable nginx
$ sudo systemctl start nginx
|
方案B:Docker(推荐)
更现代的方式,环境隔离,迁移方便。
1
2
3
4
5
6
7
| # 安装Docker
$ curl -fsSL https://get.docker.com | sh
$ sudo usermod -aG docker $USER
# 验证
$ docker --version
Docker version 24.0.7, build afdd53b
|
Docker的详细教程参考Docker官方文档。
第五步:绑定域名
配置DNS
去你的域名注册商,添加A记录:
| 主机记录 | 记录类型 | 记录值 |
|---|
@ | A | 你的VPS IP |
www | A | 你的VPS IP |
DNS生效需要几分钟到48小时不等,可以用以下命令检查:
1
2
3
4
5
6
7
8
9
10
11
| $ dig example.com +short
203.0.113.10
# 或者
$ nslookup example.com
Server: 8.8.8.8
Address: 8.8.8.8#53
Non-authoritative answer:
Name: example.com
Address: 203.0.113.10
|
配置Nginx虚拟主机
1
| $ sudo vim /etc/nginx/sites-available/mysite
|
1
2
3
4
5
6
7
8
9
10
11
| server {
listen 80;
server_name example.com www.example.com;
root /var/www/mysite;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
|
1
2
3
4
| $ sudo mkdir -p /var/www/mysite
$ echo "<h1>我的网站上线了!</h1>" | sudo tee /var/www/mysite/index.html
$ sudo ln -s /etc/nginx/sites-available/mysite /etc/nginx/sites-enabled/
$ sudo nginx -t && sudo systemctl reload nginx
|
第六步:配置SSL证书
HTTPS是必须的,用Let’s Encrypt免费证书。参考Ubuntu官方Certbot安装指南:
1
2
3
4
5
6
7
8
9
10
11
| $ sudo apt install -y certbot python3-certbot-nginx
$ sudo certbot --nginx -d example.com -d www.example.com
Saving debug log to /var/log/letsencrypt/letsencrypt.log
...
Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/example.com/fullchain.pem
Key is saved at: /etc/letsencrypt/live/example.com/privkey.pem
# 设置自动续期
$ sudo certbot renew --dry-run
Congratulations, all simulated renewals succeeded
|
第七步:日常维护
设置自动更新
1
2
| $ sudo apt install -y unattended-upgrades
$ sudo dpkg-reconfigure -plow unattended-upgrades
|
设置定时备份
1
2
3
| $ crontab -e
# 每天凌晨3点备份网站
0 3 * * * tar -czf /backup/www-$(date +\%Y\%m\%d).tar.gz /var/www/
|
监控服务器状态
1
2
3
4
5
6
7
| # 实时查看系统资源
$ htop
# 查看磁盘使用
$ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 40G 3.2G 35G 9% /
|
常见问题
连不上服务器
1
2
3
4
5
6
7
8
| # 1. 检查IP是否正确
$ ping 你的IP
# 2. 检查端口是否通(换了端口的话)
$ telnet 你的IP 2222
# 3. 检查VPS是否开机
# 去VPS控制面板看状态
|
网站打不开
1
2
3
4
5
6
7
8
| # 检查Nginx是否运行
$ sudo systemctl status nginx
# 检查防火墙是否开了80端口
$ sudo ufw status
# 检查DNS是否生效
$ dig example.com
|
总结
从拿到VPS到网站上线,整个流程就是:
- SSH登录 -> 2. 安全配置 -> 3. 安装软件 -> 4. 部署网站 -> 5. 绑域名 -> 6. 配SSL -> 7. 日常维护
其中安全配置最容易被忽略,但也是最重要的。养成好习惯,以后管理多台服务器时会轻松很多。
想学更多Linux基础命令,推荐从Linux最常用的50个命令开始。