在 CentOS 上部署 C++ 服务,除了“能编译能跑”之外,还有很多工程化、稳定性和运维层面的坑。下面按重要程度 + 实战经验给你梳理一份清单。
CentOS(尤其是 7)自带的:
风险
version GLIBCXX_3.4.21 not found建议
-static,不推荐全部)yum install centos-release-scl
yum install devtoolset-11
scl enable devtoolset-11 bash
-O2 -Wall -Wextra -g
-g:方便 coredump 分析-O0-fno-omit-frame-pointer(定位栈更准)make install 到系统/opt/yourapp/libLD_LIBRARY_PATH=/opt/yourapp/lib ./server
CentOS 7+ 强烈建议 systemd
[Unit]
Description=My C++ Service
After=network.target
[Service]
ExecStart=/opt/yourapp/bin/server
WorkingDirectory=/opt/yourapp
Restart=always
User=appuser
[Install]
WantedBy=multi-user.target
systemctl enable yourapp
systemctl start yourapp
✅ 自动重启
✅ 日志收集
✅ 资源限制
useradd -r -s /sbin/nologin appuser
至少处理:
SIGINTSIGTERM(systemd 停止)✅ 优雅退出(关闭连接、flush 日志)
ulimit -c unlimited
/etc/security/limits.conf
* soft core unlimited
* hard core unlimited
分析:
gdb ./server core
bt
避免:
ulimit -n 65535
systemd:
LimitNOFILE=65535
cout 狂打/opt/yourapp/log/*.log {
daily
rotate 7
compress
}
/healthfirewall-cmd --add-port=8080/tcp --permanent
firewall-cmd --reload
getenforce
临时:
setenforce 0
正式环境建议写 policy,而不是关掉。
/opt/yourapp/
├── bin/
├── lib/
├── conf/
├── log/
└── run/
FROM centos:7
❌ 在 Ubuntu 编译,CentOS 跑
❌ root 跑服务
❌ 不处理 SIGTERM
❌ core 没开
❌ fd 用光
❌ 日志写满磁盘
如果你愿意,可以告诉我:
我可以直接给你 一套可落地的部署模板。