新买的服务器,从零开始搭建环境是个体力活。这篇记录一下 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 密码
服务器交付后,第一件事就是改密码:
按照提示输入新密码并确认。建议使用强密码策略:
| 密码要求 |
说明 |
| 长度 |
至少 12 位 |
| 复杂度 |
大小写字母 + 数字 + 特殊符号 |
| 更新周期 |
生产环境建议每 90 天更换 |
2. 查看系统基本配置
1 2 3 4 5 6 7 8 9 10 11
| 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
| 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
| 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
| curl --silent --location https://rpm.nodesource.com/setup_14.x | sudo bash -
yum -y install nodejs
node -v npm -v
|
配置 npm 镜像
1 2 3 4 5
| npm install -g cnpm --registry=https://registry.npm.taobao.org
npm config set registry https://registry.npm.taobao.org
|
安装 PM2 进程管理器
1 2 3 4 5
| npm install -g pm2
pm2 -v
|
6. 安装 Redis
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| yum install -y redis
systemctl start redis
systemctl enable redis
systemctl status redis
redis-cli ping
|
7. 安装 Nginx
添加 Nginx 官方仓库
1 2
| 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
| 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
| systemctl enable nginx.service
systemctl enable redis.service
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 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"
tail -f -n 1000 /var/log/mongodb/mongod.log
tail -1000 /var/log/messages
tail -n +1000 /var/log/messages
head -n 1000 /var/log/messages
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
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
| vim /etc/ssh/sshd_config
PermitRootLogin no PasswordAuthentication no Port 2222 MaxAuthTries 3
|
写在最后
新服务器环境搭建的几个关键点:
- 安全第一:立即修改 root 密码,后续配置 SSH 密钥登录并禁用密码认证
- 系统更新:首次上线前执行
yum update 更新所有安全补丁
- 服务安装:按业务需求安装 MongoDB、Node.js、Redis、Nginx,均使用官方仓库确保版本可控
- 参数优化:根据并发需求调整文件描述符限制,注意不要超过系统
fs.file-max
- 自启配置:所有服务配置 systemd 开机自启,Node.js 应用使用 PM2 管理并执行
pm2 save
- 验证重启:所有配置完成后重启服务器,逐一验证服务是否正常启动
- 日志管理:掌握
tail、head、grep 的组合使用,快速定位问题
通过标准化的初始化流程,可以确保每台服务器都达到一致、安全、可维护的状态,为后续的业务部署打下坚实基础。