Linux服务器运维踩坑记录

Linux服务器运维踩坑记录

基于CentOS 7系统的运维实操记录,从初始化到自动化部署遇到的问题整理。

服务器初始化配置

系统基础检查

新服务器到手先检查基本信息:

1
2
3
4
5
6
cat /etc/centos-release
top -bn1 | head -20
df -h
free -h
ip addr
hostname -I

系统安全基线配置

修改root密码:

1
passwd root

SSH安全配置:

1
2
3
4
5
6
7
8
vim /etc/ssh/sshd_config

# 建议配置
PermitRootLogin no # 禁止root直接登录
PasswordAuthentication no # 使用密钥认证
MaxAuthTries 3 # 最大尝试次数
ClientAliveInterval 300 # 客户端保活间隔
ClientAliveCountMax 2 # 保活检测次数

系统升级

1
2
yum update -y
yum install -y wget curl vim net-tools telnet

核心服务安装配置

Node.js环境搭建

NodeSource仓库安装:

1
2
3
4
5
6
7
8
9
10
11
curl --silent --location https://rpm.nodesource.com/setup_14.x | sudo bash
yum -y install nodejs

# 安装cnpm加速npm
npm install -g cnpm --registry=https://registry.npm.taobao.org

# 配置淘宝npm镜像
npm config set registry https://registry.npm.taobao.org

node -v
npm -v

npm镜像源配置:

1
2
3
4
5
# 配置华为镜像
npm config set registry https://mirrors.huaweicloud.com/repository/npm/

# 查看当前镜像源
npm config get registry

MongoDB安装配置

创建仓库文件:

1
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
yum install -y mongodb-org
systemctl start mongod
systemctl enable mongod
systemctl status mongod

Redis安装配置

1
2
3
4
5
6
7
8
9
10
11
12
13
yum install -y redis

# 修改配置支持远程访问
vim /etc/redis.conf

# 注释掉bind 127.0.0.1
# bind 127.0.0.1

# 设置保护模式
protected-mode no

systemctl start redis
systemctl enable redis

Nginx安装配置

创建仓库文件:

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
4
5
6
yum install -y nginx
systemctl start nginx
systemctl enable nginx

# 配置反向代理
vim /etc/nginx/conf.d/app.conf

反向代理配置示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
server {
listen 80;
server_name api.example.com;

location / {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_cache_bypass $http_upgrade;
}
}

502错误解决方案:

当遇到502错误时,检查proxy_pass配置:

1
2
3
4
5
6
7
8
9
# 错误配置 - 使用IP可能失败
location / {
proxy_pass http://123.123.123.123; # 可能502
}

# 正确配置 - 使用域名和https
location / {
proxy_pass https://target.example.com; # 推荐
}

防火墙与安全配置

Firewalld基础操作

查看防火墙状态:

1
2
3
systemctl status firewalld
firewall-cmd --state
firewall-cmd --list-all

端口管理:

1
2
3
4
5
firewall-cmd --query-port=8080/tcp
firewall-cmd --permanent --add-port=80/tcp
firewall-cmd --permanent --add-port=3306/tcp
firewall-cmd --permanent --remove-port=8080/tcp
firewall-cmd --reload

高级规则配置

允许特定IP访问:

1
2
3
4
5
firewall-cmd --zone=public \
--add-rich-rule 'rule family="ipv4" source address="192.168.1.10" accept' \
--permanent

firewall-cmd --reload

配置文件直接编辑:

1
vim /etc/firewalld/zones/public.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?xml version="1.0" encoding="utf-8"?>
<zone>
<short>Public</short>
<description>For use in public areas.</description>
<service name="ssh"/>
<service name="dhcpv6-client"/>
<port protocol="tcp" port="80"/>
<port protocol="tcp" port="22"/>
<port protocol="tcp" port="8080"/>
<port protocol="tcp" port="443"/>
<rule family="ipv4">
<source address="120.25.xx.xx"/>
<accept/>
</rule>
<rule family="ipv4">
<source address="172.18.xx.xx"/>
<accept/>
</rule>
</zone>

服务开关机管理

1
2
3
4
5
systemctl enable nginx.service
systemctl enable redis.service
systemctl enable mongod.service

systemctl disable nginx.service

系统参数调优

文件描述符配置

查看当前配置:

1
2
3
cat /proc/sys/fs/file-max
ls /proc/$(pgrep nginx)/fd | wc -l
cat /proc/$(pgrep nginx)/limits | grep "files"

修改配置(注意风险):

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

vim /etc/security/limits.conf

# 添加以下内容
* soft nofile 65535
* hard nofile 65535
root soft nofile 65535
root hard nofile 65535

系统级配置:

1
2
3
4
5
6
7
8
9
vim /etc/sysctl.conf

# 添加以下配置
fs.file-max = 1000000
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_keepalive_time = 1200
net.core.somaxconn = 65535
net.ipv4.tcp_max_syn_backlog = 65535

高并发参数调优

1
2
3
sysctl -a | grep net.core
sysctl -a | grep fs.file
sysctl -p

Jenkins自动化部署

Jenkins安装

yum安装:

1
2
3
4
5
6
7
8
9
10
sudo wget -O /etc/yum.repos.d/jenkins.repo \
https://pkg.jenkins.io/redhat-stable/jenkins.repo

sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key

yum install -y jenkins

