Linux服务器核心组件部署指南:Nginx、Redis、PostgreSQL与Docker实践

一、环境准备与通用原则

在开始部署前,需确保服务器满足以下条件:

  1. 操作系统:Ubuntu 20.04 LTS或CentOS 8+(推荐LTS版本)
  2. 最小硬件配置:2核4G内存(生产环境建议4核8G起)
  3. 网络要求:开放80/443(Web)、6379(Redis)、5432(PostgreSQL)、2375-2376(Docker)端口
  4. 权限要求:具备sudo权限的普通用户或root账户

安全建议

  • 生产环境务必配置防火墙规则
  • 敏感服务(如Redis/PostgreSQL)建议绑定内网IP
  • 定期更新系统补丁(sudo apt update && sudo apt upgrade -y

二、Nginx部署与高可用配置

2.1 安装与版本验证

  1. # Ubuntu/Debian系
  2. sudo apt update && sudo apt install nginx -y
  3. # CentOS/RHEL系
  4. sudo yum install epel-release -y
  5. sudo yum install nginx -y

验证安装:

  1. nginx -v # 显示版本号
  2. nginx -V # 显示编译参数(含模块信息)

2.2 服务管理最佳实践

  1. # 启动服务(兼容systemd系统)
  2. sudo systemctl start nginx
  3. sudo systemctl enable nginx # 设置开机自启
  4. # 状态检查
  5. sudo systemctl status nginx
  6. journalctl -u nginx --no-pager -n 50 # 查看最近50条日志

2.3 防火墙配置

  1. # UFW防火墙(Ubuntu)
  2. sudo ufw allow 'Nginx Full' # 开放80/443
  3. sudo ufw status # 验证规则
  4. # Firewalld(CentOS)
  5. sudo firewall-cmd --permanent --add-service=http
  6. sudo firewall-cmd --permanent --add-service=https
  7. sudo firewall-cmd --reload

2.4 配置文件管理

  • 主配置文件:/etc/nginx/nginx.conf
  • 站点配置目录:/etc/nginx/conf.d/
  • 日志目录:/var/log/nginx/

配置重载技巧

  1. sudo nginx -t # 语法检查
  2. sudo systemctl reload nginx # 无中断重载
  3. sudo nginx -s reload # 等效命令

三、Redis内存数据库部署

3.1 安装与验证

  1. # Ubuntu/Debian
  2. sudo apt install redis-server -y
  3. # CentOS/RHEL
  4. sudo yum install redis -y

验证服务:

  1. redis-cli ping # 应返回PONG
  2. redis-cli info | grep redis_version # 查看版本

3.2 生产环境优化配置

修改/etc/redis/redis.conf关键参数:

  1. bind 127.0.0.1 # 生产环境建议改为内网IP
  2. protected-mode yes
  3. requirepass YourStrongPassword # 启用认证
  4. maxmemory 2gb # 根据服务器内存设置
  5. maxmemory-policy allkeys-lru # 淘汰策略

3.3 服务管理

  1. sudo systemctl restart redis
  2. sudo systemctl status redis
  3. # 日志查看
  4. tail -f /var/log/redis/redis-server.log

四、PostgreSQL数据库部署

4.1 安装与初始化

  1. # Ubuntu/Debian
  2. sudo apt install postgresql postgresql-contrib -y
  3. # CentOS/RHEL
  4. sudo yum install postgresql-server postgresql-contrib -y
  5. sudo postgresql-setup --initdb # 初始化数据库

4.2 安全配置

  1. sudo -u postgres psql # 切换postgres用户
  2. ALTER USER postgres WITH PASSWORD 'SecurePassword123!';
  3. CREATE DATABASE mydb;
  4. \q # 退出

修改/etc/postgresql/12/main/pg_hba.conf(版本号可能不同):

  1. # TYPE DATABASE USER ADDRESS METHOD
  2. host all all 192.168.1.0/24 md5 # 允许内网访问

4.3 服务管理

  1. sudo systemctl restart postgresql
  2. sudo -u postgres psql -l # 列出数据库

五、Docker容器平台部署

5.1 安装流程

  1. # 卸载旧版本(如有)
  2. sudo apt remove docker docker-engine docker.io containerd runc
  3. # 安装依赖
  4. sudo apt install apt-transport-https ca-certificates curl gnupg-agent software-properties-common -y
  5. # 添加官方GPG密钥(中立化描述)
  6. curl -fsSL https://download.某镜像源.com/linux/ubuntu/gpg | sudo apt-key add -
  7. # 添加仓库
  8. sudo add-apt-repository "deb [arch=amd64] https://download.某镜像源.com/linux/ubuntu $(lsb_release -cs) stable"
  9. # 安装Docker CE
  10. sudo apt update && sudo apt install docker-ce docker-ce-cli containerd.io -y

5.2 配置非root用户

  1. sudo usermod -aG docker $USER # 当前用户加入docker组
  2. newgrp docker # 立即生效
  3. docker run hello-world # 验证安装

5.3 生产环境配置

创建/etc/docker/daemon.json

  1. {
  2. "registry-mirrors": ["https://<your-mirror>.mirror.aliyuncs.com"],
  3. "exec-opts": ["native.cgroupdriver=systemd"],
  4. "log-driver": "json-file",
  5. "log-opts": {
  6. "max-size": "100m"
  7. },
  8. "storage-driver": "overlay2"
  9. }

应用配置:

  1. sudo systemctl restart docker
  2. docker info | grep "Cgroup Driver" # 验证配置

六、组件协同与监控建议

  1. Nginx反向代理配置示例

    1. server {
    2. listen 80;
    3. server_name api.example.com;
    4. location / {
    5. proxy_pass http://localhost:8080; # 代理到Docker容器
    6. proxy_set_header Host $host;
    7. }
    8. }
  2. 监控方案

  • 基础监控:htopnmon
  • 日志集中:ELK Stack或行业常见日志服务
  • 告警系统:Prometheus+Grafana或云平台监控服务
  1. 备份策略
  • Redis:SAVE命令或AOF持久化
  • PostgreSQL:pg_dump定时备份
  • Docker:卷备份与镜像仓库同步

七、常见问题处理

  1. 端口冲突

    • 使用ss -tulnp | grep <port>检查占用
    • 修改服务配置文件中的端口参数
  2. 权限问题

    • 确保服务用户对数据目录有读写权限
    • 使用chown -R修正文件所有权
  3. 性能调优

    • Nginx:调整worker_processes和worker_connections
    • Redis:优化maxmemory和淘汰策略
    • PostgreSQL:调整shared_buffers和work_mem

通过本文的详细指导,运维人员可以系统掌握Linux环境下核心组件的部署方法,构建出高可用、易维护的技术栈。建议在实际操作前进行沙箱环境验证,并根据业务需求调整配置参数。