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密码:
SSH安全配置:
1 2 3 4 5 6 7 8
| vim /etc/ssh/sshd_config
PermitRootLogin no 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
npm install -g cnpm --registry=https://registry.npm.taobao.org
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
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
| location / { proxy_pass http://123.123.123.123; }
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
|
Jenkins权限配置
解决SSH权限问题:
在 /etc/sudoers 中添加:
1
| jenkins ALL=(ALL) NOPASSWD:ALL
|
解决 sudo: no tty present and no askpass program specified 错误。
PM2进程管理
安装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 |
删除所有进程 |
开机自启配置
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监控
监控端口要求:
- Push metrics: 80
- Reverse interaction: 43554
数据盘挂载
阿里云数据盘挂载
查看数据盘:
创建分区:
交互命令:
p - 打印分区表
n - 创建新分区
p - 主分区
- 回车 - 使用默认分区号
- 回车 - 使用默认起始扇区
- 回车 - 使用默认结束扇区
w - 写入分区表
创建文件系统:
1 2 3 4 5
| mkfs -t ext4 /dev/vdb1
mkfs -t xfs /dev/vdb1
|
配置自动挂载:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| cp /etc/fstab /etc/fstab.bak
mkdir /data
echo `blkid /dev/vdb1 | awk '{print $2}' | sed 's/"//g'` /data ext4 defaults 0 0 >> /etc/fstab
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
tail -1000 /var/log/mongodb/mongod.log
tail -n +1000 /var/log/mongodb/mongod.log
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
| head -n 1000 /var/log/app.log
head -50 /var/log/app.log
|
故障排查
常见问题解决
ifconfig命令不存在:
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环境的初始化配置、服务安装、安全加固和自动化部署几个方面。每个部分都是实际踩过坑的经验总结。
有问题欢迎交流。