Linux 新服务器环境搭建踩坑记录:CentOS 系统初始化与服务部署

新买的服务器,从零开始搭建环境是个体力活。这篇记录一下 CentOS 7 上安装 MongoDB、Node.js、Redis、Nginx 的完整流程和踩过的坑。

服务器初始化流程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
┌─────────────────────────────────────────────────────────────────────┐
│ 新服务器初始化标准流程 │
├─────────────────────────────────────────────────────────────────────┤

│ 1. 修改 root 密码
│ │
│ 2. 查看系统配置(CPU/内存/磁盘)
│ │
│ 3. 升级系统补丁
│ │
│ 4. 安装基础服务(MongoDB/Node.js/Redis/Nginx)
│ │
│ 5. 优化系统参数(文件描述符 limits)
│ │
│ 6. 配置开机自启
│ │
│ 7. 重启验证

系统基础配置

1. 修改 root 密码

服务器交付后,第一件事就是改密码:

1
passwd root

按照提示输入新密码并确认。建议使用强密码策略:

密码要求 说明
长度 至少 12 位
复杂度 大小写字母 + 数字 + 特殊符号
更新周期 生产环境建议每 90 天更换

2. 查看系统基本配置

1
2
3
4
5
6
7
8
9
10
11
# 查看 CPU 和内存使用情况
top

# 查看磁盘使用情况(人类可读格式)
df -h

# 查看内存详细信息
free -h

# 查看系统版本
cat /etc/redhat-release
命令 输出说明
top 实时 CPU、内存、进程状态
df -h 磁盘分区使用情况
free -h 物理内存和交换空间

3. 升级系统

1
2
3
4
5
# 更新所有软件包
yum update -y

# 仅更新安全补丁
yum update --security -y

注意:生产环境升级前建议创建系统快照或备份关键数据。

核心服务安装

4. 安装 MongoDB

添加 MongoDB 官方仓库

1
2
# 创建 repo 文件
vim /etc/yum.repos.d/mongodb-org-4.2.repo

文件内容:

1
2
3
4
5
6
[mongodb-org-4.2]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.2/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.2.asc

安装并启动

1
2
3
4
5
6
7
8
9
10
11
# 安装 MongoDB
yum install -y mongodb-org

# 启动服务
systemctl start mongod

# 设置开机自启
systemctl enable mongod

# 查看状态
systemctl status mongod
版本 对应 MongoDB 适用场景
4.2 稳定版 生产环境推荐
4.4 较新版 新特性需求
5.0 最新版 测试环境

5. 安装 Node.js

使用 NodeSource 仓库安装

1
2
3
4
5
6
7
8
9
# 添加 NodeSource 仓库(以 Node.js 14.x 为例)
curl --silent --location https://rpm.nodesource.com/setup_14.x | sudo bash -

# 安装 Node.js
yum -y install nodejs

# 验证安装
node -v
npm -v

配置 npm 镜像

1
2
3
4
5
# 安装 cnpm(可选)
npm install -g cnpm --registry=https://registry.npm.taobao.org

# 或直接设置 npm 淘宝镜像
npm config set registry https://registry.npm.taobao.org

安装 PM2 进程管理器

1
2
3
4
5
# 全局安装 PM2
npm install -g pm2

# 查看 PM2 版本
pm2 -v

6. 安装 Redis

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# CentOS 默认仓库安装
yum install -y redis

# 启动服务
systemctl start redis

# 设置开机自启
systemctl enable redis

# 查看状态
systemctl status redis

# 测试连接
redis-cli ping
# 预期输出:PONG

7. 安装 Nginx

添加 Nginx 官方仓库

1
2
# 创建 repo 文件
vim /etc/yum.repos.d/nginx.repo

文件内容:

1
2
3
4
5
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1

安装并启动

1
2
3
4
5
6
7
8
9
10
11
# 安装 Nginx
yum install -y nginx

# 启动服务
systemctl start nginx

# 设置开机自启
systemctl enable nginx

# 查看状态
systemctl status nginx
Nginx 版本 特性
Stable 稳定版,推荐生产使用
Mainline 开发版,包含最新功能

系统参数优化

8. 配置文件描述符限制

高并发服务需要足够的文件描述符,否则会出现 “Too many open files” 错误。

查看当前限制

1
2
3
4
5
# 查看当前用户限制
ulimit -n

# 查看系统最大限制
sysctl -n -e fs.file-max

修改 limits.conf

警告:配置的值不能超过系统支持的 fs.file-max,否则系统将无法启动!

1
2
3
4
5
# 备份原配置
cp /etc/security/limits.conf /etc/security/limits.conf.$(date +%Y-%m-%d)

# 编辑配置
vim /etc/security/limits.conf

添加以下内容:

1
2
3
4
5
6
7
8
9
# 针对所有用户
* soft nofile 65535
* hard nofile 65535

# 或针对特定用户
root soft nofile 65535
root hard nofile 65535
www soft nofile 65535
www hard nofile 65535
参数 说明
soft 当前生效的限制,可动态调整
hard 硬限制,不能超过此值
nofile 打开文件的最大数量

修改 sysctl 配置(可选)

1
2
3
4
5
6
7
8
# 编辑系统参数
vim /etc/sysctl.conf

# 添加
fs.file-max = 655360

