请将“Aming linux”设为star,并尽快收到推送!
10.3 从源代码编译和安装软件包
Linux下安装源码包是最常用的。 在日常管理工作中,阿明的软件大部分都是通过源代码安装的。 要安装源码包,我们需要将源码编译成可执行的二进制文件。 如果你能看懂这类源代码,你就可以改变那些源代码的自定义函数,然后根据你的需要进行编译。 使用源代码包不仅可以自定义和更改源代码,还可以自定义相关功能源码编译安装视频,因为源代码包可以在编译时添加额外的选项。
源码包的编译使用Linux系统中的编译器。 常见的源码包通常都是用C语言开发的,因为C语言是Linux上最标准的编程语言。 Linux上的C语言编译器称为gcc,可以用来将C语言编译成可执行的二进制文件。 因此,如果你的机器上没有安装gcc,则很难编译源代码,可以使用命令yuminstall -y gcc来完成安装。
安装源码包一般需要以下3步。
(1)./配置
在这一步中,您可以自定义功能并添加相应的选项。 可以通过命令./configure --help查看具体选项。 这一步会手动检查你的Linux系统及相关包是否有编译源码包所需的库,因为一旦缺少某个库,编译就无法完成。 只有测量通过后,才会生成Makefile。
(2) 使
使用此命令将根据Makefile中预设的参数进行编译,尽管gcc在这一步中工作。
(3) 进行安装
该步骤为安装步骤,用于创建相关软件的存储目录和配置文件。
对于以上三个步骤,并不是所有源码包软件都是一样的,也就是说,源码包安装没有标准的安装流程。 这就需要你拿到源码包并解压,进入目录,找到相关的帮助文档(通常文件名会是INSTALL或者README)。 接下来Ming将编译并安装一个源码包,帮助大家更深入地了解安装源码包的过程。
10.3.1 下载源码包
您必须去官网下载源码包,因为您从其他网站下载的源码包很可能已经被更改。 我们首先将Nginx的源码包下载到/usr/local/src/目录下,如下图:
# cd /usr/local/src/
# wget http://nginx.org/download/nginx-1.22.0.tar.gz
Ming提供的下载地址是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查看详细信息。 这里Ming将Nginx安装在/usr/local/nginx目录下,该选项的配置为--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来获取相关信息。
关注“Aming linux”公众号,继续学习Linux技能,不断提高!
以前的文章
伸出你可爱的小手源码编译安装视频,点击右下角“喜欢”,点击“观看”!