CentOS服务器部署与运维实战指南

发布时间:2026/7/16 10:01:27
CentOS服务器部署与运维实战指南 1. 为什么Web开发者需要CentOS小书第一次在服务器上部署Web应用时我对着黑底白字的终端窗口手足无措。那时才明白再华丽的代码最终都要落地到服务器环境。CentOS作为企业级Linux发行版以其稳定性和长周期支持成为Web服务部署的首选平台。但官方文档往往过于庞杂而网络教程又碎片化严重——这正是我们需要一本手边小书的根本原因。这本虚拟的小册子要解决三个核心痛点首先是环境配置的标准化问题同一个Nginx配置在开发机和生产环境表现可能天差地别其次是故障排查的效率问题当502错误突然出现时没时间从头查阅几百页的手册最后是安全防护的完备性从防火墙规则到SSL证书更新每个环节都关乎线上服务的生死。2. CentOS环境精要配置2.1 最小化安装后的必做清单刚装完CentOS 7.9 minimal系统时连ifconfig命令都不存在。建议按这个顺序完善基础环境# 安装基础工具集 yum install -y net-tools vim wget curl epel-release # 关闭SELinux生产环境需谨慎 setenforce 0 sed -i s/SELINUXenforcing/SELINUXdisabled/ /etc/selinux/config # 配置阿里云镜像源 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo重要提示EPEL源安装后建议检查优先级避免与基础源冲突。我曾遇到因优先级错乱导致PHP版本异常的问题。2.2 Web服务环境搭建LAMP和LNMP是两种经典组合个人更推荐后者# 安装NginxMySQLPHPLEMP yum install -y nginx mariadb-server php-fpm php-mysqlnd # 配置Nginx与PHP-FPM联动 sed -i s/user apache/user nginx/ /etc/php-fpm.d/www.conf sed -i s/group apache/group nginx/ /etc/php-fpm.d/www.conf systemctl enable --now nginx php-fpm mariadb防火墙需要放行必要端口firewall-cmd --permanent --add-servicehttp firewall-cmd --permanent --add-servicehttps firewall-cmd --reload3. 高频问题解决方案库3.1 Yum仓库故障处理当遇到为仓库appstream下载元数据失败错误时可尝试清理缓存yum clean all rm -rf /var/cache/yum更换为国内源前文已述对于CentOS 8 Stream特有的问题建议dnf --disablerepoappstream --enablerepobaseos install -y centos-release-stream dnf distro-sync3.2 磁盘空间告急处理通过LVM扩容的完整流程# 查看当前空间 df -h vgs # 扩展逻辑卷假设VG有剩余空间 lvextend -L 10G /dev/mapper/centos-root xfs_growfs /dev/mapper/centos-root # 对xfs文件系统3.3 Web服务典型故障502错误的排查路线图检查PHP-FPM进程状态systemctl status php-fpm查看socket权限ls -l /var/run/php-fpm/测试PHP解析创建info.php含?php phpinfo(); ?检查Nginx错误日志tail -50 /var/log/nginx/error.log4. 安全加固 checklist4.1 基础防护层SSH加固sed -i s/#Port 22/Port 2222/ /etc/ssh/sshd_config sed -i s/PermitRootLogin yes/PermitRootLogin no/ /etc/ssh/sshd_config systemctl restart sshd安装配置CSF防火墙yum install -y perl-libwww-perl perl-LWP-Protocol-https wget https://download.configserver.com/csf.tgz tar xzf csf.tgz cd csf sh install.sh4.2 Web应用防护自动续签Lets Encrypt证书yum install -y certbot python2-certbot-nginx certbot --nginx -d example.com --email adminexample.com --agree-tos --no-eff-email echo 0 3 * * * /usr/bin/certbot renew --quiet /etc/crontabNginx防CC攻击配置示例limit_req_zone $binary_remote_addr zoneone:10m rate10r/s; server { location / { limit_req zoneone burst20; } }5. 开发辅助工具集5.1 调试工具三件套实时日志追踪tail -f /var/log/nginx/access.log | grep -v ELB-HealthCheckerTCP连接分析ss -antp | grep :80性能快照top -c -o %CPU -n 1 -b cpu_usage.log5.2 数据库管理技巧MySQL紧急救援命令-- 查找锁表进程 SHOW PROCESSLIST; -- 导出不含数据的结构 mysqldump -d -u root -p dbname schema.sql -- 大表优化 ALTER TABLE large_table ENGINEInnoDB;6. 自动化运维实践6.1 Shell脚本模板部署用的标准化脚本#!/bin/bash set -e BACKUP_DIR/backups/$(date %Y%m%d) mkdir -p $BACKUP_DIR # 数据库备份 mysqldump -u root -p$DB_PASS --all-databases | gzip $BACKUP_DIR/alldb.sql.gz # 网站文件同步 rsync -avz --delete /var/www/ userbackup-server:/remote/backup/6.2 Ansible快速入门批量管理服务器的playbook示例--- - hosts: webservers become: yes tasks: - name: Ensure Nginx is installed yum: namenginx statepresent - name: Copy customized config template: src: templates/nginx.conf.j2 dest: /etc/nginx/nginx.conf notify: restart nginx handlers: - name: restart nginx systemd: namenginx staterestarted7. 虚拟化环境特别指南7.1 VMware增强工具安装VMware Tools的正确姿势yum install -y open-vm-tools perl systemctl enable --now vmtoolsd7.2 Docker化部署将传统应用容器化的示例FROM centos:7 RUN yum install -y epel-release \ yum install -y nginx php-fpm \ yum clean all COPY nginx.conf /etc/nginx/conf.d/default.conf COPY www/ /usr/share/nginx/html/ EXPOSE 80 CMD [nginx, -g, daemon off;]8. 性能调优备忘录8.1 内核参数优化/etc/sysctl.conf关键配置net.ipv4.tcp_max_syn_backlog 8192 net.core.somaxconn 65535 vm.swappiness 10 fs.file-max 655358.2 PHP性能三板斧opcache配置示例opcache.enable1 opcache.memory_consumption128 opcache.interned_strings_buffer8 opcache.max_accelerated_files4000 opcache.revalidate_freq609. 监控与报警体系9.1 简易监控方案使用netdata实现实时监控bash (curl -Ss https://my-netdata.io/kickstart.sh)9.2 日志分析管道ELK栈快速搭建# Elasticsearch yum install -y java-11-openjdk rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch yum install -y https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.9.3-x86_64.rpm10. 升级与迁移策略10.1 跨版本升级从CentOS 7到8的过渡方案yum install -y centos-release-stream dnf --releasever8 --allowerasing --setoptdeltarpmfalse distro-sync10.2 服务迁移检查清单配置文件备份/etc/nginx,/etc/php-fpm.d数据库导出验证证书文件迁移定时任务记录防火墙规则导出每次登录服务器时我都会先运行neofetch查看系统概况。这个小习惯帮助我多次提前发现异常情况比如异常的负载平均值或错误的磁盘挂载。记住好的运维不是等故障发生才行动而是通过日常观察将问题扼杀在萌芽状态。