sudo systemctl start jenkins
sudo systemctl enable jenkins
sudo systemctl status jenkins

防火墙配置

1
2
3
4
5
6
7
8
9
10
11
YOURPORT=8080
PERM="--permanent"
SERV="$PERM --service=jenkins"

firewall-cmd $PERM --new-service=jenkins
firewall-cmd $SERV --set-short="Jenkins ports"
firewall-cmd $SERV --set-description="Jenkins port exceptions"
firewall-cmd $SERV --add-port=$YOURPORT/tcp
firewall-cmd $PERM --add-service=jenkins
firewall-cmd --zone=public --add-service=http --permanent
firewall-cmd --reload

初始配置

获取初始密码:

1
cat /var/lib/jenkins/secrets/initialAdminPassword

插件安装失败解决方案:

1
2
3
4
5
6
vim /var/lib/jenkins/updates/default.json

# 替换所有:
# https://updates.jenkins.io/download/
# 替换为:
# https://mirrors.tuna.tsinghua.edu.cn/jenkins/

Jenkins权限配置

解决SSH权限问题:

/etc/sudoers 中添加:

1
jenkins ALL=(ALL) NOPASSWD:ALL

解决 sudo: no tty present and no askpass program specified 错误。

PM2进程管理

安装PM2

1
npm install -g pm2

常用命令

命令 说明
pm2 start app.js 启动应用
pm2 start app.js -i 4 启动4个实例(cluster模式)
pm2 start app.js --name my-api 指定应用名称
pm2 list 查看所有进程
pm2 monit 监控面板
pm2 logs 查看日志
pm2 stop all 停止所有进程
pm2 restart all 重启所有进程
pm2 reload all 零停机重载
pm2 delete all 删除所有进程

开机自启配置

1
2
pm2 startup
pm2 save

PM2配置文件

ecosystem.config.js:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
module.exports = {
apps: [
{
name: 'api-server',
script: './server.js',
instances: 'max',
exec_mode: 'cluster',
env: {
NODE_ENV: 'production',
PORT: 3000
},
env_development: {
NODE_ENV: 'development',
PORT: 3000
},
log_file: './logs/combined.log',
out_file: './logs/out.log',
error_file: './logs/error.log',
log_date_format: 'YYYY-MM-DD HH:mm:ss Z',
merge_logs: true,
max_memory_restart: '1G'
}
]
};

Keymetrics监控

1
pm2 link xxxx xxxx

监控端口要求:

  • Push metrics: 80
  • Reverse interaction: 43554

数据盘挂载

阿里云数据盘挂载

查看数据盘:

1
fdisk -l

创建分区:

1
fdisk -u /dev/vdb

交互命令:

  • p - 打印分区表
  • n - 创建新分区
  • p - 主分区
  • 回车 - 使用默认分区号
  • 回车 - 使用默认起始扇区
  • 回车 - 使用默认结束扇区
  • w - 写入分区表

创建文件系统:

1
2
3
4
5
# 创建ext4文件系统
mkfs -t ext4 /dev/vdb1

# 或创建xfs文件系统(推荐MongoDB使用)
mkfs -t xfs /dev/vdb1

配置自动挂载:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 备份fstab
cp /etc/fstab /etc/fstab.bak

# 创建挂载点
mkdir /data

# 写入fstab(ext4)
echo `blkid /dev/vdb1 | awk '{print $2}' | sed 's/"//g'` /data ext4 defaults 0 0 >> /etc/fstab

# 或(xfs)
echo `blkid /dev/vdb1 | awk '{print $2}' | sed 's/"//g'` /data xfs defaults 0 0 >> /etc/fstab

# 挂载
mount /dev/vdb1 /data/

八、日志管理与分析

8.1 tail命令日志查看

1
2
3
4
5
6
7
8
9
10
11
# 实时查看日志
tail -f /var/log/nginx/access.log

# 查看最后1000行
tail -1000 /var/log/mongodb/mongod.log

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

# 查看日志文件中间内容(1001-3000行)
cat /var/log/app.log | head -n 3000 | tail -n +1001

8.2 grep过滤日志

1
2
3
4
5
6
7
8
# 实时过滤关键字
tail -f /var/log/app.log | grep "ERROR"

# 多个关键字过滤
tail -f /var/log/app.log | grep "ERROR" | grep "Database"

# 在日志文件中搜索
grep -i "error" /var/log/app.log

8.3 head命令使用

1
2
3
4
5
# 查看前1000行
head -n 1000 /var/log/app.log

# 查看前50行
head -50 /var/log/app.log

故障排查

常见问题解决

ifconfig命令不存在:

1
yum install net-tools

Tomcat乱码:

修改 tomcat/conf/logging.properties

1
java.util.logging.ConsoleHandler.encoding = GBK

GitHub访问问题:

在Windows hosts 文件中添加:

1
2
3
# GitHub Start
151.101.76.133 raw.githubusercontent.com
# GitHub End

系统安装问题

CentOS 7 U盘安装无法进入图形界面:

1
vmlinuz initrd.img inst.stage2=hd:/dev/sdb4 quiet

小结

这篇文章记录了我在Linux服务器运维中遇到的实际问题和解决方案。主要是CentOS 7环境的初始化配置、服务安装、安全加固和自动化部署几个方面。每个部分都是实际踩过坑的经验总结。


有问题欢迎交流。