0
点赞
收藏
分享

微信扫一扫

源码安装Nginx 解决报错

半夜放水 2022-04-23 阅读 106
nginx

CentOS8源码安装Nginx

编译安装注意事项

  • 如果安装时不是使用的默认路径,则必须要修改PATH环境变量,以能够识别此程序的二进制文件路径;
    • 修改/etc/profile文件或在/etc/profile.d/目录建立一个以.sh为后缀的文件,在里面定义export PATH=/path/to/somewhere:$PATH
  • 默认情况下,系统搜索库文件的路径只有/lib,/usr/lib
    • 增添额外库文件搜索路径方法:
      • 在/etc/ld.so.conf.d/中创建以.conf为后缀名的文件,而后把要增添的路径直接写至此文件中。此时库文件增添的搜索路径重启后有效,若要使用增添的路径立即生效则要使用ldconfig命令
    • ldconfig:通知系统重新搜索库文件
/etc/ld.so.conf和/etc/ls.so.conf.d/*.conf    //配置文件
/etc/ld.so.cache            //缓存文件
-v      //显示重新搜索库的过程
-p      //打印出系统启动时自动加载并缓存到内存中的可用库文件名及文件路径映射关系
  • 头文件:输出给系统
    • 默认:系统在/usr/include中找头文件,若要增添头文件搜索路径,使用链接进行
  • man文件路径:安装在–prefix指定的目录下的man目录
    • 默认:系统在/usr/share/man中找man文件。此时因为编译安装的时候不是安装到默认路径下,如果要查找man文件则可以使用以下两种方法:
      • man -M /path/to/man_dir command
      • 在/etc/man_db.conf文件中添加一条MANPATH

源码包编译实例

下面通过编译安装nginx来深入理解源码包安装

//1.下载Nginx
[root@localhost ~]# wget https://nginx.org/download/nginx-1.20.2.tar.gz  

 //2.解压包
[root@localhost ~]# tar xf nginx-1.20.2.tar.gz 
[root@localhost ~]# cd nginx-1.20.2

//3.指定编译参数 
[root@localhost nginx-1.20.2]# ./configure --prefix=/usr/local/nginx  

//4.验证这一步命令是否成功, 非0的都不算成功
[root@localhost nginx-1.20.2]# echo $?
0
//查看make命令是否存在,没有
[root@localhost nginx-1.20.2]# which make
/usr/bin/which: no make in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin)
//安装make命令
[root@localhost ~]# dnf -y install make
//查看cup核心数,使用只能低于核心数
[root@localhost ~]# nproc 
4
//使用make编译必须在生成makefile的文件目录下使用若出现报错或安装失败需删除目录重新开始上述步骤
[root@localhost nginx-1.20.2]# make -j 3  

[root@localhost nginx-1.20.2]# make install

//查看Nginx里面有什么?
[root@localhost ~]# ls /usr/local/nginx
conf  html  logs  sbin

conf  放配置文件
html  放网站的
logs  放日志的
sbin  放程序文件的
//查看未发现80端口(ngxin端口80)
[root@localhost ~]# ss -antl  
State    Recv-Q   Send-Q       Local Address:Port             Peer Address:Port         Process         
LISTEN   0        128                0.0.0.0:22                    0.0.0.0:*                            
LISTEN   0        128                   [::]:22                       [::]:*        

//启动Nginx (这种方式启动nginx麻烦,设置环境变量直接nginx启动)
[root@localhost ~]# /usr/local/nginx/sbin/nginx 
[root@localhost ~]# ss -antl
State  Recv-Q Send-Q Local Address:Port   Peer Address:Port Process 
LISTEN 0      128          0.0.0.0:80          0.0.0.0:*            
LISTEN 0      128          0.0.0.0:22          0.0.0.0:*            
LISTEN 0      128             [::]:22             [::]:*   

//配置环境变量

//第一种方式配置环境变量
[root@localhost ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@localhost ~]# cat /etc/profile.d/nginx.sh 
export PATH=/usr/local/nginx/sbin:$PATH
//第二种方式配置环境变量 (vim写比较稳妥)
[root@localhost ~]# vim /etc/profile.d/nginx.sh
[root@localhost ~]# cat /etc/profile.d/nginx.sh 
export PATH=/usr/local/nginx/sbin:$PATH //写入nginx.sh文件里

//刷新
[root@localhost ~]# source /etc/profile.d/nginx.sh 
[root@localhost ~]# echo $PATH
/usr/local/nginx/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

//杀掉nginx进程
[root@localhost ~]# pkill nginx

//查看端口号
[root@localhost ~]# ss -antl
State  Recv-Q Send-Q Local Address:Port   Peer Address:Port Process 
LISTEN 0      128          0.0.0.0:22          0.0.0.0:*            
LISTEN 0      128             [::]:22             [::]:*     

//启动nginx
[root@localhost ~]# nginx
//查看端口号
[root@localhost ~]# ss -antl
State  Recv-Q Send-Q Local Address:Port   Peer Address:Port Process 
LISTEN 0      128          0.0.0.0:80          0.0.0.0:*            
LISTEN 0      128          0.0.0.0:22          0.0.0.0:*            
LISTEN 0      128             [::]:22             [::]:*     

//假设配置头文件 (因为nginx没有头文件)

//有头文件就要做软连接
[root@localhost ~]# ln -s /usr/local/nginx/include /usr/include/nginx

//关闭防火墙再去访问

//查看防火墙状态(默认开机自启)
[root@localhost ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabl>
   Active: active (running) since Fri 2022-04-22 14:14:23 CST; 1h 5>
     Docs: man:firewalld(1)
//马上关掉防火墙(关闭开机自启)     
[root@localhost ~]# systemctl disable --now firewalld

1:查看防火状态
systemctl status firewalld
service  iptables status
2:暂时关闭防火墙
systemctl stop firewalld
service  iptables stop
3:永久关闭防火墙
systemctl disable firewalld
chkconfig iptables off
4:开启防火墙
systemctl enable firewalld
service iptables restart  
5.:重启防火墙
systemctl restart firewalld

ip访问nginx (出现这个就OK了)

在这里插入图片描述

源码编译报错信息处理

//执行./configure --prefix=/usr/local/nginx报的错
./configure: error: C compiler cc is not found

//解决
[root@localhost nginx-1.20.2]# dnf -y install gcc gcc-c++

//执行dnf -y install gcc gcc-c++报的错
Error: 
 Problem: package gcc-8.5.0-4.el8_5.x86_64 requires glibc-devel >= 2.2.90-12, but none of the providers can be installed

//解决
[root@localhost nginx-1.20.2]# dnf -y install boost-devel --allowerasing

//执行./configure --prefix=/usr/local/nginx报的错
./configure: error: the HTTP rewrite module requires the PCRE library.

//解决
[root@localhost nginx-1.20.2]# dnf -y install pcre-devel

//执行./configure --prefix=/usr/local/nginx报的错
./configure: error: the HTTP gzip module requires the zlib library.

//解决
[root@localhost nginx-1.20.2]# dnf -y install zlib-devel

//问题?(使用https证书需要这个库开启),现在只管安装
Configuration summary
  + using system PCRE library
  + OpenSSL library is not used  //没有使用到系统OpenSSL库
  + using system zlib library
  
//查看系统OpenSSL库,默认是关闭的
[root@localhost nginx-1.20.2]#  ./configure --help|grep ssl
  --with-http_ssl_module             enable ngx_http_ssl_module

//启动它就解决了
[root@localhost nginx-1.20.2]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module 

//执行./configure --prefix=/usr/local/nginx --with-http_ssl_module 报的错
./configure: error: SSL modules require the OpenSSL library.

//解决
[root@localhost nginx-1.20.2]# dnf -y install openssl openssl-devel
举报

相关推荐

0 条评论