# 安装与配置

URL: https://caijiao.org/redis/01-introduction/03-installation
Source: docs/redis/01-introduction/03-installation.md
Description: 本章详细介绍 Redis 在主流操作系统上的安装步骤，包括 Linux（Ubuntu、CentOS）、macOS 以及 Windows 平台的多种安装方式，并辅以基础配置讲解，助你快速搭建稳定的 Redis 环境。

Redis 支持多种操作系统，以下分别介绍主要平台的安装方法及基础配置。

## 1. Ubuntu / Debian

* **使用包管理器安装**：

  ```bash
    sudo apt update
    sudo apt install redis-server
  ````

* **启动服务**：

  ```bash
  sudo systemctl start redis.service
  sudo systemctl enable redis.service
  ```

* **检查状态**：

  ```bash
  sudo systemctl status redis
  ```

* 默认配置文件路径：`/etc/redis/redis.conf`，可通过修改该文件调整 Redis 参数。

## 2. CentOS / RHEL

* **安装 EPEL 仓库**：

  ```bash
  sudo yum install epel-release
  ```

* **安装 Redis**：

  ```bash
  sudo yum install redis
  ```

* **启动并设置开机自启**：

  ```bash
  sudo systemctl start redis
  sudo systemctl enable redis
  ```

* 配置文件一般位于 `/etc/redis.conf`。

## 3. macOS

* **使用 Homebrew 安装**：

  ```bash
  brew update
  brew install redis
  ```

* **启动 Redis**：

  ```bash
  brew services start redis
  ```

* 也可手动启动：

  ```bash
  redis-server /usr/local/etc/redis.conf
  ```

## 4. Windows

Redis 官方不直接支持 Windows，但有以下选择：

* **微软维护的 Windows 版 Redis**（已停止更新）：
  在 GitHub 下载并安装，但版本较旧，不建议生产使用。

* **使用 WSL（Windows Subsystem for Linux）**：
  安装 Ubuntu 子系统，按 Linux 安装方法操作，推荐使用。

* **Docker 方式**：
  安装 Docker Desktop，使用官方 Redis 镜像：

  ```bash
  docker run -d -p 6379:6379 redis
  ```

## 5. 源码编译安装（适用于所有平台）

* 下载 Redis 源码包：

  ```bash
  wget http://download.redis.io/releases/redis-7.0.0.tar.gz
  tar xzf redis-7.0.0.tar.gz
  cd redis-7.0.0
  ```

* 编译安装：

  ```bash
  make
  sudo make install
  ```
  
* 运行 Redis 服务器：

  ```bash
  src/redis-server
  ```

## 基础配置

* 主要配置文件 `redis.conf` 可自定义：

  * **端口和绑定地址**：默认监听 `6379` 端口，`bind 127.0.0.1` 限制本地访问。
  * **持久化策略**：RDB 快照和 AOF 日志。
  * **安全**：设置 `requirepass` 进行密码保护。
  * **内存管理**：`maxmemory` 配合淘汰策略防止 OOM。
  * **日志和守护进程**：`daemonize yes` 可让 Redis 以守护进程运行。

合理配置并启动 Redis 服务后，即可通过客户端连接，进行数据操作和开发。
