linux软件包管理-源码编译

linux的进阶篇

一,源码包的基本描述

  • 在linux环境下面安装源码包是比较常见的, 早期运维管理工作中,大部分软件都是通过源码安装的。那么安装一个源码包,是需要我们自己把源代码编译成二进制的可执行文件。
  • 源码包的编译用到了linux系统里的编译器,通常源码包都是用C语言开发的,这也是因为C语言为linux上最标准的程序语言。Linux上的C语言编译器叫做gcc,利用它就可以把C语言变成可执行的二进制文件。所以如果你的机器上没有安装gcc就没有办法去编译源码。可以使用yum -y install gcc来完成安装。

二,源码包的优点

  • 1,自定义修改源代码
  • 2,定制需要的相关功能
  • 3,新版软件优先使用更新源码

三,源码包的获取方式


四,源码包分类

  • 源码格式(需要编译安装)
  • 二进制格式(解压后可以直接使用)

五, 源码包的安装

  • 编译需要编译环境,开发环境,开发库,开发工具。
    常用的编译环境有c、c++、perl、java、python5种
    c环境的编译器:gcc(GNU C Complier)
    c++环境的编译器:g++

  • make(进行编译的动作)编译命令:c、c++的统一项目管理工具,编译时有可能调用gcc也有可能调用g++。使用makefile文件定义make按何种次序去编译源程序文件中的源程序

  • 源码安装三部曲(常见):

    • 第一步: ./configure(定制组件)

      1.指定安装路径,例如 – prefix=/opt/nginx-1.12
      2.启用或禁用某项功能, 例如 --enable-ssl
      3.和其它软件关联,例如–with-pcre
      4.检查安装环境,例如是否有编译器 gcc,是否满足软件的依赖需求
      5.检测通过后生成Makefile文件

    • 第二步: make

      1.执行make命令进行编译, 可以使用-j指定CPU核心数进行编译
      2.按Makefile文件进行编译, 编译成可执行二进制文件
      3.生成各类模块和主程序

    • 第三步: make install

      1.按Makefile定义好的路径拷贝至安装目录中
      上面介绍的源码三部曲不能百分百通用于所有源码包, 也就是说源码包的安装并非存在标准安装步骤,但是大部分源码安装都是类似的步骤
      建议:
      拿到源码包解压后,然后进入到目录找相关的帮助文档,通常会以INSTALL或者README为文件名

5.1 configure脚本的功能

  • 让用户选定编译特性
  • 检查编译环境是否符合程序编译的基本需要

5.2 编译安装注意事项

  • a,如果安装时不是使用的默认路径,则必须要修改PATH环境变量,以能够识别此程序的二进制文件路径;
    修改/etc/profile文件或在/etc/profile.d/目录建立一个以.sh为后缀的文件,在里面定义export PATH=$PATH:/path/to/somewhere

    • 增添环境变量,使得nginx源码包命令服务使用的更加方便。

      • 第一步:echo ‘export PATH=/usr/local/nginx/sbin:$PATH’ > /etc/profile.d/nginx.sh 将安装的nginx目录的nginx命令添加到环境变量中,使得我们提取命令时更加方便
      • 第二步:source /etc/profile.d/nginx.sh //将上面写入的环境变量加入到系统的环境变量中
      • 第三步:echo $PATH //查看当前的环境变量
      • 第四步:nginx 运行命令
      • 第五步:Ss -antl 查看系统侦听的端口,相应服务启动的端口是否正在被侦听。
  • b,默认情况下,系统搜索库文件的路径只有/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 //打印出系统启动时自动加载并缓存到内存中的可用库文件名及文件路径映射关系

  • c,头文件:输出给系统
    默认:系统在/usr/include中找头文件,若要增添头文件搜索路径,使用链接进行
    man文件路径:安装在–prefix指定的目录下的man目录

    • 通过链接生成头文件链接。
      在这里插入图片描述
    • 默认:系统在/usr/share/man中找man文件。此时因为编译安装的时候不是安装到默认路径下,如果要查找man文件则可以使用以下两种方法:
      man -M /path/to/man_dir command
      在/etc/man_db.conf文件中添加一条MANPATH
      #

5.3 源码包编译实例

  • 下面通过编译安装nginx来深入理解源码包安装
    • //1.基础环境准备 [root@localhost ~]# yum -y install gcc gcc-c++ make wget

    • //2.下载源码包(源码包一定要上官方站点下载,其他站点不安全) [root@localhost ~]# cd /usr/src
      [root@localhost src]# wget http://nginx.org/download/nginx-1.12.2.tar.gz

    • //3.解压源码包,并进入相应目录 [root@localhost src]# tar xf nginx-1.12.2.tar.gz
      [root@localhost src]# cd nginx-1.12.2

    • //4.配置相关的选项,并生成Makefile [root@localhost nginx-1.12.2]# ./configure --help|head

      help选项:

–help print this message
–prefix=PATH set installation prefix
–sbin-path=PATH set nginx binary pathname
–modules-path=PATH set modules path
–conf-path=PATH set nginx.conf pathname
–error-log-path=PATH set error log pathname
–pid-path=PATH set nginx.pid pathname
–lock-path=PATH set nginx.lock pathname

//后面的内容省略了,使用 ./configure --help 命令查看可以使用的选项
//一般常用的有 --prefix=PREFIX 这个选项的意思是定义软件包安装到哪里
//建议,源码包都是安装在/opt/目录下

  • //5.指定编译参数 [root@localhost nginx-1.12.2]# ./configure --prefix=/opt/nginx-1.12.2
  • //6.验证这一步命令是否成功, 非0的都不算成功 [root@localhost nginx-1.12.2]# echo $?
    0
  • //7.编译并安装 [root@localhost nginx-1.12.2]# make
    [root@localhost nginx-1.12.2]# make install
    [root@localhost nginx-1.12.2]# echo $?
  • //8.建立软链接 [root@localhost nginx-1.12.2]# ln -s /opt/nginx-1.12.2 /opt/nginx

