在Linux下如何安装和卸载源码包呢

描述

10.3 源码编译安装软件包

在Linux下安装源码包是最常用的。在日常的管理工作中,阿铭的大部分软件都是通过源码安装的。安装源码包,需要我们把源代码编译成可执行的二进制文件。

如果你能读懂这些源代码,就可以修改这些源代码的自定义功能,然后再按你的需求编译。使用源码包除了可以自定义修改源代码外,还可以定制相关的功能,因为源码包在编译时可以附加额外的选项。

源码包的编译用到了Linux系统里的编译器。常见的源码包一般都是用C语言开发的,因为C语言是Linux上最标准的程序语言。

Linux上的C语言编译器称为gcc,利用它可以把C语言编译成可执行的二进制文件。所以,如果你的机器上没有安装gcc,就无法编译源码,你可以使用命令yuminstall -y gcc来完成安装。

安装源码包通常需要以下3个步骤。

(1) ./configure

这一步可以定制功能,加上相应的选项即可,具体有什么选项可以通过命令./configure --help来查看。这一步会自动检测你的Linux系统与相关的套件是否有编译该源码包时所需要的库,因为一旦缺少某个库,就不能完成编译。只有检测通过后,才会生成Makefile文件。

(2) make

使用这个命令,会根据Makefile文件中预设的参数进行编译,这一步其实就是gcc在工作了。

(3) make install

这一步是安装步骤,用于创建相关软件的存放目录和配置文件。

对于以上这3个步骤,并不是所有的源码包软件都一样,也就是说,源码包的安装并没有标准的安装步骤。这就需要你拿到源码包解压后,进入目录,找到相关的帮助文档(通常,会以INSTALL或者README为文件名)。下面阿铭会编译安装一个源码包来帮助你更深刻地理解安装源码包的过程。

10.3.1 下载源码包

下载源码包一定要去官方站点,因为你从其他网站下载的源码包很有可能是被修改过的。我们先把Nginx的源码包下载到/usr/local/src/目录下,如下所示:

 

# cd /usr/local/src/
# wget http://nginx.org/download/nginx-1.22.0.tar.gz

 

阿铭提供的下载地址为Nginx下载地址,如果该地址已失效,请到其官网获取最新的Nginx下载地址。在下载之前,阿铭先进入/usr/local/src目录,这是因为阿铭习惯把源码包都放到这个目录下。这样做的好处是方便自己和其他管理员维护。所以,阿铭建议你将下载的源码包都统一放到这个目录下。

10.3.2 解压源码包

解压.tar.gz格式的压缩包,这在前面已经介绍过,示例命令如下:

 

# tar -zxvf nginx-1.22.0.tar.gz

 

10.3.3 配置相关的选项并生成Makefile

首先,进入解压后的源码目录,在配置之前可以查看可用的配置参数,如下所示:

 

# cd nginx-1.22.0
# ./configure --help |less
 --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


  --user=USER                        set non-privileged user for
                                     worker processes
  --group=GROUP                      set non-privileged group for
                                     worker processes

 

限于篇幅,后面的内容阿铭省略了。常用的配置选项有--prefix=PREFIX,它的意思是定义软件包的安装路径。如果你想了解其他配置选项,也可以使用命令./configure --help查看详情。这里阿铭把Nginx安装在/usr/local/nginx目录下,该选项的配置为--prefix=/usr/local/nginx。配置过程如下所示:

 

# ./configure --prefix=/usr/local/nginx
checking for OS
 + Linux 4.18.0-348.el8.0.2.x86_64 x86_64
checking for C compiler ... found
 + using GNU C compiler
 + gcc version: 8.5.0 20210514 (Red Hat 8.5.0-10) (GCC)
checking for gcc -pipe switch ... found
checking for -Wl,-E switch ... found
checking for gcc builtin atomic operations ... found
checking for C99 variadic macros ... found
checking for gcc variadic macros ... found
checking for gcc builtin 64 bit byteswap ... found
checking for unistd.h ... found
checking for inttypes.h ... found
checking for limits.h ... found
checking for sys/filio.h ... not found
checking for sys/param.h ... found
checking for sys/mount.h ... found
checking for sys/statvfs.h ... found
checking for crypt.h ... found
checking for Linux specific features
checking for epoll ... found
checking for EPOLLRDHUP ... found
checking for EPOLLEXCLUSIVE ... found
checking for eventfd() ... found
checking for O_PATH ... found
checking for sendfile() ... found
checking for sendfile64() ... found
中间省略
./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.

 

不幸的是配置刚开始就报错了,因为Nginx需要zlib库,该系统并未安装。安装命令如下:

 

# yum install -y pcre-devel

 

安装后再继续上面的步骤,如下所示:

 

# ./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即可:

 

# yum install -y zlib-devel

 

然后继续./configure那一步,最终通过,验证这一步是否成功的命令是:

 

# echo $?
0

 

这里返回值是0,说明执行成功,否则就没有成功。此时就成功生成Makefile了。查看结果如下:

 

# ls -l Makefile
-rw-r--r-- 1 root root 69 11月  9 21:43 Makefile

 

10.3.4 进行编译

生成Makefile后,需要进行编译,如下所示:

 

# make
-bash:
make: 未找到命令

 

说明:这是因为我们的系统还未安装make命令,安装即可

 

# yum install –y make
# make
make -f objs/Makefile
make[1]:进入目录“/usr/local/src/nginx-1.22.0”
cc
-c -pipe -O -W -Wall -Wpointer-arith
-Wno-unused-parameter -Werror -g  -I
src/core -I src/event -I src/event/modules -I src/os/unix -I objs 
 -o objs/src/core/nginx.o 
 src/core/nginx.c
cc -c
-pipe -O -W -Wall -Wpointer-arith
-Wno-unused-parameter -Werror -g  -I
src/core -I src/event -I src/event/modules -I src/os/unix -I objs 

 

编译时会出现类似这样杂乱的信息。限于篇幅,阿铭只列出一小部分内容。编译的时间会比较长,这是因为CPU高速计算时的使用率很高。编译后再使用命令echo$?验证是否编译成功,如下所示:

 

# echo $?
0

 

如果验证结果是0,就可以执行最后一步了。

10.3.5 安装

安装源码包的命令为make install,如下所示:

 

# make install
make -f objs/Makefile install
make[1]:进入目录“/usr/local/src/nginx-1.23.0”
test -d '/usr/local/nginx' || mkdir -p '/usr/local/nginx'
test -d '/usr/local/nginx/sbin' 
 || mkdir -p '/usr/local/nginx/sbin'
test ! -f '/usr/local/nginx/sbin/nginx' 
 || mv '/usr/local/nginx/sbin/nginx'
  '/usr/local/nginx/sbin/nginx.old'
cp objs/nginx '/usr/local/nginx/sbin/nginx'
test -d '/usr/local/nginx/conf' 
 || mkdir -p '/usr/local/nginx/conf'
cp conf/koi-win '/usr/local/nginx/conf'
cp conf/koi-utf '/usr/local/nginx/conf'
cp conf/win-utf '/usr/local/nginx/conf'

 

当然,你也可以使用命令echo $?验证是否已正确安装。执行完这一步,则会在/usr/local/nginx目录下增加很多目录。使用ls命令查看该目录,如下所示:

 

# ls /usr/local/nginx/
conf  html   logs   sbin

 

到此,Nginx源码的安装就完成了。其实在日常的源码安装过程中,遇到错误不能完成安装的情况很多,这些错误通常都是因为缺少某一个库文件。

这需要你仔细琢磨报错信息或者查看当前目录下的config.log以得到相关的信息。






审核编辑:刘清

打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分