# 立即生效
sysctl -p

9. 安装依赖库

某些 Node.js 模块需要编译原生扩展,需安装基础编译工具:

1
2
# 安装编译依赖
yum -y install pcre-devel zlib-devel openssl openssl-devel unzip gcc-c++ make
包名 作用
pcre-devel 正则表达式库(Nginx 需要)
zlib-devel 压缩库
openssl-devel SSL/TLS 加密库
gcc-c++ C++ 编译器
make 构建工具

服务开机自启配置

10. 配置系统服务自启

1
2
3
4
5
6
7
8
# Nginx 开机自启
systemctl enable nginx.service

# Redis 开机自启
systemctl enable redis.service

# MongoDB 开机自启
systemctl enable mongod.service

验证自启配置:

1
2
# 查看所有已启用自启的服务
systemctl list-unit-files | grep enabled

11. 配置 PM2 开机自启

PM2 需要额外配置才能在系统重启后自动恢复 Node.js 进程:

1
2
3
4
5
6
7
8
# 生成 PM2 开机启动脚本
pm2 startup

# 根据输出提示执行对应的命令(例如)
sudo env PATH=$PATH:/usr/bin /usr/lib/node_modules/pm2/bin/pm2 startup systemd -u root --hp /root

# 保存当前进程列表
pm2 save
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
┌─────────────────────────────────────────────────────────────────────┐
│ PM2 开机自启原理 │
├─────────────────────────────────────────────────────────────────────┤

│ 系统启动
│ │
│ ▼
│ systemd 执行 pm2-root.service
│ │
│ ▼
│ pm2 resurrect ──► 读取 ~/.pm2/dump.pm2
│ │
│ ▼
│ 自动重启所有保存的进程

│ 关键命令:
│ • pm2 startup 生成 systemd 启动脚本
│ • pm2 save 保存当前进程列表到 dump.pm2
│ • pm2 resurrect 从 dump.pm2 恢复进程(系统自动调用)

日志查看与管理

常用日志查看命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 实时查看日志(带关键字过滤)
tail -f /var/log/nginx/access.log | grep "500"

# 查看最后 1000 行并实时更新
tail -f -n 1000 /var/log/mongodb/mongod.log

# 查看文件最后 1000 行(不实时)
tail -1000 /var/log/messages

# 从第 1000 行开始显示
tail -n +1000 /var/log/messages

# 查看日志文件前 1000 行
head -n 1000 /var/log/messages

# 查看日志中间范围(第 1001-3000 行)
cat /var/log/messages | head -n 3000 | tail -n +1001
命令 作用
tail -f 实时追踪文件末尾
tail -n 1000 显示最后 1000 行
head -n 1000 显示前 1000 行
grep 过滤关键字

多关键字过滤

1
2
3
4
5
# 同时过滤多个关键字(与关系)
tail -f /var/log/app.log | grep "ERROR" | grep "timeout"

# 过滤多个关键字(或关系)
tail -f /var/log/app.log | grep -E "ERROR|WARN|FATAL"

最终验证

12. 重启并验证所有服务

1
2
3
4
5
6
7
8
9
10
11
# 重启服务器
reboot

# 重启后检查所有服务状态
systemctl status nginx
systemctl status redis
systemctl status mongod
pm2 status

# 检查端口监听
ss -tlnp | grep -E "80|443|27017|6379|3000"
服务 默认端口 检查命令
Nginx 80/443 curl -I http://localhost
MongoDB 27017 mongo --eval "db.runCommand({ping: 1})"
Redis 6379 redis-cli ping
Node.js 3000+ curl http://localhost:3000/health

安全配置建议

防火墙基础配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 查看防火墙状态
systemctl status firewalld

# 开放必要端口
firewall-cmd --permanent --add-service=ssh
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https

# 开放自定义端口(如 MongoDB,仅允许内网访问)
firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="10.0.0.0/8" port protocol="tcp" port="27017" accept'

# 重载防火墙
firewall-cmd --reload

# 查看已开放端口
firewall-cmd --list-all

SSH 安全加固

1
2
3
4
5
6
7
8
# 编辑 SSH 配置
vim /etc/ssh/sshd_config

# 建议修改项
PermitRootLogin no # 禁止 root 登录
PasswordAuthentication no # 使用密钥登录(配置密钥后)
Port 2222 # 修改默认端口(可选)
MaxAuthTries 3 # 最大尝试次数

写在最后

新服务器环境搭建的几个关键点:

  1. 安全第一:立即修改 root 密码,后续配置 SSH 密钥登录并禁用密码认证
  2. 系统更新:首次上线前执行 yum update 更新所有安全补丁
  3. 服务安装:按业务需求安装 MongoDB、Node.js、Redis、Nginx,均使用官方仓库确保版本可控
  4. 参数优化:根据并发需求调整文件描述符限制,注意不要超过系统 fs.file-max
  5. 自启配置:所有服务配置 systemd 开机自启,Node.js 应用使用 PM2 管理并执行 pm2 save
  6. 验证重启:所有配置完成后重启服务器,逐一验证服务是否正常启动
  7. 日志管理:掌握 tailheadgrep 的组合使用,快速定位问题

通过标准化的初始化流程,可以确保每台服务器都达到一致、安全、可维护的状态,为后续的业务部署打下坚实基础。