5.4源码编译报错信息处理

  • error-1checking for C compiler … not found ./configure: error: C compiler cc is not found

    //解决方案
    [root@localhost ~]# yum -y install gcc gcc-c++ make

  • error-2./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.

    //解决方案
    [root@localhost ~]# yum install -y pcre-devel

  • error-3./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.

    //解决方案:
    [root@localhost ~]# yum -y install zlib-devel

  • error-4./configure: error: SSL modules require the OpenSSL library.
    You can either do not enable the modules, or install the OpenSSL
    library into the system, or build the OpenSSL library statically
    from the source with nginx by using --with-openssl= option.

    //解决方案
    [root@localhost ~]# yum -y install openssl-devel

5.5源码编译安装之-Nginx

1,准备源码包及资料文件。

  • 获取方式,通过Nginx官网获取
    Nginx
    在这里插入图片描述
  • 下载需要的镜像包
  • 在xshell下进行下载。

    [root@localhost ~]# wget http://103.95.217.6/nginx.org/download/nginx-1.18.0.tar.gz

  • 解压文件 tar -xf nginx-1.18.0.tar.gz

2,文件资料准备好后开始进行编译的三部曲

  • 我们首先需要安装gcc的编译器,通过命令dnf -y install gcc,否则后续的编译文件无法进行。

  • 1,运行configure配置文件,

    [root@localhost nginx-1.18.0]# ./configure --prefix=/usr/local/nginx

    • 运行后可能会提示这样一个错误

    ./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.

    • 解决方法:

    安装zlib所需要的库:dnf -y install zlib-devel

  • 2,运行make命令与make install安装命令;

    安装成功:在这里插入图片描述

  • 3,运行命令,查看网页。进入nginx的sbin目录下。
    在这里插入图片描述
    在这里插入图片描述


5.6,源码编译安装之-Apache

1,准备源码包及资料文件。

  • 获取方式,通过apache官网获取
    Apahce![]
  • 下载需要的三个镜像文件包:
  • 在xshell下进行下载。
    在这里插入图片描述
  • 现将文件都拷贝到/usr/local/src存放源文件的目录下:

    [root@localhost ~]# mv apr-1.6.5.tar.gz apr-util-1.6.1.tar.gz httpd-2.4.46.tar.gz /usr/local/

  • 然后在终端下解压:tar -xf httpd-2.4.46.tar.gz (注意三个文件都要下载,都要解压)

2,文件资料准备好后开始进行编译的三部曲

  • 1,资料解压完后应该看到这三个文件夹。

    在这里插入图片描述

  • 我们首先需要安装gcc的编译器,通过命令dnf -y install gcc,否则后续的编译文件无法进行。

  • 2,首先我们进入apr-1.6.5这个文件夹中,进行编译安装;

    (注:如果不安装这个apr代理,在编译httpd中的./configure文件时则会出现以下错误)
    Apache2.4报错checking for APR… no configure: error: APR not found. Please read the documentation.
    所以需要进行编译Apr代理

    • 编译命令:

      ttar -zxf apr-1.6.5.tar.gz
      cd apr-1.6.5
      ./configure --prefix=/usr/local/apr
      make && make install

    • 编译Apr会出现一个问题,在configure脚本文件中定义了一个目录,这个目录变量需要注释掉,否则出现以下错误:

      config.status: executing libtool commands
      rm: cannot remove ‘libtoolT’: No such file or directory

  • 3,其次我们在进行编译时又会爆出apr-util的错误,所以我们又需要对apr-uitl进行编译安装:

    checking for APR-util… no
    configure: error: APR-util not found. Please read the documentation.

    • 编译命令 (注,这个时候也会提示一个错误。)

      tar -zxf apr-util-1.3.12.tar.gz
      cd apr-util-1.3.12
      ./configure --prefix=/usr/local/apr-util -with-apr=/usr/local/apr/bin/apr
      make && make install

    • 提示错误。

      xml/apr_xml.c:35:10: 致命错误:expat.h:没有那个文件或目录
      #include <expat.h>
      ^~~~~~~~~
      编译中断。
      make[1]: *** [/root/apr-util-1.6.1/build/rules.mk:206:xml/apr_xml.lo] 错误 1
      make[1]: 离开目录“/root/apr-util-1.6.1”
      make: *** [/root/apr-util-1.6.1/build/rules.mk:118:all-recursive] 错误 1

    • 解决办法:

      dnf -y install expat-devel安装这个库文件。
      还可能会报出缺少pcre包,也是同样的安装pcre的包组。

  • 4,将apr和apr-util依赖包组安装完成后,就开始我们的Apache的安装了

    • 进入httpd目录,进行配置文件的安装:

      [root@localhost httpd-2.4.46]./configure --prefix=/usr/local/apache --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util
      [root@localhost httpd-2.4.46] make
      [root@localhost httpd-2.4.46] make install

    • 安装完成后看到以下内容表示Apache已经安装成功。
      在这里插入图片描述
    • 进入[root@localhost httpd-2.4.46]# /usr/local/apache/bin/apachectl start启动apache服务。
      然后浏览网页。
    • 注意,无法访问需要关闭防火墙和selinux的访问控制。在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/LBJ19224/article/details/109266886