Centos 安装Nginx错误集锦

443 阅读4分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

1. 情景再现

今天在学习Nginx的时候Windows安装的好好的,也可以正常启动了,但是Centos安装的时候出现了一些错误,记录一下

2. 安装步骤

1. tar包安装

  1. 下载Nginx安装包下载地址

    # 或者使用wget命令
    wget http://nginx.org/download/nginx-1.18.0.tar.gz
    
  2. 将压缩包上传到服务器root根目录

    scp nginx.tar.gz root@ip:
    
  3. 解压压缩包

    tar -zxvf nginx.tar.gz
    
  4. 编译nginx

    cd nginx 
    ./configure
    make
    make install
    # 查看是否安装成功
    whereis nginx
    # 启动nginx
    cd /usr/local/nginx/sbin
    ./nginx
    

2. yum安装

  1. 配置 yum 源

    # 配置 rpm源,如果出现 Unknow host error,请参考 第四章 Vmware中的错误
    rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
    # 或者手动创建一个配置文件
    #进入 yum 安装源文件目录
    cd /etc/yum.repo.d #切换到yum安装源文件目录
    vim nginx.repo #创建一个安装包源
    # 粘贴下列内容进去
    [nginx-stable]
    name=nginx stable repo
    baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
    gpgcheck=1
    enabled=1
    gpgkey=https://nginx.org/keys/nginx_signing.key
    [nginx-mainline]
    name=nginx mainline repo
    baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
    gpgcheck=1
    enabled=0
    gpgkey=https://nginx.org/keys/nginx_signing.key
    # 按 esc 退出编辑模式 :wq! 保存退出
    :wq!
    
  2. 安装nginx

    yum -y install nginx
    

3. 阿里云问题描述

  1. PCRE 缺失

./configure: error: the HTTP rewrite module requires the PCRE library. You can either disable the module by using --without-http_rewrite_module option, or install the PCRE library into the system, or build the PCRE library statically from the source with nginx by using --with-pcre= option.

解决办法:安装 PCRE

yum -y install pcre-devel
  1. HTTP缺失

./configure: error: the HTTP cache module requires md5 functions from OpenSSL library. You can either disable the module by using --without-http-cache option, or install the OpenSSL library into the system, or build the OpenSSL library statically from the source with nginx by using --with-http_ssl_module --with-openssl= options.

解决办法:安装openssl

yum -y install openssl openssl-devel
  1. 执行上述两个命令时可能会出现下面的错误

error: rpmdb: BDB0113 Thread/process 3330/139891423307584 failed: BDB1507 Thread died in Berkeley DB library error: db5 error(-30973) from dbenv->failchk: BDB0087 DB_RUNRECOVERY: Fatal error, run database recovery error: cannot open Packages index using db5 - (-30973) error: cannot open Packages database in /var/lib/rpm

解决办法:删除db,重新构建rpm

# 进入rpm目录
cd /var/lib/rpm
# 查看rpm目录
ls
# 删除所有的 db文件 小心别执行成 rm -rf /
# 可以参考 https://blog.csdn.net/geek64581/article/details/101095854 这个博客使用 mv命令代替 rm 命令
rm -rf __db*
# 重新构建 rpm
rpm --rebuliddb
# 构建完成之后重新执行即可

4. Vmware 安装问题描述

安装前需要确保系统有 gcc pcre-devel zlib-devel openssl-devel

  1. 提示没有C compiler

checking for OS

  • Linux 3.10.0-514.el7.x86_64 x86_64 checking for C compiler ... not found

./configure: error: C compiler cc is not found

错误原因:没有安装gcc

yum -y install gcc
  1. 找不到yum镜像

"Could not resolve host: mirrors.njupt.edu.cn; Unknown error"

解决办法

# 编辑 resolv.conf文件
vim /etc/resolv.conf
# 添加内容
nameserver 8.8.8.8
nameserver 8.8.4.4
# 重启network 或者重启linux
service network restart
# 或者
shutdown -r now
  1. 没有安装zlib

/configure: error: the HTTP gzip module requires the zlib library. You can either disable the module by using --without-http_gzip_module option, or install the zlib library into the system, or build the zlib library statically from the source with nginx by using --with-zlib= option.

yum -y install zlib-devel

5. 访问nginx

# 启动Nginx
cd /usr/local/niginx/sbin
./nginx
# 如果没有体质打印就是启动成功了,也可以使用ps命令查看
ps -ef|grep nginx

打开浏览器查看页面 localhost:80 或者 localhost ,因为http默认的端口是 80

在这里插入图片描述

如果使用的是Vmware访问不了的话,有两种解决办法

  1. 关闭防火墙

    #停止防火墙
    systemctl stop firewalld.service
    # 禁止防火墙开机启动
    systemctl disable firewalld.service
    
  2. 安装iptables防火墙

    # 安装iptables
    yum install iptables-services
    # 修改配置文件
    vim /etc/sysconfig/iptables
    # 按i进入编辑模式找到下列内容
    -A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
    #在该行下面添加
    -A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
    # 或者直接找到22那一行按yyp复制一行,改为80即可
    # 想复制多行需要 yynp dd是删除当前行 yy3p就是复制三行
    #按 esc退出编辑模式 :wq保存退出
    :wq! 
    #最后重启防火墙使配置生效
    systemctl restart iptables.service
    #设置防火墙开机启动
    systemctl enable iptables.service