第405集软件管理-源码安装包管理 | 字数总计: 3.2k | 阅读时长: 13分钟 | 阅读量:
软件管理 - 源码安装包管理 1. 源码包基本概述 在linux环境下面安装源码包是比较常见的, 早期运维管理工作中,大部分软件都是通过源码安装的。那么安装一个源码包,是需要我们自己把源代码编译成二进制的可执行文件。
1.1 源码包编译原理 源码包的编译用到了linux系统里的编译器,通常源码包都是用C语言开发的,这也是因为C语言为linux上最标准的程序语言。Linux上的C语言编译器叫做gcc,利用它就可以把C语言变成可执行的二进制文件。所以如果你的机器上没有安装gcc就没有办法去编译源码。可以使用yum install -y gcc来完成安装。
1.2 使用源码包的好处
自定义修改源代码 : 可以根据需求修改源代码
定制需要的相关功能 : 可以启用或禁用特定功能模块
新版软件优先更新源码 : 官方通常先发布源码包
1.3 源码包与二进制包对比
特性
源码包
二进制包(RPM)
安装方式
需要编译
直接安装
安装时间
较长
较短
可定制性
高
低
依赖处理
手动解决
自动解决
适用场景
需要定制功能
快速部署
2. 源码包如何获取 2.1 获取途径
官方网站 : 可以获得最新的软件包
GitHub : 开源项目源码
SourceForge : 开源软件仓库
2.2 下载源码包 1 2 3 4 5 6 7 8 wget http://nginx.org/download/nginx-1.12.2.tar.gz curl -O http://nginx.org/download/nginx-1.12.2.tar.gz wget -P /soft/src http://nginx.org/download/nginx-1.12.2.tar.gz
2.3 源码包格式
格式
说明
解压命令
.tar.gz
gzip压缩的tar包
tar xzf
.tar.bz2
bzip2压缩的tar包
tar xjf
.tar.xz
xz压缩的tar包
tar xJf
.zip
zip压缩包
unzip
3. 源码包如何安装 3.1 编译环境准备 必需工具 :
gcc : C语言编译器
make : 构建工具
依赖库 : 根据软件需求安装(如pcre、openssl等)
1 2 3 4 5 yum install -y gcc make wget yum install -y gcc-c++ pcre-devel openssl-devel zlib-devel
3.2 源码安装三步曲(常见) 源码安装通常分为三个步骤:
功能 :
a. 指定安装路径,例如 --prefix=/soft/nginx-1.12
b. 启用或禁用某项功能, 例如 --enable-ssl
c. 和其它软件关联,例如--with-pcre
d. 检查安装环境,例如是否有编译器 gcc,是否满足软件的依赖需求
e. 检测通过后生成Makefile文件
第二步: make 功能 :
执行make命令进行编译, 可以使用-j指定CPU核心数
按Makefile文件进行编译, 编译成可执行二进制文件
生成各类模块和主程序
第三步: make install 功能 :
执行make install
按Makefile定义好的路径拷贝至安装目录中
注意 : 上面介绍的源码三部曲不能百分百通用于所有源码包, 也就是说源码包的安装并非存在标准安装步骤。
建议 : 拿到源码包解压后,然后进入到目录找相关的帮助文档,通常会以INSTALL或者README为文件名。
3.3 源码安装基本流程 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 tar xzf software.tar.gz cd software/cat READMEcat INSTALL./configure --prefix=/path/to/install make make install
4. 源码包编译实例 下面通过编译Nginx来深入理解源码包安装
4.1 基础环境准备
4.2 下载源码包 1 2 3 4 [root@node1 ~] [root@node1 src] [root@node1 src]
4.3 解压源码包 1 2 3 [root@node1 src] [root@node1 src]
4.4 配置相关的选项 1 2 3 4 5 6 7 8 9 10 [root@node1 nginx-1.12.2] --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
常用配置选项 :
--prefix=PREFIX: 定义软件包安装到哪里(建议源码包都安装在/soft/目录下)
--enable-xxx: 启用某个功能模块
--disable-xxx: 禁用某个功能模块
--with-xxx: 关联其他软件库
4.5 指定编译参数 1 2 3 4 5 6 7 8 9 10 [root@node1 nginx-1.12.2] [root@node1 nginx-1.12.2] --prefix=/soft/nginx-1.12.2 \ --with-http_ssl_module \ --with-http_stub_status_module \ --with-pcre \ --with-http_realip_module
4.6 验证配置是否成功 1 2 3 [root@node1 nginx-1.12.2] 0
返回值说明 :
0: 配置成功
非0: 配置失败,需要查看错误信息
4.7 编译并安装 1 2 3 4 5 6 7 8 9 10 [root@node1 nginx-1.12.2] [root@node1 nginx-1.12.2] [root@node1 nginx-1.12.2] [root@node1 nginx-1.12.2]
4.8 验证安装 1 2 3 4 5 6 7 8 ls -l /soft/nginx-1.12.2//soft/nginx-1.12.2/sbin/nginx -t /soft/nginx-1.12.2/sbin/nginx
5. 源码编译报错信息处理 5.1 案例一: C编译器未找到 错误信息 :
1 2 checking for C compiler … not found ./configure: error: C compiler cc is not found
解决方案 :
1 2 yum -y install gcc gcc-c++ make
5.2 案例二: PCRE库缺失 错误信息 :
1 2 3 4 ./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.
解决方案 :
1 2 3 4 5 yum install -y pcre-devel ./configure --with-pcre=/path/to/pcre
5.3 案例三: zlib库缺失 错误信息 :
1 2 3 4 5 ./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.
解决方案 :
1 2 3 4 5 yum -y install zlib-devel ./configure --without-http_gzip_module
5.4 案例四: OpenSSL库缺失 错误信息 :
1 2 3 ./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.
解决方案 :
1 2 3 4 5 yum -y install openssl-devel ./configure --without-http_ssl_module
5.5 常见依赖库安装 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 yum install -y \ gcc \ gcc-c++ \ make \ pcre-devel \ openssl-devel \ zlib-devel \ ncurses-devel \ readline-devel \ libxml2-devel \ libcurl-devel \ libjpeg-devel \ libpng-devel \ freetype-devel
6. 源码安装完整示例 6.1 Nginx完整编译示例 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 #!/bin/bash yum install -y gcc gcc-c++ make wget pcre-devel openssl-devel zlib-devel mkdir -p /soft/srccd /soft/srcwget http://nginx.org/download/nginx-1.12.2.tar.gz tar xzf nginx-1.12.2.tar.gz cd nginx-1.12.2./configure \ --prefix=/soft/nginx-1.12.2 \ --sbin-path=/soft/nginx-1.12.2/sbin/nginx \ --conf-path=/soft/nginx-1.12.2/conf/nginx.conf \ --error-log-path=/soft/nginx-1.12.2/logs/error.log \ --access-log-path=/soft/nginx-1.12.2/logs/access.log \ --pid-path=/soft/nginx-1.12.2/logs/nginx.pid \ --lock-path=/soft/nginx-1.12.2/logs/nginx.lock \ --with-http_ssl_module \ --with-http_stub_status_module \ --with-http_realip_module \ --with-pcre \ --with-http_gzip_static_module if [ $? -ne 0 ]; then echo "Configure failed!" exit 1 fi make -j4 if [ $? -ne 0 ]; then echo "Make failed!" exit 1 fi make install if [ $? -eq 0 ]; then echo "Nginx installed successfully!" /soft/nginx-1.12.2/sbin/nginx -v else echo "Install failed!" exit 1 fi
6.2 MySQL源码编译示例 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 #!/bin/bash yum install -y \ gcc gcc-c++ make cmake \ ncurses-devel \ openssl-devel \ bison \ libaio-devel groupadd mysql useradd -r -g mysql -s /bin/false mysql cd /soft/srcwget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.30.tar.gz tar xzf mysql-5.7.30.tar.gz cd mysql-5.7.30cmake . \ -DCMAKE_INSTALL_PREFIX=/soft/mysql-5.7.30 \ -DMYSQL_DATADIR=/soft/mysql-5.7.30/data \ -DSYSCONFDIR=/soft/mysql-5.7.30/etc \ -DWITH_MYISAM_STORAGE_ENGINE=1 \ -DWITH_INNOBASE_STORAGE_ENGINE=1 \ -DWITH_MEMORY_STORAGE_ENGINE=1 \ -DWITH_READLINE=1 \ -DMYSQL_UNIX_ADDR=/soft/mysql-5.7.30/mysql.sock \ -DMYSQL_TCP_PORT=3306 \ -DENABLED_LOCAL_INFILE=1 \ -DWITH_PARTITION_STORAGE_ENGINE=1 \ -DEXTRA_CHARSETS=all \ -DDEFAULT_CHARSET=utf8 \ -DDEFAULT_COLLATION=utf8_general_ci make -j4 make install cd /soft/mysql-5.7.30./bin/mysqld --initialize --user=mysql --datadir=/soft/mysql-5.7.30/data
6.3 PHP源码编译示例 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 #!/bin/bash yum install -y \ gcc gcc-c++ make \ libxml2-devel \ libcurl-devel \ libjpeg-devel \ libpng-devel \ freetype-devel \ openssl-devel \ zlib-devel cd /soft/srcwget http://php.net/distributions/php-7.2.0.tar.gz tar xzf php-7.2.0.tar.gz cd php-7.2.0./configure \ --prefix=/soft/php-7.2.0 \ --with-config-file-path=/soft/php-7.2.0/etc \ --enable-fpm \ --with-fpm-user=www \ --with-fpm-group=www \ --with-mysql \ --with-mysqli \ --with-pdo-mysql \ --with-openssl \ --with-curl \ --with-gd \ --with-jpeg-dir \ --with-png-dir \ --with-freetype-dir \ --enable-mbstring \ --enable-opcache make -j4 make install
7. 源码安装最佳实践 7.1 安装前准备
阅读文档 : 查看README、INSTALL等文档
检查依赖 : 确认所有依赖库已安装
规划路径 : 确定安装路径,建议使用/soft/目录
备份数据 : 如果是升级,先备份旧版本
7.2 编译优化 1 2 3 4 5 6 7 8 make -j$(nproc ) make VERBOSE=1 make && make install
7.3 安装后配置 1 2 3 4 5 6 7 8 9 echo 'export PATH=/soft/nginx-1.12.2/sbin:$PATH' >> /etc/profilesource /etc/profileln -s /soft/nginx-1.12.2 /soft/nginxvim /etc/systemd/system/nginx.service
7.4 卸载源码安装的软件 1 2 3 4 5 6 7 8 9 rm -rf /soft/nginx-1.12.2cd /soft/src/nginx-1.12.2make uninstall
8. 源码安装常见问题 1 2 3 4 5 ls -l configure./autogen.sh
8.2 问题2: 权限不足 1 2 3 4 5 chmod +x configurechmod -R 755 /soft/
8.3 问题3: 依赖库版本不匹配 1 2 3 4 5 6 pkg-config --modversion openssl pkg-config --modversion pcre ./configure --with-openssl=/usr/local/openssl
8.4 问题4: 编译时间过长 1 2 3 4 5 make -j$(nproc ) make -j4
9. 源码安装与RPM安装对比 9.1 对比说明
特性
源码安装
RPM安装
安装速度
慢(需要编译)
快(直接安装)
可定制性
高
低
依赖处理
手动
自动
版本控制
灵活
受仓库限制
卸载
手动删除
rpm -e
更新
重新编译
yum update
9.2 选择建议 使用源码安装的场景 :
需要特定功能模块
需要最新版本
需要自定义编译选项
生产环境需要稳定版本
使用RPM安装的场景 :
10. 命令总结 10.1 源码安装基本命令
步骤
命令
说明
解压
tar xzf file.tar.gz
解压源码包
配置
./configure --prefix=/path
配置编译选项
编译
make
编译源码
安装
make install
安装软件
卸载
make uninstall
卸载软件(如果支持)
选项
说明
示例
--prefix
安装路径
--prefix=/soft/nginx
--enable-xxx
启用功能
--enable-ssl
--disable-xxx
禁用功能
--disable-debug
--with-xxx
关联库
--with-pcre
--without-xxx
不使用库
--without-http_rewrite_module
10.3 常用依赖库
库名
开发包
用途
PCRE
pcre-devel
正则表达式
OpenSSL
openssl-devel
SSL/TLS支持
zlib
zlib-devel
压缩支持
libxml2
libxml2-devel
XML解析
curl
libcurl-devel
HTTP客户端