nginx安装
原创...大约 6 分钟
Nginx 安装详细教程
摘要
Nginx是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器。本教程将详细介绍Nginx的多种安装方式,包括包管理器安装和源码编译安装。
1. 安装前准备
1.1 系统要求
- Linux操作系统(CentOS、Ubuntu、Debian等)
- 具有sudo权限的用户
- 确保系统已更新到最新版本
1.2 更新系统
# CentOS/RHEL
sudo yum update -y
# Ubuntu/Debian
sudo apt update && sudo apt upgrade -y2. 包管理器安装
2.1 CentOS/RHEL 系统
1. 安装EPEL仓库(CentOS 7及以下)
sudo yum install epel-release -y2. 安装Nginx
# CentOS 7及以下
sudo yum install nginx -y
# CentOS 8/Rocky Linux/AlmaLinux
sudo dnf install nginx -y3. 启动并设置开机自启
# 启动nginx服务
sudo systemctl start nginx
# 设置开机自启
sudo systemctl enable nginx
# 查看服务状态
sudo systemctl status nginx4. 防火墙配置
# 开放HTTP端口
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload2.2 Ubuntu/Debian 系统
1. 安装Nginx
sudo apt install nginx -y2. 启动并设置开机自启
# 启动nginx服务
sudo systemctl start nginx
# 设置开机自启
sudo systemctl enable nginx
# 查看服务状态
sudo systemctl status nginx3. 防火墙配置(如果启用了ufw)
sudo ufw allow 'Nginx Full'
sudo ufw status3. 源码编译安装
3.1 安装编译依赖
CentOS/RHEL
sudo yum groupinstall "Development Tools" -y
sudo yum install pcre-devel zlib-devel openssl-devel wget -yUbuntu/Debian
sudo apt install build-essential libpcre3-dev zlib1g-dev libssl-dev wget -y3.2 创建nginx用户
sudo useradd -r -s /sbin/nologin nginx3.3 下载Nginx源码
# 创建下载目录
mkdir -p ~/nginx-build && cd ~/nginx-build
# 下载最新稳定版(请访问nginx.org获取最新版本号)
wget http://nginx.org/download/nginx-1.24.0.tar.gz
# 解压
tar -xzf nginx-1.24.0.tar.gz
cd nginx-1.24.03.4 配置编译选项
./configure --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-file-aio --with-threads./configure \
--prefix=/etc/nginx \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--access-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/run/nginx.lock \
--http-client-body-temp-path=/var/cache/nginx/client_temp \
--http-proxy-temp-path=/var/cache/nginx/proxy_temp \
--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \
--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \
--http-scgi-temp-path=/var/cache/nginx/scgi_temp \
--user=nginx \
--group=nginx \
--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-threads \
--with-stream \
--with-stream_ssl_module \
--with-http_slice_module \
--with-file-aio \
--with-http_v2_module3.4.1 编译参数详细说明
基础路径配置:
--prefix=/etc/nginx- 设置Nginx安装的根目录--sbin-path=/usr/sbin/nginx- 指定nginx二进制文件的路径--conf-path=/etc/nginx/nginx.conf- 主配置文件路径--error-log-path=/var/log/nginx/error.log- 错误日志文件路径--access-log-path=/var/log/nginx/access.log- 访问日志文件路径--pid-path=/var/run/nginx.pid- PID文件路径--lock-path=/var/run/nginx.lock- 锁文件路径
临时文件路径配置:
--http-client-body-temp-path=/var/cache/nginx/client_temp- 客户端请求体临时文件目录--http-proxy-temp-path=/var/cache/nginx/proxy_temp- 代理临时文件目录--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp- FastCGI临时文件目录--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp- uWSGI临时文件目录--http-scgi-temp-path=/var/cache/nginx/scgi_temp- SCGI临时文件目录
用户和组配置:
--user=nginx- 指定运行nginx的用户--group=nginx- 指定运行nginx的用户组
核心功能模块:
--with-http_ssl_module- 启用SSL/HTTPS支持(必需)--with-http_realip_module- 获取真实客户端IP地址(用于负载均衡后端)--with-http_v2_module- 启用HTTP/2协议支持--with-threads- 启用线程池支持,提高性能--with-file-aio- 启用异步文件I/O,提升大文件传输性能
内容处理模块:
--with-http_addition_module- 在响应内容前后添加文本--with-http_sub_module- 内容替换过滤器--with-http_slice_module- 将请求分割为多个子请求
媒体文件支持:
--with-http_flv_module- 支持Flash视频(FLV)文件的伪流媒体服务--with-http_mp4_module- 支持MP4文件的伪流媒体服务
压缩和缓存:
--with-http_gzip_static_module- 支持发送预压缩的.gz文件--with-http_gunzip_module- 为不支持gzip的客户端解压内容
其他实用模块:
--with-http_dav_module- 启用WebDAV支持(文件管理)--with-http_random_index_module- 随机选择目录中的文件作为索引--with-http_secure_link_module- 生成和验证安全链接--with-http_stub_status_module- 提供基本状态信息页面--with-http_auth_request_module- 基于外部请求结果进行访问控制
流处理模块:
--with-stream- 启用TCP/UDP代理和负载均衡功能--with-stream_ssl_module- 为stream模块启用SSL支持
自定义编译选项
你可以根据实际需求调整编译选项:
- 如果不需要某些功能,可以移除对应的
--with-*参数 - 如果需要其他模块,可以添加相应的编译选项
- 查看所有可用选项:
./configure --help
注意事项
- 编译选项一旦确定,后续修改需要重新编译
- 某些模块可能需要额外的系统库支持
- 建议保留SSL、realip、stub_status等常用模块
3.5 编译和安装
# 编译(使用多核心加速)
make -j$(nproc)
# 安装
sudo make install3.6 创建必要的目录
sudo mkdir -p /var/cache/nginx/client_temp
sudo mkdir -p /var/cache/nginx/proxy_temp
sudo mkdir -p /var/cache/nginx/fastcgi_temp
sudo mkdir -p /var/cache/nginx/uwsgi_temp
sudo mkdir -p /var/cache/nginx/scgi_temp
sudo chown -R nginx:nginx /var/cache/nginx3.7 创建systemd服务文件
注意目录
sudo tee /etc/systemd/system/nginx.service > /dev/null <<EOF
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/bin/kill -s HUP \$MAINPID
ExecStop=/bin/kill -s QUIT \$MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF3.8 启动服务并设置开机自启
# 重新加载systemd配置
sudo systemctl daemon-reload
# 启动nginx服务
sudo systemctl start nginx
# 设置开机自启
sudo systemctl enable nginx
# 查看服务状态
sudo systemctl status nginx4. 必要依赖包说明
4.1 核心依赖
- PCRE (Perl Compatible Regular Expressions): 用于正则表达式支持
- zlib: 用于gzip压缩功能
- OpenSSL: 用于SSL/TLS加密支持
4.2 可选模块说明
--with-http_ssl_module: SSL/HTTPS支持--with-http_realip_module: 获取真实客户端IP--with-http_gzip_static_module: 静态gzip压缩--with-http_stub_status_module: 状态监控--with-stream: TCP/UDP代理支持--with-http_v2_module: HTTP/2协议支持
5. 基本配置
5.1 默认配置文件位置
- 主配置文件:
/etc/nginx/nginx.conf - 站点配置目录:
/etc/nginx/conf.d/ - 默认网站根目录:
/usr/share/nginx/html/
5.2 基本配置示例
# /etc/nginx/nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# 包含其他配置文件
include /etc/nginx/conf.d/*.conf;
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
location / {
index index.html index.htm;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}6. 常用命令
6.1 服务管理
# 启动nginx
sudo systemctl start nginx
# 停止nginx
sudo systemctl stop nginx
# 重启nginx
sudo systemctl restart nginx
# 重新加载配置(不中断服务)
sudo systemctl reload nginx
# 查看服务状态
sudo systemctl status nginx
# 设置开机自启
sudo systemctl enable nginx
# 取消开机自启
sudo systemctl disable nginx6.2 配置测试
# 测试配置文件语法
sudo nginx -t
# 测试配置文件并显示详细信息
sudo nginx -T
# 查看nginx版本和编译信息
nginx -V6.3 日志查看
# 查看错误日志
sudo tail -f /var/log/nginx/error.log
# 查看访问日志
sudo tail -f /var/log/nginx/access.log7. 验证安装
7.1 检查nginx进程
ps aux | grep nginx7.2 检查端口监听
sudo netstat -tlnp | grep :80
# 或者使用ss命令
sudo ss -tlnp | grep :807.3 测试HTTP访问
curl -I http://localhost7.4 浏览器访问
在浏览器中访问 http://your_server_ip,应该能看到Nginx的欢迎页面。
8. 安全建议
8.1 隐藏版本信息
在 http 块中添加:
server_tokens off;8.2 限制上传文件大小
client_max_body_size 10M;8.3 配置防火墙
只开放必要的端口(80, 443)。
8.4 定期更新
保持Nginx版本更新,及时修复安全漏洞。
9. 故障排除
9.1 端口被占用
# 查看80端口占用情况
sudo lsof -i :809.2 权限问题
确保nginx用户有足够的权限访问网站文件:
sudo chown -R nginx:nginx /usr/share/nginx/html/9.3 SELinux问题(CentOS/RHEL)
# 临时关闭SELinux
sudo setenforce 0
# 永久关闭(需重启)
sudo sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config Powered by Waline v3.6.0