软件管理 - 源码安装包管理

1. 源码包基本概述

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

1.1 源码包编译原理

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

1.2 使用源码包的好处

  1. 自定义修改源代码: 可以根据需求修改源代码
  2. 定制需要的相关功能: 可以启用或禁用特定功能模块
  3. 新版软件优先更新源码: 官方通常先发布源码包

1.3 源码包与二进制包对比

特性 源码包 二进制包(RPM)
安装方式 需要编译 直接安装
安装时间 较长 较短
可定制性
依赖处理 手动解决 自动解决
适用场景 需要定制功能 快速部署

2. 源码包如何获取

2.1 获取途径

  1. 官方网站: 可以获得最新的软件包

  2. GitHub: 开源项目源码

  3. SourceForge: 开源软件仓库

2.2 下载源码包

1
2
3
4
5
6
7
8
# 使用wget下载
wget http://nginx.org/download/nginx-1.12.2.tar.gz

# 使用curl下载
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 源码安装三步曲(常见)

源码安装通常分为三个步骤:

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

功能:

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

第二步: make

功能:

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

第三步: make install

功能:

  1. 执行make install
  2. 按Makefile定义好的路径拷贝至安装目录中

注意: 上面介绍的源码三部曲不能百分百通用于所有源码包, 也就是说源码包的安装并非存在标准安装步骤。

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

3.3 源码安装基本流程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 1. 解压源码包
tar xzf software.tar.gz

# 2. 进入源码目录
cd software/

# 3. 查看帮助文档
cat README
cat INSTALL

# 4. 配置编译选项
./configure --prefix=/path/to/install

# 5. 编译
make

# 6. 安装
make install

4. 源码包编译实例

下面通过编译Nginx来深入理解源码包安装

4.1 基础环境准备

1
2
# 安装编译工具和依赖
[root@node1 ~]# yum install -y gcc make wget

4.2 下载源码包

1
2
3
4
# 源码包一定要上官方站点下载,其他站点不安全
[root@node1 ~]# mkdir -p /soft/src
[root@node1 src]# cd /soft/src
[root@node1 src]# wget http://nginx.org/download/nginx-1.12.2.tar.gz

4.3 解压源码包

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

4.4 配置相关的选项

1
2
3
4
5
6
7
8
9
10
# 查看配置选项帮助
[root@node1 nginx-1.12.2]# ./configure --help|head
--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]# ./configure --prefix=/soft/nginx-1.12.2

# 完整配置示例
[root@node1 nginx-1.12.2]# ./configure \
--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
# 验证这一步命令是否成功, 非0都不算成功
[root@node1 nginx-1.12.2]# echo $?
0

返回值说明:

  • 0: 配置成功
  • 0: 配置失败,需要查看错误信息

4.7 编译并安装

1
2
3
4
5
6
7
8
9
10
# 编译(可以使用-j指定CPU核心数加速编译)
[root@node1 nginx-1.12.2]# make
# 或使用多核编译
[root@node1 nginx-1.12.2]# make -j4

# 安装
[root@node1 nginx-1.12.2]# make install

# 验证安装是否成功
[root@node1 nginx-1.12.2]# echo $?

4.8 验证安装

1
2
3
4
5
6
7
8
# 检查安装目录
ls -l /soft/nginx-1.12.2/

# 测试nginx配置
/soft/nginx-1.12.2/sbin/nginx -t

# 启动nginx
/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
# 安装gcc编译器和相关工具
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
# 安装PCRE开发库
yum install -y pcre-devel

# 或使用源码编译时指定PCRE路径
./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
# 安装zlib开发库
yum -y install zlib-devel

# 或禁用gzip模块
./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
# 安装OpenSSL开发库
yum -y install openssl-devel

# 或禁用SSL模块
./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
# Nginx源码编译安装脚本

# 1. 安装编译环境
yum install -y gcc gcc-c++ make wget pcre-devel openssl-devel zlib-devel

# 2. 创建源码目录
mkdir -p /soft/src
cd /soft/src

# 3. 下载Nginx源码
wget http://nginx.org/download/nginx-1.12.2.tar.gz

# 4. 解压
tar xzf nginx-1.12.2.tar.gz
cd nginx-1.12.2

# 5. 配置编译选项
./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

# 6. 检查配置结果
if [ $? -ne 0 ]; then
echo "Configure failed!"
exit 1
fi

# 7. 编译(使用4核)
make -j4

# 8. 检查编译结果
if [ $? -ne 0 ]; then
echo "Make failed!"
exit 1
fi

# 9. 安装
make install

# 10. 验证安装
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
# MySQL源码编译安装脚本

# 1. 安装编译环境和依赖
yum install -y \
gcc gcc-c++ make cmake \
ncurses-devel \
openssl-devel \
bison \
libaio-devel

# 2. 创建MySQL用户
groupadd mysql
useradd -r -g mysql -s /bin/false mysql

# 3. 下载源码
cd /soft/src
wget 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.30

# 4. 配置编译选项
cmake . \
-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

# 5. 编译
make -j4

# 6. 安装
make install

# 7. 初始化数据库
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
# PHP源码编译安装脚本

# 1. 安装依赖
yum install -y \
gcc gcc-c++ make \
libxml2-devel \
libcurl-devel \
libjpeg-devel \
libpng-devel \
freetype-devel \
openssl-devel \
zlib-devel

# 2. 下载PHP源码
cd /soft/src
wget http://php.net/distributions/php-7.2.0.tar.gz
tar xzf php-7.2.0.tar.gz
cd php-7.2.0

# 3. 配置编译选项
./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

# 4. 编译
make -j4

# 5. 安装
make install

7. 源码安装最佳实践

7.1 安装前准备

  1. 阅读文档: 查看README、INSTALL等文档
  2. 检查依赖: 确认所有依赖库已安装
  3. 规划路径: 确定安装路径,建议使用/soft/目录
  4. 备份数据: 如果是升级,先备份旧版本

7.2 编译优化

1
2
3
4
5
6
7
8
# 1. 使用多核编译加速
make -j$(nproc)

# 2. 查看编译过程
make VERBOSE=1

# 3. 只编译不安装(测试)
make && make install

7.3 安装后配置

1
2
3
4
5
6
7
8
9
# 1. 添加到PATH环境变量
echo 'export PATH=/soft/nginx-1.12.2/sbin:$PATH' >> /etc/profile
source /etc/profile

# 2. 创建软链接(方便管理)
ln -s /soft/nginx-1.12.2 /soft/nginx

# 3. 创建systemd服务文件(可选)
vim /etc/systemd/system/nginx.service

7.4 卸载源码安装的软件

1
2
3
4
5
6
7
8
9
# 方法1: 直接删除安装目录
rm -rf /soft/nginx-1.12.2

# 方法2: 如果有make uninstall
cd /soft/src/nginx-1.12.2
make uninstall

# 方法3: 记录安装文件,手动删除
# 安装时记录: make install > install.log

8. 源码安装常见问题

8.1 问题1: 找不到configure脚本

1
2
3
4
5
# 检查是否有configure文件
ls -l configure

# 如果没有,可能需要先运行autogen.sh
./autogen.sh

8.2 问题2: 权限不足

1
2
3
4
5
# 确保有执行权限
chmod +x configure

# 确保安装目录有写权限
chmod -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 卸载软件(如果支持)

10.2 常用configure选项

选项 说明 示例
--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客户端