在 CentOS 系统上安装 Nginx 可以通过以下步骤完成,这里提供两种主流方法:YUM 仓库安装(推荐)和源码编译安装。
sudo yum update -y
Nginx 在默认的 CentOS 仓库中可能不存在,需要先安装 EPEL:
sudo yum install epel-release -y
sudo yum install nginx -y
sudo systemctl start nginx
sudo systemctl enable nginx
bash
sudo systemctl status nginx
Welcome to Nginx
页面即表示成功。sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
sudo yum groupinstall "Development Tools" -y
sudo yum install pcre-devel zlib-devel openssl-devel -y
从 官网 获取最新稳定版链接:
wget https://nginx.org/download/nginx-1.25.3.tar.gz
tar -zxvf nginx-1.25.3.tar.gz
cd nginx-1.25.3
./configure --prefix=/usr/local/nginx --with-http_ssl_module
make
sudo make install
sudo /usr/local/nginx/sbin/nginx
创建 Systemd 服务文件 /etc/systemd/system/nginx.service
:
[Unit]
Description=The Nginx HTTP Server
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
然后启用服务:
sudo systemctl daemon-reload
sudo systemctl start nginx
sudo systemctl enable nginx
端口冲突:如果 Apache 或其他服务占用了 80 端口,需先停止它们。
sudo systemctl stop httpd
SELinux 拦截:临时禁用 SELinux 或调整策略:
sudo setenforce 0 # 临时关闭
验证配置文件:
sudo nginx -t # 检查配置语法
安装完成后,配置文件默认路径:
- YUM 安装:/etc/nginx/nginx.conf
- 源码安装:/usr/local/nginx/conf/nginx.conf