Nginx 教程

纯干货教学,从零开始学习 Nginx

Nginx 安装

Nginx 可以在多种操作系统上安装,包括 Linux、Windows 和 macOS。本章节将介绍在不同操作系统上安装 Nginx 的方法,以及如何验证安装是否成功。

🎯 安装准备

在安装 Nginx 之前,建议:

  • 确保系统已更新到最新版本
  • 确保有足够的磁盘空间
  • 确保有适当的权限(如 root 或管理员权限)
  • 了解您的系统架构(32位或64位)

在 Linux 上安装 Nginx

Linux 是 Nginx 的首选平台,支持多种 Linux 发行版。

在 Ubuntu/Debian 上安装

# 更新包索引
apt update

# 安装 Nginx
apt install nginx

# 启动 Nginx 服务
systemctl start nginx

# 设置 Nginx 开机自启
systemctl enable nginx

# 检查 Nginx 服务状态
systemctl status nginx

# 验证 Nginx 是否安装成功
curl -I http://localhost

在 CentOS/RHEL 上安装

# 安装 EPEL 仓库(CentOS 7)
yum install epel-release

# 更新包索引
yum update

# 安装 Nginx
yum install nginx

# 启动 Nginx 服务
systemctl start nginx

# 设置 Nginx 开机自启
systemctl enable nginx

# 检查 Nginx 服务状态
systemctl status nginx

# 验证 Nginx 是否安装成功
curl -I http://localhost

在 Fedora 上安装

# 更新包索引
dnf update

# 安装 Nginx
dnf install nginx

# 启动 Nginx 服务
systemctl start nginx

# 设置 Nginx 开机自启
systemctl enable nginx

# 检查 Nginx 服务状态
systemctl status nginx

# 验证 Nginx 是否安装成功
curl -I http://localhost

在 Arch Linux 上安装

# 更新包索引
pacman -Syu

# 安装 Nginx
pacman -S nginx

# 启动 Nginx 服务
systemctl start nginx

# 设置 Nginx 开机自启
systemctl enable nginx

# 检查 Nginx 服务状态
systemctl status nginx

# 验证 Nginx 是否安装成功
curl -I http://localhost

在 Windows 上安装 Nginx

在 Windows 上安装 Nginx 相对简单,主要步骤如下:

步骤 1: 下载 Nginx

  1. 访问 Nginx 官方下载页面
  2. 选择适合您系统的版本(建议选择稳定版)
  3. 下载 Windows 版本的 ZIP 包

