一、源代码编译概述:
1.使用源代码安装软件的优点
获得最新的软件版本,及时修复bug
根据用户需要,灵活定制软件功能
2.应用场合举例
安装较新版本的应用程序时
自由软件的最新版本大都以源码的形式最先发布
当前安装的程序无法满足需要时
编译安装可由用户自行修改、定制功能
需要为应用程序添加新的功能时
用户可以重新配置、自由修改源代码,加入新的功能
3.Tarball 封包
.tar.gz 和 .tar.bz2 格式居多
4.完整性校验
使用md5sum校验工具
计算MD5校验和,并与官方提供的值相比较,判断是否一致
[root@localhost ~]# md5sum httpd-2.4.25.tar.gz
24fb8b9e36cf131d78caae864fea0f6a httpd-2.4.25.tar.gz
5.确认源代码编译环境需安装支持 C/C++程序语言的编译器,如:
[root@localhost ~]# gcc --version
gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-39)
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
[root@localhost ~]# make --version
GNU Make 3.82
Built for x86_64-redhat-linux-gnu
6.编译安装的基本过程
步骤1.解包
习惯上将软件包释放到 /usr/src/ 目录(-C指定目录)
解包后的源代码文件位置:/usr/src/软件名-版本号/
[root@localhost ~]# tar -zxvf httpd-2.4.25.tar.gz -C /usr/src/
步骤2.配置
使用源码目录中的 configure 脚本
执行“./configure –help” 可以查看帮助
典型的配置选项:
–prefix=软件安装目录
[root@localhost httpd-2.4.25]# ./configure --prefix=/usr/local/apache
步骤3.编译(-j指定逻辑CPU个数,加快编译速度。默认只有1个CPU处理)
执行make命令
[root@localhost httpd-2.4.25]# make -j 4
步骤4.安装
执行make install 命令(在命令前面添加time,可以查看安装时间,可选项)
[root@localhost httpd-2.4.25]# time make install
二、案例(apache编译安装)
1、下载软件包(http://archive.apache.org/dist/httpd/)
[root@localhost ~]# ifconfig ens32
ens32: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.14.13 netmask 255.255.255.0 broadcast 192.168.14.255
[root@localhost ~]# wget http://archive.apache.org/dist/httpd/httpd-2.4.25.tar.gz
2、完整性检验,因为这里下载的是gz包,所以打开对应的MD5
查看下载到本地的MD5值
[root@localhost ~]# md5sum httpd-2.4.25.tar.gz
24fb8b9e36cf131d78caae864fea0f6a httpd-2.4.25.tar.gz
3、解压到指定目录
[root@localhost ~]# tar -zxvf httpd-2.4.25.tar.gz -C /usr/src/
4、切换到解压目录
[root@localhost ~]# cd /usr/src/httpd-2.4.25/
5、执行./configure指定安装目录,选择安装模块并且排错,安装所依赖的包。
1)源码编译使用gcc,所以先安装gcc
[root@localhost httpd-2.4.25]# yum install -y gcc
2)./configure –prefix=安装目录自定义,可以不存在,会自动创建。后面可选项根据需求更改。
第一次编译:提示APR
[root@localhost httpd-2.4.25]# ./configure --prefix=/apps/httpd24 --sysconfdir=/etc/httpd --enable-ssl --enable-so
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-unknown-linux-gnu
checking host system type... x86_64-unknown-linux-gnu
checking target system type... x86_64-unknown-linux-gnu
configure:
configure: Configuring Apache Portable Runtime library...
configure:
checking for APR... no
configure: error: APR not found. Please read the documentation.
查看yum包里面的有关apr的,一般编译缺少的包,都是提示名字加上devel开发包,比如:apr-devel
[root@localhost httpd-2.4.25]# yum search apr
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.163.com
* extras: mirrors.163.com
* updates: mirrors.163.com
===================================================================================== N/S matched: apr ======================================================================================
apr-devel.i686 : APR library development kit
apr-devel.x86_64 : APR library development kit
apr-util-devel.i686 : APR utility library development kit
apr-util-devel.x86_64 : APR utility library development kit
apr-util-ldap.x86_64 : APR utility library LDAP support
apr-util-mysql.x86_64 : APR utility library MySQL DBD driver
apr-util-nss.x86_64 : APR utility library NSS crytpo support
apr-util-odbc.x86_64 : APR utility library ODBC DBD driver
apr-util-openssl.x86_64 : APR utility library OpenSSL crytpo support
apr-util-pgsql.x86_64 : APR utility library PostgreSQL DBD driver
apr-util-sqlite.x86_64 : APR utility library SQLite DBD driver
pcp-pmda-haproxy.x86_64 : Performance Co-Pilot (PCP) metrics for HAProxy
apr.i686 : Apache Portable Runtime library
apr.x86_64 : Apache Portable Runtime library
apr-util.i686 : Apache Portable Runtime Utility library
apr-util.x86_64 : Apache Portable Runtime Utility library
haproxy.x86_64 : TCP/HTTP proxy and load balancer for high availability environments
安装:apr-devel
[root@localhost httpd-2.4.25]# yum install -y apr-devel
3)第二次编译:提示APR-util
[root@localhost httpd-2.4.25]# ./configure --prefix=/apps/httpd24 --sysconfdir=/etc/httpd --enable-ssl --enable-so
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-unknown-linux-gnu
checking host system type... x86_64-unknown-linux-gnu
checking target system type... x86_64-unknown-linux-gnu
configure:
configure: Configuring Apache Portable Runtime library...
configure:
checking for APR... yes
setting CC to "gcc"
setting CPP to "gcc -E"
setting CFLAGS to " -pthread"
setting CPPFLAGS to " -DLINUX -D_REENTRANT -D_GNU_SOURCE"
setting LDFLAGS to " "
configure:
configure: Configuring Apache Portable Runtime Utility library...
configure:
checking for APR-util... no
configure: error: APR-util not found. Please read the documentation.
按照上面操作,先查看是否有关apr-util-devel包
[root@localhost httpd-2.4.25]# yum search apr-util
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.163.com
* extras: mirrors.163.com
* updates: mirrors.163.com
=================================================================================== N/S matched: apr-util ===================================================================================
apr-util.i686 : Apache Portable Runtime Utility library
apr-util.x86_64 : Apache Portable Runtime Utility library
apr-util-devel.i686 : APR utility library development kit
apr-util-devel.x86_64 : APR utility library development kit
apr-util-ldap.x86_64 : APR utility library LDAP support
apr-util-mysql.x86_64 : APR utility library MySQL DBD driver
apr-util-nss.x86_64 : APR utility library NSS crytpo support
apr-util-odbc.x86_64 : APR utility library ODBC DBD driver
apr-util-openssl.x86_64 : APR utility library OpenSSL crytpo support
apr-util-pgsql.x86_64 : APR utility library PostgreSQL DBD driver
apr-util-sqlite.x86_64 : APR utility library SQLite DBD driver
安装:apr-util-devel
[root@localhost httpd-2.4.25]# yum install -y apr-util-devel
4)第三次编译:提示PCRE
[root@localhost httpd-2.4.25]# ./configure --prefix=/apps/httpd24 --sysconfdir=/etc/httpd --enable-ssl --enable-so
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-unknown-linux-gnu
checking host system type... x86_64-unknown-linux-gnu
checking target system type... x86_64-unknown-linux-gnu
configure:
configure: Configuring Apache Portable Runtime library...
configure:
checking for APR... yes
setting CC to "gcc"
setting CPP to "gcc -E"
setting CFLAGS to " -pthread"
setting CPPFLAGS to " -DLINUX -D_REENTRANT -D_GNU_SOURCE"
setting LDFLAGS to " "
configure:
configure: Configuring Apache Portable Runtime Utility library...
configure:
checking for APR-util... yes
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed
checking how to run the C preprocessor... gcc -E
checking for gcc option to accept ISO C99... -std=gnu99
checking for pcre-config... false
configure: error: pcre-config for libpcre not found. PCRE is required and available from http://pcre.org/
查询
[root@localhost httpd-2.4.25]# yum search pcre
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.163.com
* extras: mirrors.163.com
* updates: mirrors.163.com
===================================================================================== N/S matched: pcre =====================================================================================
pcre-devel.i686 : Development files for pcre
pcre-devel.x86_64 : Development files for pcre
pcre-static.i686 : Static library for pcre
pcre-static.x86_64 : Static library for pcre
pcre-tools.x86_64 : Auxiliary utilities for pcre
pcre2-devel.i686 : Development files for pcre2
pcre2-devel.x86_64 : Development files for pcre2
pcre2-static.i686 : Static library for pcre2
pcre2-static.x86_64 : Static library for pcre2
pcre2-tools.x86_64 : Auxiliary utilities for pcre2
pcre2-utf16.i686 : UTF-16 variant of PCRE2
pcre2-utf16.x86_64 : UTF-16 variant of PCRE2
pcre2-utf32.i686 : UTF-32 variant of PCRE2
pcre2-utf32.x86_64 : UTF-32 variant of PCRE2
pcre.i686 : Perl-compatible regular expression library
pcre.x86_64 : Perl-compatible regular expression library
pcre2.i686 : Perl-compatible regular expression library
pcre2.x86_64 : Perl-compatible regular expression library
安装:pcre-devel
[root@localhost httpd-2.4.25]# yum install -y pcre-devel
5)第四次编译:提示OpenSSL version is too old
编译显示过长,只截取错误提示
checking for OpenSSL... checking for user-provided OpenSSL base directory... none
checking for OpenSSL version >= 0.9.8a... FAILED
configure: WARNING: OpenSSL version is too old
no
checking whether to enable mod_ssl... configure: error: mod_ssl has been requested but can not be built due to prerequisite failures
查询
[root@localhost httpd-2.4.25]# yum search openssl
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.163.com
* extras: mirrors.163.com
* updates: mirrors.163.com
=================================================================================== N/S matched: openssl ====================================================================================
apr-util-openssl.x86_64 : APR utility library OpenSSL crytpo support
openssl-devel.i686 : Files for development of applications which will use OpenSSL
openssl-devel.x86_64 : Files for development of applications which will use OpenSSL
openssl-perl.x86_64 : Perl scripts provided with OpenSSL
openssl-static.i686 : Libraries for static linking of applications which will use OpenSSL
openssl-static.x86_64 : Libraries for static linking of applications which will use OpenSSL
perl-Crypt-OpenSSL-Bignum.x86_64 : Perl interface to OpenSSL for Bignum
perl-Crypt-OpenSSL-RSA.x86_64 : Perl interface to OpenSSL for RSA
perl-Crypt-OpenSSL-Random.x86_64 : Perl interface to OpenSSL for Random
pyOpenSSL.x86_64 : Python wrapper module around the OpenSSL library
pyOpenSSL-doc.noarch : Documentation for pyOpenSSL
xmlsec1-openssl.i686 : OpenSSL crypto plugin for XML Security Library
xmlsec1-openssl.x86_64 : OpenSSL crypto plugin for XML Security Library
xmlsec1-openssl-devel.i686 : OpenSSL crypto plugin for XML Security Library
xmlsec1-openssl-devel.x86_64 : OpenSSL crypto plugin for XML Security Library
m2crypto.x86_64 : Support for using OpenSSL in python scripts
nss_compat_ossl.i686 : Source-level compatibility library for OpenSSL to NSS porting
nss_compat_ossl.x86_64 : Source-level compatibility library for OpenSSL to NSS porting
openssl.x86_64 : Utilities from the general purpose cryptography library with TLS implementation
openssl-libs.x86_64 : A general purpose cryptography library with TLS implementation
openssl-libs.i686 : A general purpose cryptography library with TLS implementation
openssl098e.i686 : A compatibility version of a general cryptography and TLS library
openssl098e.x86_64 : A compatibility version of a general cryptography and TLS library
perl-Crypt-SSLeay.x86_64 : Crypt::SSLeay - OpenSSL glue that provides LWP https support
perl-Net-SSLeay.x86_64 : Perl extension for using OpenSSL
qca-ossl.i686 : OpenSSL plugin for the Qt Cryptographic Architecture v2
qca-ossl.x86_64 : OpenSSL plugin for the Qt Cryptographic Architecture v2
安装:
[root@localhost httpd-2.4.25]# yum install -y openssl-devel
6)第五次编译终于成功(如果按照以上操作有问题,可以删除解压包,因为相关的依赖包已经安装完成。重新解压软件包编译即可)
config.status: executing default commands
configure: summary of build options:
Server Version: 2.4.25
Install prefix: /apps/httpd24
C compiler: gcc -std=gnu99
CFLAGS: -pthread
LDFLAGS:
LIBS:
CPPFLAGS: -DLINUX -D_REENTRANT -D_GNU_SOURCE
C preprocessor: gcc -E
6、由此可见,编译成功需要的软件包:gcc apr-devel arp-util-devel pcre-devel openssl-devel
7、make
[root@localhost httpd-2.4.25]# make -j 4
8、make install(可以查看到创建了相关目录)
[root@localhost httpd-2.4.25]# make install
...
...
Installing configuration files
mkdir /etc/httpd #配置目录
mkdir /etc/httpd/extra
mkdir /etc/httpd/original
mkdir /etc/httpd/original/extra
Installing HTML documents
mkdir /apps/httpd24/htdocs #页面目录
Installing error documents
mkdir /apps/httpd24/error
Installing icons
mkdir /apps/httpd24/icons
mkdir /apps/httpd24/logs
Installing CGIs
mkdir /apps/httpd24/cgi-bin
Installing header files
mkdir /apps/httpd24/include
Installing build system files
mkdir /apps/httpd24/build
Installing man pages and online manual
mkdir /apps/httpd24/man
mkdir /apps/httpd24/man/man1
mkdir /apps/httpd24/man/man8
mkdir /apps/httpd24/manual
make[1]: Leaving directory `/usr/src/httpd-2.4.25'
9、关闭防火墙和selinux
[root@localhost httpd-2.4.25]# systemctl stop firewalld
[root@localhost httpd-2.4.25]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
10、启动apache服务(如何不知道如何启动,可以查看)
[root@localhost httpd-2.4.25]# more INSTALL
APACHE INSTALLATION OVERVIEW
Quick Start - Unix
------------------
For complete installation documentation, see [ht]docs/manual/install.html or
http://httpd.apache.org/docs/2.4/install.html
$ ./configure --prefix=PREFIX
$ make
$ make install
$ PREFIX/bin/apachectl start #用户指定安装目录(apps/httpd24)/bin/apachectl start
[root@localhost httpd-2.4.25]# cd /apps/httpd24/bin/
[root@localhost 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
[root@localhost bin]# ss -ntlp
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:* users:(("sshd",pid=5066,fd=3))
LISTEN 0 100 127.0.0.1:25 *:* users:(("master",pid=5558,fd=13))
LISTEN 0 128 :::80 :::* users:(("httpd",pid=17249,fd=4),("httpd",pid=17248,fd=4),("httpd",pid=17247,fd=4),("httpd",pid=17246,fd=4))
LISTEN 0 128 :::22 :::* users:(("sshd",pid=5066,fd=4))
LISTEN 0 100 ::1:25 :::* users:(("master",pid=5558,fd=14))
11、在其他主机访问成功
[root@localhost ~]# curl http://192.168.14.13
<html><body><h1>It works!</h1></body></html>
三、脚本安装
1、最好先下载httpd-2.4.25.tar放到/root目录下(因为脚本就在/root目录下,安装包放到跟脚本同一个目录),这样执行脚本过程中不需要重新下载,节省时间
[root@localhost ~]# cat http.sh
#!/bin/bash
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config #关闭selinux
systemctl stop firewalld #关闭防火墙
systemctl disable firewalld #开机不启动
cpu=`cat /proc/cpuinfo| grep "processor"| wc -l` #获取主机逻辑CPU个数
prefix=/apps/httpd24 #httpd安装目录
sysconfdir=/etc/httpd #httpd配置文件目录
yum install -y gcc apr-devel apr-util-devel pcre-devel openssl-devel wget
ls | grep httpd-2.4.25.tar.gz
if [ $? -eq 0 ];then
echo "-----------------------------------------------------------------------"
echo "-----------------------------------------------------------------------"
echo "the httpd is exist" #提升安装包已经存在
echo "-----------------------------------------------------------------------"
echo "-----------------------------------------------------------------------"
sleep 30
tar -zxvf httpd-2.4.25.tar.gz -C /tmp/
cd /tmp/httpd-2.4.25/
./configure --prefix=$prefix --sysconfdir=$sysconfdir --enable-ssl --enable-so
make -j $cpu
make install
echo "-----------------------------------------------------------------------"
echo "-----------------------------------------------------------------------"
echo "the httpd is start" #启动服务提示
echo "-----------------------------------------------------------------------"
echo "-----------------------------------------------------------------------"
sleep 30
$prefix/bin/apachectl start
else
wget http://archive.apache.org/dist/httpd/httpd-2.4.25.tar.gz
tar -zxvf httpd-2.4.25.tar.gz -C /tmp/
cd /tmp/httpd-2.4.25/
./configure --prefix=$prefix --sysconfdir=$sysconfdir --enable-ssl --enable-so
make -j $cpu
make install
echo "-----------------------------------------------------------------------"
echo "-----------------------------------------------------------------------"
echo "the httpd is start" #启动服务提示
echo "-----------------------------------------------------------------------"
echo "-----------------------------------------------------------------------"
sleep 30
$prefix/bin/apachectl start
fi
四、远程服务器脚本安装httpd
(其他主机调用脚本服务器里面的脚本,直接执行安装)
服务器端(192.168.14.13),把脚本放到htdocs目录下
[root@http-server htdocs]# pwd
/apps/httpd24/htdocs
[root@http-server htdocs]# ll
total 8
-rwxr-xr-x. 1 root root 1610 Sep 24 04:18 http.sh
-rw-r--r--. 1 501 games 45 Jun 11 2007 index.html
客户端:
1、客户端IP
[root@localhost ~]# ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens32: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:08:01:5d brd ff:ff:ff:ff:ff:ff
inet 192.168.14.40/24 brd 192.168.14.255 scope global noprefixroute dynamic ens32
valid_lft 3117sec preferred_lft 3117sec
inet6 fe80::c241:11e3:176d:aca4/64 scope link noprefixroute
valid_lft forever preferred_lft forever
2、cur http://192.168.14.13/http.sh查看
[root@localhost ~]# curl http://192.168.14.13/http.sh
#!/bin/bash
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
systemctl stop firewalld
cpu=`cat /proc/cpuinfo| grep "processor"| wc -l`
prefix=/apps/httpd24
sysconfdir=/etc/httpd
yum install -y gcc apr-devel apr-util-devel pcre-devel openssl-devel wget
ls | grep httpd-2.4.25.tar.gz
if [ $? -eq 0 ];then
echo "-----------------------------------------------------------------------"
echo "-----------------------------------------------------------------------"
echo "the httpd is exist"
echo "-----------------------------------------------------------------------"
echo "-----------------------------------------------------------------------"
sleep 30
tar -zxvf httpd-2.4.25.tar.gz -C /tmp/
cd /tmp/httpd-2.4.25/
./configure --prefix=$prefix --sysconfdir=$sysconfdir --enable-ssl --enable-so
make -j $cpu
make install
echo "-----------------------------------------------------------------------"
echo "-----------------------------------------------------------------------"
echo "the httpd is start"
echo "-----------------------------------------------------------------------"
echo "-----------------------------------------------------------------------"
sleep 30
$prefix/bin/apachectl start
else
wget http://archive.apache.org/dist/httpd/httpd-2.4.25.tar.gz
tar -zxvf httpd-2.4.25.tar.gz -C /tmp/
cd /tmp/httpd-2.4.25/
./configure --prefix=$prefix --sysconfdir=$sysconfdir --enable-ssl --enable-so
make -j $cpu
make install
echo "-----------------------------------------------------------------------"
echo "-----------------------------------------------------------------------"
echo "the httpd is start"
echo "-----------------------------------------------------------------------"
echo "-----------------------------------------------------------------------"
sleep 30
$prefix/bin/apachectl start
fi
3、把查看的结果传递给bash执行
root@localhost ~]# curl http://192.168.14.13/http.sh | bash
4、查看结果
make[1]: Leaving directory `/tmp/httpd-2.4.25'
-----------------------------------------------------------------------
-----------------------------------------------------------------------
the httpd is 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
[root@localhost ~]# ss -ntlp
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:* users:(("sshd",pid=5192,fd=3))
LISTEN 0 100 127.0.0.1:25 *:* users:(("master",pid=5684,fd=13))
LISTEN 0 128 :::80 :::* users:(("httpd",pid=12034,fd=4),("httpd",pid=12033,fd=4),("httpd",pid=12032,fd=4),("httpd",pid=12031,fd=4))
LISTEN 0 128 :::22 :::* users:(("sshd",pid=5192,fd=4))
LISTEN 0 100 ::1:25 :::* users:(("master",pid=5684,fd=14))
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/95230.html