Skip to content

CentOS 离线 RPM 安装 Nginx

下载 rpm 包:https://nginx.org/en/linux_packages.html

下载 rpm 依赖包:http://mirrors.aliyun.com/centos/7/os/x86_64/Packages/

检查环境

shell
# --test 表示仅检查安装环境不实际安装,如下所示,Failed 表示缺少 pcre2 依赖。
[root@localhost ~]# rpm -ivh --test nginx-1.24.0-1.el7.ngx.x86_64.rpm
warning: nginx-1.24.0-1.el7.ngx.x86_64.rpm: Header V4 RSA/SHA256 Signature, key ID 7bd9bf62: NOKEY
error: Failed dependencies:
        libpcre2-8.so.0()(64bit) is needed by nginx-1:1.24.0-1.el7.ngx.x86_64
[root@localhost ~]#

# 通过 --test 这个参数,就可以逐个找到 nginx 所需要的所有 rpm 依赖。

# 直到出现下面,意味着可以正式安装 nginx 了。
[root@localhost ~]# rpm -ivh --test nginx-1.24.0-1.el7.ngx.x86_64.rpm
warning: nginx-1.24.0-1.el7.ngx.x86_64.rpm: Header V4 RSA/SHA256 Signature, key ID 7bd9bf62: NOKEY
Preparing...                          ################################# [100%]
[root@localhost ~]#

以下列出部分需要的依赖(安装 Redis 的适合已经安装了一些依赖):

  • pcre2-10.23-2.el7.x86_64.rpm
shell
# 安装 nginx (去掉了 --test 参数)
[root@localhost ~]# rpm -ivh nginx-1.24.0-1.el7.ngx.x86_64.rpm
warning: nginx-1.24.0-1.el7.ngx.x86_64.rpm: Header V4 RSA/SHA256 Signature, key ID 7bd9bf62: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:nginx-1:1.24.0-1.el7.ngx         ################################# [100%]
----------------------------------------------------------------------

Thanks for using nginx!

Please find the official documentation for nginx here:
* https://nginx.org/en/docs/

Please subscribe to nginx-announce mailing list to get
the most important news about nginx:
* https://nginx.org/en/support.html

Commercial subscriptions for nginx are available on:
* https://nginx.com/products/

----------------------------------------------------------------------
[root@localhost ~]#

# 查找一下安装位置
[root@localhost opt]# find / -name nginx

重要目录及文件说明

目录说明
/etc/nginx目录。nginx 默认配置文件的目录
/etc/nginx/nginx.conf文件。nginx 主配置文件,文件最后配置了自动加载 conf.d 下*.conf 的配置
/etc/nginx/conf.d目录。nginx 的*.conf 多个子配置存放目录
/etc/nginx/conf.d/default.conf文件。nginx 子配置文件,主要用于端口、日志、负载均衡、反向代理等配置。
/usr/sbin/nginx文件。可执行二进制文件,nginx 的启停等执行命令前缀
/usr/share/nginx目录。里面包含 html 静态资源目录
/var/log/nginx目录。日志文件存放目录

配置

shell
cat /etc/nginx/nginx.conf
cat /etc/nginx/conf.d/default.conf

启动、停止、重启

shell
[root@localhost ~]# cd /usr/sbin
# 启动    /usr/sbin/nginx
[root@localhost nginx]# ./nginx
# 停止    /usr/sbin/nginx -s stop
[root@localhost nginx]# ./nginx -s stop
# 重启    /usr/sbin/nginx -s reload
[root@localhost nginx]# ./nginx -s reload
# 查看进程
[root@localhost nginx]# ps -ef | grep nginx

访问

http://localhost:80

如果访问报 502 错误,可能是操作系统中 SELinux 阻止了 nginx 的访问

SELinux 基于最小权限原则默认拦截了 Nginx 的请求,SELinux 是 Linux 的安全子系统,提供更安全的访问控制。

shell
# 确认 SELinux 状态
[root@localhost nginx]# sestatus
SELinux status:                 enabled
SELinuxfs mount:                /sys/fs/selinux
SELinux root directory:         /etc/selinux
Loaded policy name:             targeted
Current mode:                   permissive
Mode from config file:          disabled
Policy MLS status:              enabled
Policy deny_unknown status:     allowed
Max kernel policy version:      31
[root@localhost nginx]#

# 临时关闭 SELinux:
[root@localhost nginx]# setenforce 0
# 临时启动 SELinux:
# [root@localhost nginx]# setenforce 1

# 永久关闭/启动:
[root@localhost nginx]# vi /etc/sysconfig/selinux
shell
# 把下面这一行的值从 enforcing 改为 disabled 即可。其他内容省略。
SELINUX=disabled

然后重启系统,重启完系统(或者不重启,临时关闭 setenforce 0)之后,

shell
# 开启 HTTP 访问
[root@localhost nginx]# setsebool -P httpd_can_network_connect 1

至此,执行完成以上命令后即可对 Nginx 进行正常访问。