一、基础环境准备
在部署核心组件前,需确保服务器满足以下条件:
- 系统要求:推荐使用Ubuntu 20.04 LTS或CentOS 8+系统
- 权限配置:使用具有sudo权限的非root用户操作
- 网络连通:确保服务器可访问软件源仓库
- 资源分配:建议至少4GB内存和20GB可用磁盘空间
执行基础更新命令:
# Ubuntu/Debian系统sudo apt update && sudo apt upgrade -y# CentOS/RHEL系统sudo yum update -y
二、Nginx部署与配置
2.1 安装与验证
# Ubuntu安装sudo apt install nginx -y# CentOS安装sudo yum install epel-release -ysudo yum install nginx -y
验证安装成功:
nginx -v # 显示版本号nginx -V # 显示编译参数
2.2 服务管理
# 启动服务sudo systemctl start nginx# 设置开机自启sudo systemctl enable nginx# 检查服务状态sudo systemctl status nginx
2.3 防火墙配置
# Ubuntu ufw配置sudo ufw allow 'Nginx Full'sudo ufw status # 验证规则# CentOS firewalld配置sudo firewall-cmd --permanent --add-service=httpsudo firewall-cmd --permanent --add-service=httpssudo firewall-cmd --reload
2.4 配置优化
-
性能调优:修改
/etc/nginx/nginx.confworker_processes auto;worker_rlimit_nofile 65535;events {worker_connections 4096;}
-
虚拟主机配置:创建
/etc/nginx/conf.d/example.confserver {listen 80;server_name example.com;location / {root /var/www/html;index index.html;}}
-
测试配置:
sudo nginx -t # 语法检查sudo nginx -s reload # 重新加载配置
三、Redis内存数据库部署
3.1 安装与启动
# Ubuntu安装sudo apt install redis-server -y# CentOS安装sudo yum install redis -y
3.2 安全配置
-
修改
/etc/redis/redis.conf:bind 127.0.0.1 # 限制本地访问protected-mode yesrequirepass YourSecurePassword # 设置认证密码
-
重启服务:
sudo systemctl restart redis
3.3 性能优化
-
内存配置:
maxmemory 2gb # 根据服务器内存调整maxmemory-policy allkeys-lru # 内存淘汰策略
-
持久化配置:
save 900 1 # 15分钟至少1次修改save 300 10 # 5分钟至少10次修改save 60 10000
3.4 客户端测试
redis-cli127.0.0.1:6379> AUTH YourSecurePassword127.0.0.1:6379> SET test_key "Hello Redis"127.0.0.1:6379> GET test_key
四、PostgreSQL数据库部署
4.1 安装配置
# Ubuntu安装sudo apt install postgresql postgresql-contrib -y# CentOS安装sudo yum install postgresql-server postgresql-contrib -ysudo postgresql-setup --initdb
4.2 安全初始化
sudo -u postgres psqlpostgres=# ALTER USER postgres WITH PASSWORD 'SecureDBPass';postgres=# CREATE DATABASE mydb;postgres=# \q
4.3 远程访问配置
-
修改
/etc/postgresql/12/main/pg_hba.conf:host all all 0.0.0.0/0 md5
-
修改
/etc/postgresql/12/main/postgresql.conf:listen_addresses = '*'
-
重启服务:
sudo systemctl restart postgresql
4.4 客户端连接测试
psql -h 127.0.0.1 -U postgres -d mydb
五、Docker容器化平台部署
5.1 安装步骤
# Ubuntu安装sudo apt install apt-transport-https ca-certificates curl software-properties-common -ycurl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"sudo apt install docker-ce -y# CentOS安装sudo yum install -y yum-utilssudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.reposudo yum install docker-ce -y
5.2 用户组配置
sudo usermod -aG docker $USERnewgrp docker # 立即生效
5.3 基础操作
# 运行测试容器docker run hello-world# 管理命令示例docker ps -a # 查看所有容器docker images # 查看镜像列表docker system prune # 清理无用资源
5.4 生产环境优化
-
配置daemon.json:
{"storage-driver": "overlay2","log-driver": "json-file","log-opts": {"max-size": "10m","max-file": "3"}}
-
创建系统服务:
sudo systemctl enable dockersudo systemctl start docker
六、组件协同工作示例
6.1 Nginx反向代理Docker应用
server {listen 80;server_name app.example.com;location / {proxy_pass http://localhost:8080;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;}}
6.2 Redis缓存集成
# Python示例代码import redisimport psycopg2r = redis.Redis(host='localhost', password='SecureDBPass')conn = psycopg2.connect("dbname=mydb user=postgres password=SecureDBPass")def get_data(key):data = r.get(key)if not data:# 从PostgreSQL获取并缓存cur = conn.cursor()cur.execute("SELECT value FROM cache_table WHERE key=%s", (key,))result = cur.fetchone()if result:r.setex(key, 3600, result[0])data = result[0]return data
七、运维监控建议
-
资源监控:
- 使用
htop监控系统资源 - 配置Prometheus+Grafana监控套件
- 使用
-
日志管理:
- 配置rsyslog集中管理日志
- 设置logrotate定期轮转日志
-
备份策略:
- PostgreSQL使用pg_dump定期备份
- Redis使用RDB持久化+AOF日志
- 重要数据同步至对象存储服务
本指南完整覆盖了从基础环境搭建到高级配置优化的全流程,通过标准化操作步骤和实际案例演示,帮助技术人员快速掌握Linux服务器核心组件的部署技巧。建议在实际生产环境部署前,先在测试环境验证所有配置,并根据具体业务需求调整参数设置。