Nginx 安装与环境配置

Nginx 在不同平台的安装方法和环境配置


📋 目录


Linux 安装

Ubuntu/Debian

# 更新包索引
sudo apt update
 
# 安装 Nginx
sudo apt install nginx
 
# 启动 Nginx
sudo systemctl start nginx
 
# 设置开机自启
sudo systemctl enable nginx

CentOS/RHEL

# 安装 EPEL 仓库
sudo yum install epel-release
 
# 安装 Nginx
sudo yum install nginx
 
# 启动 Nginx
sudo systemctl start nginx
 
# 设置开机自启
sudo systemctl enable nginx

使用官方仓库安装最新版

# Ubuntu/Debian
sudo apt install curl gnupg2 ca-certificates lsb-release ubuntu-keyring
 
curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor \
    | sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null
 
echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] \
http://nginx.org/packages/ubuntu `lsb_release -cs` nginx" \
    | sudo tee /etc/apt/sources.list.d/nginx.list
 
sudo apt update
sudo apt install nginx

macOS 安装

使用 Homebrew

# 安装 Homebrew(如未安装)
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
 
# 安装 Nginx
brew install nginx
 
# 启动 Nginx
brew services start nginx
 
# 停止 Nginx
brew services stop nginx

Windows 安装

使用 Chocolatey

# 安装 Chocolatey(如未安装)
Set-ExecutionPolicy Bypass -Scope Process -Force; \
  [System.Net.ServicePointManager]::SecurityProtocol = \
  [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; \
  iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))
 
# 安装 Nginx
choco install nginx
 
# 启动 Nginx
nginx

手动安装

  1. 下载 Nginx for Windows: http://nginx.org/en/download.html
  2. 解压到 C:\nginx
  3. 双击 nginx.exe 启动
  4. 或使用命令行:
    cd C:\nginx
    start nginx

源码编译安装

准备环境

# Ubuntu/Debian
sudo apt update
sudo apt install build-essential libpcre3 libpcre3-dev zlib1g zlib1g-dev \
                 libssl-dev libgd-dev libxml2 libxml2-dev uuid-dev
 
# CentOS/RHEL
sudo yum groupinstall "Development Tools"
sudo yum install pcre-devel zlib-devel openssl-devel gd-devel libxml2-devel

编译安装

# 下载源码
cd /usr/src
wget http://nginx.org/download/nginx-1.24.0.tar.gz
tar -zxvf nginx-1.24.0.tar.gz
cd nginx-1.24.0
 
# 配置
./configure \
    --prefix=/etc/nginx \
    --sbin-path=/usr/sbin/nginx \
    --conf-path=/etc/nginx/nginx.conf \
    --error-log-path=/var/log/nginx/error.log \
    --http-log-path=/var/log/nginx/access.log \
    --pid-path=/var/run/nginx.pid \
    --lock-path=/var/run/nginx.lock \
    --with-http_ssl_module \
    --with-http_realip_module \
    --with-http_addition_module \
    --with-http_sub_module \
    --with-http_dav_module \
    --with-http_flv_module \
    --with-http_mp4_module \
    --with-http_gunzip_module \
    --with-http_gzip_static_module \
    --with-http_random_index_module \
    --with-http_secure_link_module \
    --with-http_stub_status_module \
    --with-http_auth_request_module \
    --with-http_xslt_module=dynamic \
    --with-http_image_filter_module=dynamic \
    --with-http_geoip_module=dynamic \
    --with-http_perl_module=dynamic \
    --with-threads \
    --with-stream \
    --with-stream_ssl_module \
    --with-stream_ssl_preread_module \
    --with-mail \
    --with-mail_ssl_module \
    --with-file-aio \
    --with-http_v2_module \
    --with-ipv6
 
# 编译和安装
make
sudo make install
 
# 创建 systemd 服务
sudo cat > /etc/systemd/system/nginx.service << EOF
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
 
[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.conf
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP \$MAINPID
ExecStop=/bin/kill -s TERM \$MAINPID
 
[Install]
WantedBy=multi-user.target
EOF
 
# 启动 Nginx
sudo systemctl daemon-reload
sudo systemctl start nginx
sudo systemctl enable nginx

验证安装

检查版本

nginx -v
# 或
nginx -V  # 显示详细编译信息

检查配置

nginx -t

访问测试

# 浏览器访问
http://localhost
 
# 或使用 curl
curl http://localhost

如果看到 Nginx 欢迎页面,说明安装成功。


环境配置

目录结构

/etc/nginx/                    # 主配置目录
├── nginx.conf                 # 主配置文件
├── mime.types                 # MIME 类型定义
├── fastcgi_params             # FastCGI 参数
├── uwsgi_params               # uWSGI 参数
├── scgi_params                # SCGI 参数
├── conf.d/                    # 通用配置片段
├── sites-available/           # 可用站点配置(Ubuntu/Debian)
├── sites-enabled/             # 启用站点配置(Ubuntu/Debian)
└── modules-available/         # 可用模块(Ubuntu/Debian)
    └── *.conf

/var/log/nginx/                # 日志目录
├── access.log                 # 访问日志
└── error.log                  # 错误日志

/var/www/html/                 # 默认网站根目录(Ubuntu/Debian)
/usr/share/nginx/html/         # 默认网站根目录(CentOS/RHEL)

常用命令

# 启动 Nginx
sudo systemctl start nginx
 
# 停止 Nginx
sudo systemctl stop nginx
 
# 重启 Nginx
sudo systemctl restart nginx
 
# 重载配置(不中断服务)
sudo systemctl reload nginx
# 或
sudo nginx -s reload
 
# 查看状态
sudo systemctl status nginx
 
# 开机自启
sudo systemctl enable nginx
 
# 禁用开机自启
sudo systemctl disable nginx

防火墙配置

# Ubuntu/Debian (ufw)
sudo ufw allow 'Nginx Full'
sudo ufw allow 'Nginx HTTP'
sudo ufw allow 'Nginx HTTPS'
 
# CentOS/RHEL (firewalld)
sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload

配置文件权限

# 设置正确的文件权限
sudo chown -R www-data:www-data /var/www/html
sudo chmod -R 755 /var/www/html
 
# Nginx 进程用户
user www-data;  # Ubuntu/Debian
user nginx;     # CentOS/RHEL

🔧 常见问题

端口被占用

# 查看端口占用
sudo netstat -tulpn | grep :80
sudo lsof -i :80
 
# 停止占用端口的进程
sudo systemctl stop apache2  # 如果 Apache 占用

SELinux 问题(CentOS/RHEL)

# 临时禁用 SELinux
sudo setenforce 0
 
# 永久禁用 SELinux
sudo sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config

权限问题

# 检查 Nginx 运行用户
ps aux | grep nginx
 
# 检查文件权限
ls -la /var/www/html/
 
# 修复权限
sudo chown -R www-data:www-data /var/www/html

📚 相关链接


标签: nginx 安装 配置 运维