源码包部署
一、编译安装概述
(一)源码编译的优势
主要体现在:
获得软件的最新版,及时修复bug
软件功能可按需选择/定制(DIY手工打造)
有更多软件可以选择−源码包适用于各种平台
…
(二)准备编译环境
开源软件多使用C/C++语言开发
需要gcc、make等编译工具
仓库提供的“开发工具”软件组
[root@localhost ~]# rpm -q gcc
gcc-4.8.5-44.el7.x86_64
[root@localhost ~]# rpm -q make
make-3.82-24.el7.x86_64
[root@localhost ~]# gcc --version
gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44)
Copyright © 2015 Free Software Foundation, Inc.
本程序是自由软件;请参看源代码的版权声明。本软件没有任何担保;
包括没有适销性和某一专用目的下的适用性担保。
(三)编译安装“四步曲”
(下载源码包)
步骤1:tar 解包,释放源代码至指定目录
步骤2:./configure 配置,指定安装目录/功能等选项
步骤3:make 编译,生成可执行的二进制程序文件
步骤4:make install 安装,将编译好的文件复制到系统
(测试及应用/维护软件)
二、编辑过程分步详解
(一)示例软件介绍
NMAP扫描器,著名的开源软件项目之一
Network Mapper,网络勘查员
是一款强大的网络扫描器及安全审计工具
官方网站:https://nmap.org/
(二)下载及解包
从官网下载最新源码包,并释放源码文件
建议释放到/usr/src/ 目录下
解包后标准位置:/usr/src/软件名-版本号/
[root@localhost ~]# cd /root/桌面
[root@localhost 桌面]# tar -xf nmap-7.94.tar.bz2 -C /usr/src/
[root@localhost 桌面]# cd /usr/src/
[root@localhost src]# ls
debug kernels nmap-7.94
[root@localhost src]# cd nmap-7.94/
[root@localhost nmap-7.94]# ls
acinclude.m4 missing nselib README-WIN32
aclocal.m4 mswin32 nse_libssh2.cc scan_engine.cc
BSDmakefile nbase nse_libssh2.h scan_engine_connect.cc
……
(三)配置
使用configure 脚本进行配置
通过“./configure --help” 可以查看帮助
典型配置参数:–prefix=安装目录
[root@localhost nmap-7.94]# ./configure --prefix=/usr/local/nmap
checking whether NLS is requested... yes
checking build system type... x86_64-unknown-linux-gnu
checking host system type... x86_64-unknown-linux-gnu
checking for gcc... gcc
……
(四)编译
执行make 命令
根据Makefile配置将文本代码制作成二进制程序
准备好必要的程序配置、文档及手册
// Makefile 记录文件安装位置,功能……
[root@localhost nmap-7.94]# ls Makefile
Makefile
[root@localhost nmap-7.94]# ls /usr/local/nmap
ls: 无法访问/usr/local/nmap: 没有那个文件或目录
// 报错,缺少OpenSSL开发包
[root@localhost nmap-7.94]# make
g++ -MM -I./liblinear -I./liblua -I./libdnet-stripped/include -I./libz -I./libpcre -I./libpcap -I./nbase -I./nsock/include -DHAVE_CONFIG_H -DNMAP_PLATFORM=\"x86_64-unknown-linux-gnu\" -DNMAPDATADIR=\"/usr/local/nmap/share/nmap\" -D_FORTIFY_SOURCE=2 charpool.cc FingerPrintResults.cc FPEngine.cc FPModel.cc idle_scan.cc MACLookup.cc main.cc nmap.cc
……
In file included from nse_main.cc:19:0:
nse_openssl.h:8:24: 致命错误:openssl/bn.h:没有那个文件或目录
#include <openssl/bn.h>
^
编译中断。
make: *** [nse_main.o] 错误 1
// 安装OpenSSL开发包
[root@localhost nmap-7.94]# yum install openssl-devel
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
正在解决依赖关系
--> 正在检查事务
---> 软件包 openssl-devel.x86_64.1.1.0.2k-19.el7 将被 安装
……
// 编译成功
[root@localhost nmap-7.94]# make
g++ -MM -I./liblinear -I./liblua -I./libdnet-stripped/include -I./libz -I./libpcre -I./libpcap -I./nbase -I./nsock/include -DHAVE_CONFIG_H -DNMAP_PLATFORM=\"x86_64-unknown-linux-gnu\" -DNMAPDATADIR=\"/usr/local/nmap/share/nmap\" -D_FORTIFY_SOURCE=2 charpool.cc
……
make[1]: 离开目录“/usr/src/nmap-7.94/ncat”
[root@localhost nmap-7.94]# ls /usr/local/nmap
ls: 无法访问/usr/local/nmap: 没有那个文件或目录
(五)安装
执行make install 命令
将制作好的程序及相关文件部署到系统的安装目录
独立于源码,方便用户使用此软件
[root@localhost nmap-7.94]# make install
Compiling libnetutil
cd libnetutil && make
make[1]: 进入目录“/usr/src/nmap-7.94/libnetutil”
……
make[1]: 离开目录“/usr/src/nmap-7.94/nping”
NMAP SUCCESSFULLY INSTALLED
(六)验证安装结果
查看安装目录/了解程序的用法
利用帮助机制、官方文档
[root@localhost nmap-7.94]# /usr/local/nmap/bin/nmap localhost
Starting Nmap 7.94 ( https://nmap.org ) at 2023-07-14 09:51 CST
Warning: File ./nmap-services exists, but Nmap is using /usr/local/nmap/bin/../share/nmap/nmap-services for security and consistency reasons. set NMAPDIR=. to give priority to files in your local directory (may affect the other data files too).
mass_dns: warning: Unable to determine any DNS servers. Reverse DNS is disabled. Try using --system-dns or specify valid servers with --dns-servers
Nmap scan report for localhost (127.0.0.1)
Host is up (0.0000040s latency).
Other addresses for localhost (not scanned): ::1
Not shown: 996 closed tcp ports (reset)
PORT STATE SERVICE
22/tcp open ssh
25/tcp open smtp
111/tcp open rpcbind
631/tcp open ipp
Nmap done: 1 IP address (1 host up) scanned in 0.05 seconds
三、定制httpd网站服务器
(一)httpd网站服务软件
Apache HTTP Server
源于A Patchy Server,著名的开源Web服务器软件
官方站点:http://httpd.apache.org/
(二)编译安装httpd
下载httpd-2.2.26.tar.gz 源码包
依次执行解包、配置、编译、安装的过程
安装到/usr/local/httpd/
&& 连接两个步骤
[root@localhost ~]# cd /root/桌面
[root@localhost 桌面]# tar -xf httpd-2.4.57.tar.bz2 -C /usr/src
[root@localhost 桌面]# cd /usr/src/
[root@localhost src]# ls
debug httpd-2.4.57 kernels nmap-7.94
[root@localhost src]# cd httpd-2.4.57/
[root@localhost httpd-2.4.57]# ls
ABOUT_APACHE CHANGES httpd.mak Makefile.in ROADMAP
acinclude.m4 changes-entries httpd.spec Makefile.win server
Apache-apr2.dsw CMakeLists.txt include modules srclib
Apache.dsw config.layout INSTALL NOTICE support
apache_probes.d configure InstallBin.dsp NWGNUmakefile test
ap.d configure.in LAYOUT os VERSIONING
build docs libhttpd.dep README
BuildAll.dsp emacs-style libhttpd.dsp README.CHANGES
BuildBin.dsp httpd.dep libhttpd.mak README.cmake
buildconf httpd.dsp LICENSE README.platforms
// 报错,没有找到APR包
[root@localhost httpd-2.4.57]# ./configure --prefix=/usr/local/httpd
checking for chosen layout... Apache
checking for working mkdir -p... yes
checking for grep that handles long lines and -e... /usr/bin/grep
checking for egrep... /usr/bin/grep -E
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking target system type... x86_64-pc-linux-gnu
configure:
configure: Configuring Apache Portable Runtime library...
configure:
checking for APR... no
configure: error: APR not found. Please read the documentation.
报错,没有找到APR包;原因:未连接网络;解决办法:①联网,②自己安装需要的包
// 从官网下载apr、apr-util、pcre安装包
[root@localhost ~]# cd /root/桌面
// 解压安装包
[root@localhost 桌面]# tar -xf apr-1.7.4.tar.bz2 -C /usr/src
[root@localhost 桌面]# tar -xf apr-util-1.5.4.tar.bz2 -C /usr/src
[root@localhost 桌面]# tar -xf pcre-8.45.tar.bz2 -C /usr/src
// 安装apr
[root@localhost 桌面]# cd /usr/src/apr-1.7.4/
[root@localhost apr-1.7.4]# ./configure --prefix=/usr/local/apr
[root@localhost apr-1.7.4]# make && make install
// 安装apr-util
[root@localhost apr-1.7.4]# cd ../apr-util-1.5.4/
[root@localhost apr-util-1.5.4]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/bin/apr-1-config
[root@localhost apr-util-1.5.4]# yum -y install expat-devel
[root@localhost apr-util-1.5.4]# make && make install
// 安装pcre
[root@localhost apr-util-1.5.4]# cd ../pcre-8.45/
[root@localhost pcre-8.45]# ./configure --prefix=/usr/local/pcre --with-apr=/usr/local/apr/bin/apr-1-config
[root@localhost pcre-8.45]# make && make install
// 开始安装httpd
[root@localhost pcre-8.45]# cd ../httpd-2.4.57/
[root@localhost httpd-2.4.57]# ./configure --prefix=/usr/local/httpd --enable-rewrite --enable-so --with-apr=/usr/local/apr/bin/apr-1-config --with-apr-util=/usr/local/apr-util/ --with-pcre=/usr/local/pcre/bin/pcre-config
[root@localhost httpd-2.4.57]# make && make install
注意:如果报错如下,请修改apr-util版本,建议版本在1.5及一下
[root@localhost httpd-2.4.57]# make && make install
……
/usr/local/apr-util/lib/libaprutil-1.so: undefined reference to `XML_GetErrorCode'
/usr/local/apr-util/lib/libaprutil-1.so: undefined reference to `XML_SetEntityDeclHandler'
/usr/local/apr-util/lib/libaprutil-1.so: undefined reference to `XML_ParserCreate'
/usr/local/apr-util/lib/libaprutil-1.so: undefined reference to `XML_SetCharacterDataHandler'
/usr/local/apr-util/lib/libaprutil-1.so: undefined reference to `XML_ParserFree'
/usr/local/apr-util/lib/libaprutil-1.so: undefined reference to `XML_SetUserData'
/usr/local/apr-util/lib/libaprutil-1.so: undefined reference to `XML_StopParser'
/usr/local/apr-util/lib/libaprutil-1.so: undefined reference to `XML_Parse'
/usr/local/apr-util/lib/libaprutil-1.so: undefined reference to `XML_ErrorString'
/usr/local/apr-util/lib/libaprutil-1.so: undefined reference to `XML_SetElementHandler'
collect2: error: ld returned 1 exit status
make[2]: *** [htpasswd] Error 1
make[2]: Leaving directory `/usr/local/httpd-2.4.26/support'
make[1]: *** [all-recursive] Error 1
make[1]: Leaving directory `/usr/local/httpd-2.4.26/support'
make: *** [all-recursive] Error 1
(三)验证安装结果
基本的目录结构
bin/* 程序文件、htdocs/*.html 网页文件
conf/httpd.conf 服务配置文件
manual/*.html 官方手册页
[root@localhost ~]# cd /usr/local/httpd/
[root@localhost httpd]# ls -F /usr/local/httpd/
bin/ cgi-bin/ error/ icons/ logs/ manual/
build/ conf/ htdocs/ include/ man/ modules/
(四)Web服务测试
部署网页→启动服务→浏览网页
[root@localhost httpd-2.4.57]# cat /usr/local/httpd/htdocs/index.html
<html><body><h1>It works!</h1></body></html>
// 启动服务
[root@localhost httpd]# /usr/local/httpd/bin/apachectl start
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain. Set the 'ServerName' directive globally to suppress this message
httpd (pid 65360) already running
[root@localhost httpd]# firefox http://127.0.0.1
四、源码软件的卸载
(一)安装目录的选择
集中模式
/usr/local/软件名/{bin,lib,sbin,share,… …}
方便卸载,不方便使用
分散模式
/usr/local/{bin,lib,sbin,share,… …}
方便使用,不方便卸载
[root@localhost ~]# apachectl
bash: apachectl: 未找到命令...
// 定义PATH全局
[root@localhost ~]# ln /usr/local/httpd/bin/apachectl /usr/local/bin/
[root@localhost ~]# ls /usr/local/bin/apachectl
/usr/local/bin/apachectl
[root@localhost ~]# apachectl
[root@localhost ~]# firefox http://127.0.0.1
(二)源码软件的卸载
不要与RPM软件包混淆
与RPM机制无关,因此不受rpm命令管理
同一软件可以与RPM安装的并存,但使用时要避免冲突
卸载的处理
个别软件支持make uninstall 反安装(需源码目录)
个别软件提供类似uninstall.sh 的专用工具
大多数软件只能手动删除已安装的文档
// 移除httpd文件目录
[root@localhost ~]# rm -rf /usr/local/httpd