CentOS服务器部署踩坑记录

CentOS服务器部署踩坑记录

最近又部署了几台CentOS服务器,从系统安装到服务配置,记录一下踩过的坑和解决方案。

系统初始化

修改Root密码

1
passwd root

建议用强密码,包含大小写字母、数字和特殊字符。

查看系统信息

1
2
3
4
5
# 资源使用
top

# 磁盘空间
df -h

系统更新

1
yum update

看网络情况,可能需要等一段时间。

MongoDB安装与配置

配置YUM仓库

1
vim /etc/yum.repos.d/mongodb-org-6.0.repo

添加内容:

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

安装MongoDB

1
yum install -y mongodb-org

创建数据目录

建议把数据放在独立磁盘:

1
2
3
4
5
6
7
8
9
10
11
mkdir /data/db
mkdir /data/db/mongo
mkdir /data/log
mkdir /data/log/mongodb

# 迁移原有数据(如有)
rsync -av /var/lib/mongo /data/db/mongo/

# 设置权限
chown -R mongod:mongod /data/db/mongo/
chown -R mongod:mongod /data/log/mongodb/

SELinux处理

SELinux经常干扰MongoDB,建议关闭。

临时关闭(立即生效):

1
setenforce 0

永久禁用(需重启):

1
2
vi /etc/selinux/config
# SELINUX=enforcing 改为 SELINUX=disabled

启动服务

1
2
3
systemctl start mongod.service
systemctl enable mongod.service
systemctl status mongod.service

Node.js环境

安装EPEL

1
2
yum info epel-release
yum install epel-release

安装Node.js

1
2
3
4
5
6
yum install nodejs
npm

# 验证
node --version
npm -v

配置npm镜像

国内环境建议换镜像:

1
2
3
4
5
6
7
8
# 华为云
npm config set registry https://mirrors.huaweicloud.com/repository/npm/

# 或淘宝
npm config set registry https://registry.npmmirror.com

# 查看配置
npm config get registry

PM2进程管理

1
2
3
4
5
npm i -g pm2

# 开机自启
pm2 startup
pm2 save

注意:PM2有时候会莫名启动多个进程,生产环境要留意监控。

Redis安装

1
2
3
yum install redis
systemctl enable redis.service
systemctl start redis.service

Nginx配置

配置YUM仓库

1
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
yum install nginx
systemctl enable nginx.service
systemctl start nginx.service

解决Request Entity Too Large

上传大文件时遇到的错误,修改配置:

1
2
3
4
5
6
7
server {
listen 80;
server_name your-domain.com;
root /home/public/;

client_max_body_size 50M; # 默认1M,改成50M
}

系统参数优化

文件描述符限制

高并发应用需要调大这个值。

查看当前限制:

1
2
ulimit -n
sysctl -n -e fs.file-max

警告:修改前必须备份,不能超过系统最大值,否则可能启动不了。

备份并修改:

1
2
3
cp /etc/security/limits.conf /etc/security/limits.conf.backup

vim /etc/security/limits.conf

添加:

1
2
* soft nofile 65536
* hard nofile 65536

安装必要依赖

1
yum -y install pcre-devel zlib-devel openssl openssl-devel unzip

SSL证书配置

安装Certbot

1
2
3
4
5
6
yum install snapd
systemctl enable --now snapd.socket
ln -s /var/lib/snapd/snap /snap

snap install --classic certbot
ln -s /snap/bin/certbot /usr/bin/certbot

注意:装完需要重启服务器。

生成证书

1
certbot certonly --nginx

按提示输入域名和邮箱。

Nginx SSL配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
server {
listen 443 ssl;
server_name xxx.com;
root /home/public/;

ssl_certificate /etc/letsencrypt/live/xxx.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/xxx.com/privkey.pem;

ssl_session_timeout 1d;
ssl_session_cache shared:SSL:50m;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
}

测试自动续期

1
certbot renew --dry-run

Java环境

有些老项目需要Java:

1
2
3
4
5
# 查看可用版本
yum list java*

# 安装OpenJDK 1.8
yum install java-1.8.0-openjdk.x86_64

防火墙配置

开发环境关闭

1
2
systemctl stop firewalld.service
systemctl disable firewalld.service

生产环境配置

1
2
3
4
5
6
7
8
9
10
11
12
# 启动
sudo systemctl start firewalld
sudo systemctl enable firewalld

# 查看规则
sudo firewall-cmd --list-all

# 开放端口
sudo firewall-cmd --zone=public --add-port=8080/tcp --permanent

# 重载配置
sudo firewall-cmd --reload

服务管理速查

服务 启动 停止 自启
MongoDB systemctl start mongod systemctl stop mongod systemctl enable mongod
Nginx systemctl start nginx systemctl stop nginx systemctl enable nginx
Redis systemctl start redis systemctl stop redis systemctl enable redis

常见问题

MongoDB启动失败

错误:Attempted to create a lock file on a read-only directory

排查:

  1. 检查目录权限
  2. 确认SELinux已关闭
  3. 检查磁盘空间:df -h
  4. 确认数据目录不是只读挂载

Node.js连接MongoDB失败

错误:MongoServerSelectionError connect ECONNREFUSED 127.0.0.1:27017

原因:Node.js用IPv6,localhost指向:::1而不是127.0.0.1。

解决:

  1. 降级Node.js到16.x
  2. 或者直接用IP连接:mongodb://127.0.0.1:27017/xxx

npm安装卡住

错误:reify:fsevents sill reify mark deleted

解决:

  1. 检查镜像源
  2. 换官方源或华为镜像
  3. 清缓存:npm cache clean --force

CentOS部署整体流程比较成熟,但坑也不少。SELinux、权限、IPv6这些是老问题了。建议:

  1. 配置文件改之前先备份
  2. SELinux搞不定的直接关了(生产环境除外)
  3. Node.js和MongoDB的IPv6问题注意一下
  4. SSL证书自动续期要测试
  5. 防火墙规则记得保存

有问题的欢迎留言讨论。