8.1 服务器环境配置实战
基于实际部署经验,详细记录生产环境配置过程和常见问题解决方案。
生产环境配置实战
1. 系统基础环境
• 操作系统:CentOS 7.9(推荐Ubuntu 20.04 LTS)
• 内核优化:ulimit调整、文件描述符、内存管理
• 安全加固:SSH密钥登录、防火墙配置、系统更新
系统优化命令
# 1. 调整系统限制
echo "* soft nofile 65536" >> /etc/security/limits.conf
echo "* hard nofile 65536" >> /etc/security/limits.conf
# 2. 优化内核参数
echo "net.core.somaxconn = 65535" >> /etc/sysctl.conf
echo "vm.swappiness = 10" >> /etc/sysctl.conf
sysctl -p
# 3. 禁用不必要服务
systemctl disable postfix
systemctl disable sendmail
# 4. 配置时区
timedatectl set-timezone Asia/Shanghai
性能提升:高并发连接数提升50%,内存使用优化
2. Go运行环境部署
• Go版本选择:Go 1.19.3(稳定版本)
• 编译优化:静态编译、交叉编译、版本管理
• 服务管理:systemd服务、日志管理、进程监控
Go服务部署脚本
#!/bin/bash
# Go应用部署脚本 - 生产环境优化
# 1. 设置环境变量
export GIN_MODE=release
export GOCACHE=/root/.cache/go-build
# 2. 编译应用
cd /root/projects/yearandyear
go mod tidy
go build -ldflags="-s -w" -o yearandyear-server ./cmd/server
# 3. 创建systemd服务
cat > /etc/systemd/system/yearandyear.service << EOF
[Unit]
Description=YearAndYear API Server
After=network.target
[Service]
Type=simple
User=root
WorkingDirectory=/root/projects/yearandyear
ExecStart=/root/projects/yearandyear/yearandyear-server
Restart=always
RestartSec=5
Environment=GIN_MODE=release
StandardOutput=journal
StandardError=journal
[Install]
WantedBy=multi-user.target
EOF
# 4. 启用并启动服务
systemctl daemon-reload
systemctl enable yearandyear
systemctl start yearandyear
# 5. 验证服务状态
systemctl status yearandyear
journalctl -u yearandyear -n 20
部署经验:systemd管理比supervisor更稳定
监控要点:服务自动重启、日志收集、进程监控
3. FFmpeg视频环境配置
• 版本兼容:FFmpeg 4.2.2(解决xfade滤镜问题)
• 编译安装:静态编译版本、依赖库、环境变量
• 性能调优:硬件加速、多线程、内存限制
FFmpeg源码编译
# FFmpeg 4.2.2 源码编译 - 解决滤镜支持问题
cd /tmp
wget https://ffmpeg.org/releases/ffmpeg-4.2.2.tar.bz2
tar -xjf ffmpeg-4.2.2.tar.bz2
cd ffmpeg-4.2.2
# 安装编译依赖
yum install -y gcc gcc-c++ make yasm nasm
yum install -y libmp3lame-devel libogg-devel libvorbis-devel libtheora-devel
yum install -y libvpx-devel libx264-devel libx265-devel
# 配置编译选项(支持更多滤镜)
./configure \
--prefix=/usr/local/ffmpeg \
--enable-gpl \
--enable-version3 \
--enable-nonfree \
--enable-hardcoded-tables \
--enable-ffplay \
--enable-x11grab \
--enable-libx264 \
--enable-libx265 \
--enable-libmp3lame \
--enable-libvorbis \
--enable-libtheora \
--enable-libvpx \
--enable-filters
# 编译安装
make -j$(nproc)
make install
# 验证安装
ffmpeg -version
ffmpeg -filters | grep xfade # 应该显示支持
编译时间:大约2小时(取决于服务器性能)
空间需求:编译过程需要5GB+临时空间
4. 数据库环境配置
• MySQL 8.0:主从复制、性能调优、备份策略
• Redis 6.2:集群配置、持久化、内存优化
• 连接池:最大连接数、超时设置、重连机制
MySQL优化配置
# /etc/my.cnf - MySQL性能优化
[mysqld]
# 基础配置
port = 3306
socket = /var/lib/mysql/mysql.sock
pid-file = /var/run/mysqld/mysqld.pid
# 内存配置
innodb_buffer_pool_size = 2G
innodb_log_file_size = 256M
innodb_log_buffer_size = 16M
key_buffer_size = 32M
# 连接配置
max_connections = 1000
max_connect_errors = 10000
wait_timeout = 28800
interactive_timeout = 28800
# InnoDB优化
innodb_flush_log_at_trx_commit = 2
innodb_file_per_table = 1
innodb_open_files = 400
innodb_io_capacity = 2000
# 日志配置
log-error = /var/log/mysql/error.log
slow_query_log = /var/log/mysql/slow.log
long_query_time = 2
优化效果:QPS提升40%,内存使用效率提升30%
8.2 容器化部署
采用Docker容器化部署,简化部署流程。
部署架构
• 业务服务容器:API服务部署
• 视频处理容器:FFmpeg服务部署
• 数据库容器:MySQL、Redis容器化
• 反向代理:Nginx负载均衡
8.3 监控告警系统
建立完整的监控告警体系,实时掌握系统状态。
监控指标
• 系统监控:CPU、内存、磁盘使用率
• 服务监控:API响应时间、错误率
• 业务监控:用户活跃度、任务完成率
• 安全监控:异常访问、安全事件