2026/4/6 16:25:03
网站建设
项目流程
Nginx是一款高性能的HTTP和反向代理Web服务器因其稳定性、丰富的功能集、简单的配置和低资源消耗而闻名。本教程将详细介绍在主流Linux发行版CentOS/RHEL和Ubuntu/Debian上安装和配置Nginx的完整流程。一、准备工作1.1 安装必要依赖在开始安装之前需要确保系统已安装必要的编译依赖包# CentOS/RHEL 系统 yum -y install gcc gcc-c pcre pcre-devel zlib zlib-devel openssl openssl-develTip如果提示缺少 wget先安装yum install -y wget依赖包说明依赖包作用gcc / gcc-cC/C 编译器pcre / pcre-devel正则表达式处理库zlib / zlib-devel数据压缩库openssl / openssl-develSSL/TLS 加密库提示如果是离线环境需要提前下载这些 RPM 包解压后使用rpm -Uvh *.rpm --nodeps --force安装。二、下载 Nginx 安装包2.1 进入安装目录cd /usr/local2.2 下载 Nginxningx官网http://nginx.org/en/download.html如果Linux联网直接在Linux服务上使用wget命令把Nginx安装包下载到/usr/local/目录中# 使用 wget 下载推荐稳定版本 wget -c http://nginx.org/download/nginx-1.28.2.tar.gzTip:如果linux不能联网下载可在nginx官网中下载安装包上传到服务器中三、解压并安装 Nginx3.1 解压安装包tar -zxvf nginx-1.28.2.tar.gz解压后会得到nginx-1.28.2目录。3.2 修改nginx文件名mv nginx-1.28.2 nginx3.3 进入源码目录cd nginx3.4 配置 Nginx基础配置指定安装目录./configure --prefix/usr/local/nginx推荐配置添加常用模块./configure \ --prefix/usr/local/nginx \ --with-http_stub_status_module \ --with-http_ssl_module⚠️常见问题如果报错./configure: error: C compiler cc is not found解决yum -y install gcc gcc-c autoconf automake make3.5 编译并安装# 编译使用 -j4 可加速编译根据 CPU 核心数调整 make # 安装 make install或者合并执行make make install四、启动和管理 Nginx4.1 Nginx 目录结构安装完成后Nginx 的主要目录结构/usr/local/nginx/ ├── conf/ # 配置文件目录 │ └── nginx.conf # 主配置文件 ├── html/ # 默认网站根目录 ├── logs/ # 日志目录 └── sbin/ # 可执行文件目录 └── nginx # Nginx 主程序4.2 启动 Nginx# 方式 1直接启动使用默认配置文件 /usr/local/nginx/sbin/nginx # 方式 2指定配置文件启动 /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf4.3 常用管理命令命令说明/usr/local/nginx/sbin/nginx启动 Nginx/usr/local/nginx/sbin/nginx -s stop快速停止强制终止/usr/local/nginx/sbin/nginx -s quit优雅停止处理完请求后停止/usr/local/nginx/sbin/nginx -s reload重载配置文件平滑重启/usr/local/nginx/sbin/nginx -t测试配置文件语法/usr/local/nginx/sbin/nginx -v查看版本/usr/local/nginx/sbin/nginx -V查看详细编译配置4.4 查看运行状态# 查看 Nginx 进程 ps -ef | grep nginx # 查看端口监听 netstat -tlnp | grep nginx # 或 ss -tlnp | grep nginx五、配置防火墙如果无法通过浏览器访问需要配置防火墙# 临时关闭防火墙测试环境 systemctl stop firewalld.service # 永久关闭防火墙不推荐生产环境 systemctl disable firewalld.service # 或者开放 80 端口推荐 firewall-cmd --zonepublic --add-port80/tcp --permanent firewall-cmd --reload六、验证安装6.1 浏览器访问在浏览器中输入服务器 IP 地址http://你的服务器IP/看到Welcome to nginx!页面即表示安装成功。6.2 curl 测试curl -I http://localhost预期输出七、设置开机自启7.1 使用 rc.local 方式# 编辑开机启动文件 vim /etc/rc.local # 在文件底部追加启动命令 /usr/local/nginx/sbin/nginx # 赋予执行权限 chmod x /etc/rc.local7.2 使用 Systemd 服务方式推荐创建服务文件sudo tee /etc/systemd/system/nginx.service EOF [Unit] Descriptionnginx - high performance web server Afternetwork.target [Service] Typeforking PIDFile/usr/local/nginx/logs/nginx.pid ExecStartPre/usr/local/nginx/sbin/nginx -t ExecStart/usr/local/nginx/sbin/nginx ExecReload/usr/local/nginx/sbin/nginx -s reload ExecStop/usr/local/nginx/sbin/nginx -s stop PrivateTmptrue [Install] WantedBymulti-user.target EOF启用服务# 重载 systemd systemctl daemon-reload # 设置开机自启 systemctl enable nginx # 使用 systemctl 管理 systemctl start nginx # 启动 systemctl stop nginx # 停止 systemctl restart nginx # 重启 systemctl reload nginx # 重载配置 systemctl status nginx # 查看状态八、常见问题8.1 出现 403 Forbidden如果访问出现 403 错误可能是权限问题# 查看 SELinux 状态 getenforce # 临时关闭 SELinux setenforce 0 # 永久关闭 sed -i s/SELINUXenforcing/SELINUXdisabled/g /etc/selinux/config或者修改网站目录权限chmod -R 755 /usr/local/nginx/html8.2 端口被占用# 查找占用 80 端口的进程 lsof -i :80 netstat -tlnp | grep :80 # 停止占用进程后重启 Nginx九、配置文件说明主配置文件位置/usr/local/nginx/conf/nginx.conf# 基本配置结构 user nginx; # 运行用户 worker_processes auto; # 工作进程数 events { worker_connections 1024; # 每个进程最大连接数 } http { include mime.types; # MIME 类型 default_type application/octet-stream; # 日志格式 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 logs/access.log main; # 访问日志 sendfile on; keepalive_timeout 65; # 虚拟主机配置 server { listen 80; # 监听端口 server_name localhost; # 域名 location / { root html; # 网站根目录 index index.html index.htm; } error_page 500 502 503 504 /50x.html; location /50x.html { root html; } } }总结步骤关键命令安装依赖yum install gcc pcre-devel zlib-devel openssl-devel下载wget http://nginx.org/download/nginx-1.24.0.tar.gz解压tar -zxvf nginx-1.24.0.tar.gz配置./configure --prefix/usr/local/nginx编译安装make make install启动/usr/local/nginx/sbin/nginx停止/usr/local/nginx/sbin/nginx -s stop重载/usr/local/nginx/sbin/nginx -s reload按照以上步骤你就可以在 Linux 系统上成功安装并运行 Nginx 了