步骤 2: 解压安装

  1. 将下载的 ZIP 包解压到您选择的目录(如 C:\nginx
  2. 解压后,您将看到 nginx.exe 可执行文件

步骤 3: 启动 Nginx

# 使用命令提示符启动 Nginx
cd C:\nginx
nginx.exe

# 检查 Nginx 是否启动成功
# 打开浏览器访问 http://localhost

# 停止 Nginx
nginx.exe -s stop

# 重新加载配置
nginx.exe -s reload

# 测试配置
nginx.exe -t

步骤 4: 将 Nginx 注册为系统服务(可选)

您可以使用 sc create 命令将 Nginx 注册为系统服务,或使用第三方工具如 nginx-service.exe

在 macOS 上安装 Nginx

在 macOS 上安装 Nginx 有两种主要方法:使用 Homebrew 或从源码编译。

使用 Homebrew 安装

# 更新 Homebrew
brew update

# 安装 Nginx
brew install nginx

# 启动 Nginx
brew services start nginx

# 停止 Nginx
brew services stop nginx

# 重启 Nginx
brew services restart nginx

# 验证 Nginx 是否安装成功
curl -I http://localhost:8080

从源码编译安装(通用方法)

从源码编译安装 Nginx 可以自定义配置和模块,适合需要特定功能的用户。

# 下载 Nginx 源码
wget http://nginx.org/download/nginx-1.24.0.tar.gz

# 解压源码
tar -zxvf nginx-1.24.0.tar.gz

# 进入源码目录
cd nginx-1.24.0

# 配置 Nginx
./configure \
    --prefix=/usr/local/nginx \
    --conf-path=/etc/nginx/nginx.conf \
    --pid-path=/var/run/nginx.pid \
    --lock-path=/var/run/nginx.lock \
    --with-http_ssl_module \
    --with-http_v2_module \
    --with-http_gzip_static_module \
    --with-http_stub_status_module \
    --with-stream

# 编译和安装
make
make install

# 创建 Nginx 用户
useradd -r nginx

# 启动 Nginx
/usr/local/nginx/sbin/nginx

# 验证 Nginx 是否安装成功
curl -I http://localhost

小众 Linux 发行版安装

在 Alpine Linux 上安装

# 更新包索引
apk update

# 安装 Nginx
apk add nginx

# 启动 Nginx
rc-service nginx start

# 设置 Nginx 开机自启
rc-update add nginx default

# 检查 Nginx 状态
rc-service nginx status

# 验证 Nginx 是否安装成功
curl -I http://localhost

在 Slackware 上安装

# 使用 slackpkg 安装(如果可用)
slackpkg update
slackpkg install nginx

# 或者从源码编译安装
# 下载 Nginx 源码
wget http://nginx.org/download/nginx-1.24.0.tar.gz

# 解压并编译安装
# 参考通用源码编译安装步骤

# 启动 Nginx
/etc/rc.d/rc.nginx start

# 设置 Nginx 开机自启
# 在 /etc/rc.d/rc.local 中添加:/etc/rc.d/rc.nginx start

# 验证 Nginx 是否安装成功
curl -I http://localhost

在 Gentoo Linux 上安装

# 更新包索引
emerge --sync

# 安装 Nginx
emerge nginx

# 启动 Nginx
/etc/init.d/nginx start

# 设置 Nginx 开机自启
rc-update add nginx default

# 检查 Nginx 状态
/etc/init.d/nginx status

# 验证 Nginx 是否安装成功
curl -I http://localhost

嵌入式设备和小众设备安装

在树莓派(Raspberry Pi)上安装

# 更新包索引
sudo apt update

# 安装 Nginx
sudo apt install nginx

# 启动 Nginx 服务
sudo systemctl start nginx

# 设置 Nginx 开机自启
sudo systemctl enable nginx

# 检查 Nginx 服务状态
sudo systemctl status nginx

# 验证 Nginx 是否安装成功
curl -I http://localhost

# 树莓派优化建议:修改 Nginx 配置,减少内存使用
sudo nano /etc/nginx/nginx.conf
# 调整 worker_processes 为 1
# 调整 worker_connections 为 512

在 Orange Pi 上安装

# 更新包索引
sudo apt update

# 安装 Nginx
sudo apt install nginx

# 或者使用 Orange Pi 专用包管理器
# opkg update
# opkg install nginx

# 启动 Nginx 服务
sudo systemctl start nginx

# 设置 Nginx 开机自启
sudo systemctl enable nginx

# 验证 Nginx 是否安装成功
curl -I http://localhost

在 ASUS Tinker Board 上安装

# 更新包索引
sudo apt update

# 安装 Nginx
sudo apt install nginx

# 启动 Nginx 服务
sudo systemctl start nginx

# 设置 Nginx 开机自启
sudo systemctl enable nginx

# 验证 Nginx 是否安装成功
curl -I http://localhost

其他小众系统安装

在 FreeBSD 上安装

# 使用 pkg 安装
pkg update
pkg install nginx

# 或者使用 ports 安装
cd /usr/ports/www/nginx
make install clean

# 启动 Nginx 服务
service nginx start

# 设置 Nginx 开机自启
# 在 /etc/rc.conf 中添加:nginx_enable="YES"

# 验证 Nginx 是否安装成功
curl -I http://localhost

在 OpenBSD 上安装

# 使用 pkg_add 安装
pkg_add nginx

# 启动 Nginx 服务
rcctl start nginx

# 设置 Nginx 开机自启
rcctl enable nginx

# 验证 Nginx 是否安装成功
curl -I http://localhost

在 NetBSD 上安装

# 使用 pkgin 安装
pkgin update
pkgin install nginx

# 或者使用 pkg_add 安装
pkg_add nginx

# 启动 Nginx 服务
/etc/rc.d/nginx start

# 设置 Nginx 开机自启
# 在 /etc/rc.conf 中添加:nginx=YES

# 验证 Nginx 是否安装成功
curl -I http://localhost

在 Solaris 上安装

# 使用 pkg 安装(Oracle Solaris 11+)
pkg install nginx

# 或者从源码编译安装
# 参考通用源码编译安装步骤

# 启动 Nginx 服务
svcadm enable svc:/network/http:nginx

# 检查 Nginx 状态
svcs -l svc:/network/http:nginx

# 验证 Nginx 是否安装成功
curl -I http://localhost

在 AIX 上安装

# 使用 yum 安装(如果可用)
yum install nginx

# 或者从源码编译安装
# 参考通用源码编译安装步骤,但需要注意 AIX 特有的编译选项

# 启动 Nginx 服务
startsrc -s nginx

# 设置 Nginx 开机自启
# 在 /etc/rc.d/rc.local 中添加启动命令

# 验证 Nginx 是否安装成功
curl -I http://localhost

验证 Nginx 安装

安装完成后,您可以通过以下方法验证 Nginx 是否安装成功:

方法 1: 使用 curl 命令

# 检查 HTTP 响应头
curl -I http://localhost

# 检查完整响应
curl http://localhost

方法 2: 使用浏览器

打开浏览器,访问 http://localhost,如果看到 Nginx 的欢迎页面,则表示安装成功。

方法 3: 检查 Nginx 版本

# 检查 Nginx 版本
nginx -v

# 检查 Nginx 详细版本信息
nginx -V

Nginx 安装目录结构

不同操作系统和安装方法会导致 Nginx 的目录结构略有不同,以下是常见的目录结构:

Ubuntu/Debian 目录结构

# 主配置文件
/etc/nginx/nginx.conf

# 站点配置文件目录
/etc/nginx/sites-available/
/etc/nginx/sites-enabled/

# 模块配置目录
/etc/nginx/conf.d/

# 日志文件目录
/var/log/nginx/

# 网站根目录
/var/www/html/

# Nginx 可执行文件
/usr/sbin/nginx

# Nginx 模块目录
/usr/lib/nginx/modules/

CentOS/RHEL 目录结构

# 主配置文件
/etc/nginx/nginx.conf

# 站点配置文件目录
/etc/nginx/conf.d/

# 日志文件目录
/var/log/nginx/

# 网站根目录
/usr/share/nginx/html/

# Nginx 可执行文件
/usr/sbin/nginx

# Nginx 模块目录
/usr/lib64/nginx/modules/

Nginx 服务管理

安装完成后,您需要管理 Nginx 服务,包括启动、停止、重启和重载配置。

使用 systemctl 管理服务(systemd 系统)

# 启动 Nginx 服务
systemctl start nginx

# 停止 Nginx 服务
systemctl stop nginx

# 重启 Nginx 服务
systemctl restart nginx

# 重载 Nginx 配置
systemctl reload nginx

# 查看 Nginx 服务状态
systemctl status nginx

# 启用 Nginx 开机自启
systemctl enable nginx

# 禁用 Nginx 开机自启
systemctl disable nginx

使用 service 管理服务(SysV 系统)

# 启动 Nginx 服务
service nginx start

# 停止 Nginx 服务
service nginx stop

# 重启 Nginx 服务
service nginx restart

# 重载 Nginx 配置
service nginx reload

# 查看 Nginx 服务状态
service nginx status

常见安装问题及解决方法

端口占用问题

问题:Nginx 启动失败,提示端口 80 已被占用。

解决方法

# 查看哪个进程占用了端口 80
lsof -i :80

# 或者使用 netstat
netstat -tulpn | grep :80

# 停止占用端口的进程,或者修改 Nginx 配置使用其他端口

权限问题

问题:Nginx 无法访问网站目录,出现 403 Forbidden 错误。

解决方法

# 检查网站目录权限
ls -la /var/www/html

# 修改网站目录权限
chmod 755 /var/www/html

# 修改网站目录所有者
chown -R www-data:www-data /var/www/html

# 检查 selinux 状态(CentOS/RHEL)
getenforce

# 临时禁用 selinux
setenforce 0

# 永久禁用 selinux(修改配置文件)
vi /etc/selinux/config
# 将 SELINUX=enforcing 改为 SELINUX=disabled

配置文件错误

问题:Nginx 启动失败,提示配置文件有错误。

解决方法

# 测试 Nginx 配置
nginx -t

# 查看详细错误信息
nginx -t -c /etc/nginx/nginx.conf

# 根据错误信息修改配置文件
vi /etc/nginx/nginx.conf

💡 学习提示

安装 Nginx 时的最佳实践:

  • 使用官方推荐的安装方法,确保安全性和稳定性
  • 定期更新 Nginx 到最新稳定版,获取安全补丁和新特性
  • 使用版本控制管理 Nginx 配置文件,便于回滚和管理
  • 为不同的网站创建单独的配置文件,提高可维护性
  • 使用 nginx -t 测试配置文件,确保配置正确

📝 学习检查

通过本章节的学习,你应该掌握:

  • 在不同操作系统上安装 Nginx 的方法
  • 如何验证 Nginx 安装是否成功
  • Nginx 的目录结构
  • 如何管理 Nginx 服务
  • 常见安装问题的解决方法