Linux-源码安装包管理

Linux-源码安装包管理

源码包基本概述

在linux环境下面安装源码包是比较常见的, 早期运维管理工作中,大部分软件都是通过源码安装的。那么安装一个源码包,是需要我们自己把源代码编译成二进制的可执行文件。

源码包的编译用到了linux系统里的编译器,通常源码包都是用C语言开发的,这也是因为C语言为linux上最标准的程序语言。Linux上的C语言编译器叫做gcc,利用它就可以把C语言变成可执行的二进制文件。所以如果你的机器上没有安装gcc就没有办法去编译源码。

可以使用yum -y install gcc来完成安装

源码包的好处

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

源码包的获取

官方网站, 可以获得最新的软件包

源码包的安装三部曲

第一步: ./configure

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

第二步: make

1.执行make命令进行编译
2.按Makefile文件进行编译, 编译成可执行二进制文件
3.生成各类模块和主程序

第三步: make install

按Makefile定义好的路径拷贝至安装目录中

拿到源码包解压后,然后进入到目录找相关的帮助文档,通常会以INSTALL或者README为文件名

安装nginx

第一步:准备工作
[root@localhost ~]# yum -y install gcc gcc-c++ wget bzip2 make

第二步:下载源码包
[root@localhost ~]# wget http://nginx.org/download/nginx-1.18.0.tar.gz
[root@localhost ~]# ls
anaconda-ks.cfg  nginx-1.18.0.tar.gz

第三步:解压源码包,并进入相应目录
[root@localhost ~]# tar xf nginx-1.18.0.tar.gz 
[root@localhost ~]# cd nginx-1.18.0
[root@localhost nginx-1.18.0]# ls
auto     CHANGES.ru  configure  html     man     src
CHANGES  conf        contrib    LICENSE  README

第四步:配置相关的选项,并生成Makefile
[root@localhost nginx-1.18.0]# ./configure --prefix=/usr/local/nginx

第五步:解决error报错
[root@localhost nginx-1.18.0]# ./configure --prefix=/usr/local/nginx
./configure: error: the HTTP rewrite module requires the PCRE library.
//需要安装PCRE的lib库
[root@localhost nginx-1.18.0]# yum -y install pcre-devel

[root@localhost nginx-1.18.0]# ./configure --prefix=/usr/local/nginx
./configure: error: the HTTP gzip module requires the zlib library.
//需要安装zlib的lib库
[root@localhost nginx-1.18.0]# yum install -y zlib-devel

[root@localhost nginx-1.18.0]# ./configure --prefix=/usr/local/nginx
Configuration summary
  + using system PCRE library
  + OpenSSL library is not used
  + using system zlib library
  
//如果想要用OpenSSL library应该怎么做?
[root@localhost nginx-1.18.0]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module
./configure: error: SSL modules require the OpenSSL library.
//需要安装OpenSSL的lib库
[root@localhost nginx-1.18.0]# yum -y install openssl openssl-devel

[root@localhost nginx-1.18.0]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module
Configuration summary
  + using system PCRE library
  + using system OpenSSL library
  + using system zlib library

第六步:验证这一步命令是否成功, 非0的都不算成功
[root@localhost nginx-1.18.0]# echo $?
0

第七步:编译并安装
[root@localhost nginx-1.18.0]# make
make[1]: Leaving directory '/root/nginx-1.18.0'
//确保没有error报错
[root@localhost nginx-1.18.0]# make install
make[1]: Leaving directory '/root/nginx-1.18.0'
[root@localhost nginx]# echo $?
0

修改环境变量

如果安装时不是使用的默认路径,则必须要修改PATH环境变量,以能够识别此程序的二进制文件路径;

修改方法:修改/etc/profile文件或在/etc/profile.d/目录建立一个以.sh为后缀的文件,在里面定义export PATH=$PATH:/(程序执行目录)

如何将刚刚安装的nginx添加到默认路径?

[root@localhost ~]# which nginx
/usr/bin/which: no nginx in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin)
export PATH=$PATH:/usr/local/nginx/sbin
[root@localhost ~]# source /etc/profile.d/nginx.sh 
[root@localhost ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/nginx/sbin
[root@localhost ~]# nginx
[root@localhost ~]# ss -antl
State       Recv-Q      Send-Q           Local Address:Port             Peer Address:Port 
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 ~]# nginx -s stop
[root@localhost ~]# ss -antl
State       Recv-Q      Send-Q           Local Address:Port             Peer Address:Port 
LISTEN      0           128                    0.0.0.0:22                    0.0.0.0:*   
LISTEN      0           128                       [::]:22                       [::]:* 
原文地址:https://www.cnblogs.com/yuqinghao/p/14135998.html