🗣 Wd提问: nginx安装
🤖 Ai回答: 在 Linux 系统上安装 Nginx 有多种方式,最常见的是通过 包管理器(如 `apt`、`yum`)或 源码编译安装。以下以主流的 Ubuntu/Debian 和 CentOS/RHEL 系统为例,介绍几种常用安装方法。
✅ 方法一:Ubuntu / Debian 系统(推荐使用 apt)
1、更新系统包列表
bash
sudo apt update
2、安装 Nginx
bash
sudo apt install nginx -y
3、启动并设置开机自启
bash
sudo systemctl start nginx
sudo systemctl enable nginx
4、验证是否安装成功
打开浏览器访问服务器 IP 地址:
http://你的服务器IP
如果看到 “Welcome to nginx!” 页面,说明安装成功。
5、常用命令
bash
sudo systemctl status nginx 查看状态
sudo systemctl restart nginx 重启
sudo systemctl reload nginx 重载配置(不中断服务)
sudo nginx -t 测试配置文件语法
✅ 方法二:CentOS / RHEL / Rocky Linux / AlmaLinux(推荐使用 yum/dnf)
1、更新系统
bash
sudo yum update -y
或者如果你用的是 dnf(如 Fedora/CentOS 8+)
sudo dnf update -y
2、安装 Nginx
方式 A:使用官方仓库(推荐)
bash
sudo yum install epel-release -y 安装 EPEL 仓库(部分系统需要)
sudo yum install nginx -y
注:CentOS 8+ 及以上版本建议使用 `dnf`:
bash
sudo dnf install nginx -y
方式 B:添加官方 Nginx 仓库(获取最新版)
bash
sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
sudo yum install nginx -y
注意:根据你的系统版本修改 URL 中的 `centos/7` → 如 `centos/8` 或 `rhel/8`。
3、启动并设置开机自启
bash
sudo systemctl start nginx
sudo systemctl enable nginx
4、防火墙放行(如开启防火墙)
bash
sudo firewall-cmd permanent add-service=http
sudo firewall-cmd reload
5、验证
访问 `http://你的服务器IP`,看到欢迎页面即成功。
✅ 方法三:源码编译安装(适合定制化需求)
1、安装依赖
bash
Ubuntu/Debian
sudo apt install build-essential libpcre3 libpcre3-dev zlib1g zlib1g-dev libssl-dev -y
CentOS/RHEL
sudo yum install gcc-c++ pcre-devel zlib-devel openssl-devel -y
2、下载源码
bash
cd /tmp
wget https://nginx.org/download/nginx-1.26.0.tar.gz 最新稳定版请查看官网
tar -zxvf nginx-1.26.0.tar.gz
cd nginx-1.26.0
3、配置编译选项(可选)
bash
./configure \
prefix=/usr/local/nginx \
with-http_ssl_module \
with-http_v2_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-mail \
with-mail_ssl_module \
with-file-aio \
with-threads \
with-stream \
with-stream_ssl_module \
with-stream_realip_module
⚠️ 根据需求调整模块,如不需要某些功能可删减。
4、编译并安装
bash
make && sudo make install
5、创建 systemd 服务文件(方便管理)
创建 `/etc/systemd/system/nginx.service`:
ini
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
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=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
6、启动服务
bash
sudo systemctl daemon-reload
sudo systemctl start nginx
sudo systemctl enable nginx
📁 默认路径说明(包管理器安装)
| 类型 | 路径 |
| 主程序 | `/usr/sbin/nginx` |
| 配置文件 | `/etc/nginx/nginx.conf` |
| 站点配置 | `/etc/nginx/sites-available/`(Ubuntu)或 `/etc/nginx/conf.d/`(CentOS) |
| 日志文件 | `/var/log/nginx/access.log`, `/var/log/nginx/error.log` |
| 网站根目录 | `/var/www/html` 或 `/usr/share/nginx/html` |
🔐 安全建议
修改默认首页或删除测试页。
设置防火墙规则(只开放 80/443)。
使用 SSL/TLS 加密(可配合 Let's Encrypt)。
关闭不必要的模块和版本信息(`server_tokens off;`)。
🧪 测试配置文件语法
bash
sudo nginx -t
输出 `syntax is ok` 表示无误。
🔄 升级 Nginx(包管理器方式)
bash
sudo apt update && sudo apt upgrade nginx Ubuntu/Debian
sudo yum update nginx CentOS/RHEL
❓常见问题
Q: 访问不了?端口被占用?
A: 检查端口是否被 Apache 或其他服务占用:
bash
sudo netstat -tlnp | grep :80
sudo systemctl stop apache2 如果是 Apache
Q: 权限错误?
A: 确保网站目录权限正确:
bash
sudo chown -R www-data:www-data /var/www/html Ubuntu
sudo chown -R nginx:nginx /usr/share/nginx/html CentOS
✅ 总结:新手推荐使用包管理器安装(apt/yum),简单快捷;高级用户或需定制功能可选择源码编译。
如需进一步配置虚拟主机、反向代理、HTTPS、负载均衡等,请告诉我,我可以为你详细展开!
📌 提示:Nginx 官网下载地址:https://nginx.org/en/download.html
📌 文档地址:https://nginx.org/en/docs/
祝你安装顺利!🎉
📣 商家广告:





0
IP地址: 171.115.64.229
搜索次数: 6
提问时间: 2025-12-17 03:37:40
本站所有
❓
问答
均由Ai自动分析整理,内容仅供参考,若有误差请用“联系”里面信息通知我们人工修改或删除。
本站由
🟢
豌豆Ai
提供技术支持,使用的最新版:
豌豆Ai站群搜索引擎系统 V.25.10.25
搭建本站。