centOS7安装ngnix

128 阅读2分钟

在线安装

yum install -y epel-release
yum install nginx

离线安装

下载RPM安装包

方案一 离线安装无依赖包

nginx.org/packages/ce…

方案二 离线包以及依赖包

yum install -y epel-release
yum install --downloadonly --downloaddir=/nginx/ nginx
yum reinstall --downloadonly --downloaddir=/nginx/ nginx

方案三 离线包以及依赖包

yum install -y epel-release
yum install -y yum-utils
yumdownloader --destdir=/nginx --resolve nginx

方案四 离线包以及全量依赖包

yum install -y epel-release
yum install -y yum-utils
repotrack -p /nginx nginx

方案二 方案三 只会下载指定的rpm包以及当前os缺少的依赖包 方案四下载全量依赖包

安装nginx

  • 检查缺少的依赖包
rpm -ivh --test nginx-1.20.1-10.el7.x86_64.rpm

  • 安装缺少的依赖包,按照实际情况安装缺少的依赖包
yum localinstall openssl11-libs-1.1.1k-7.el7.x86_64.rpm
yum localinstall nginx-filesystem-1.20.1-10.el7.noarch.rpm
  • 安装nginx
yum localinstall nginx-1.20.1-10.el7.x86_64.rpm

配置nginx

vi /etc/nginx/nginx.conf
server {
 listen     80;
 location /api {
           #匹配到后转发到这个域名地址
            proxy_pass http://localhost:5000;
}
 location / {
            index index.html;
            root "/project/dist";
            # 检查是否是首次请求,如果是则添加header  
            if ($request_filename ~* .*\.(html)$) {  
                add_header Cache-Control no-store;  
            }  
        } 
    }

重启nginx

systemctl stop nginx
systemctl start nginx

如果失败,查看日志 12097ac416b1de4b7a865e42a35a495aae71bd533437ed2b038a0cd4736b0052.png

systemctl status nginx.service

8fe68b9ab0abc4378e32dd127adb6f991c45d165b9b1d940bf9d710db2b5f8c9.png 原因 seLinux限制了http的端口

    getenforce

这个命令可以查看当前是否开启了selinux 如果输出 disabled 或 permissive 那就是关闭了 如果输出 enforcing 那就是开启了 selinux

方法1 临时关闭selinux

    setenforce 0    ##设置SELinux 成为permissive模式
    setenforce 1    ##设置SELinux 成为enforcing模式

方法2 永久关闭selinux 修改/etc/selinux/config文件 将SELINUX=enforcing改为SELINUX=disabled 重启机器即可

方法3 添加seLinux允许的http端口 - 不推荐如果可以直接方案二

# 1、查看端口是否加入seLinux允许的http端口
 semanage port -l | grep http_port_t

# 2、添加端口
semanage port -a -t http_port_t -p tcp 80

如果出现 semanage command not found错误就执行
yum -y install policycoreutils-python

防火墙

启动: systemctl start firewalld
关闭: systemctl stop firewalld
查看状态: systemctl status firewalld 
开机禁用  : systemctl disable firewalld
开机启用  : systemctl enable firewalld

设置开放的端口号

sudo firewall-cmd --add-port=80/tcp --permanent