linux源码安装软件

今天我们学习一下Linux下源码安装软件。

在linux下安装源码是最常用的,使用源码包除了可以自定义修改源代码外,还可以定制相关的功能,因为源码包在编译时可以附加额外的选项。
源码包的编译用到了linux里面的编译器 ,linux上的C语言编译器成为gcc,如果没有gcc就无法编译安装。

编译安装通常三步骤。
(1)**./configure ** 。这一步可以定制功能,加上相应选项即可,具体选项通过 ./configure --help来查看。这一步自动检测linux系统相关的套件是否有编译源码包时所需要的库。如果缺少就不能编译,同通过后会生成Makefile文件。
(2)make 使用这个命令,会根据Makefile文件总预设的参数进行编译
(3)make install 。这一步时安装步骤,用于创建相关软件的存放目录和配置文件

举例安装 httpd

第一步下载源码包
一定要去官方网站下载,其他地方的源码包可能被修改过。
通常把安装文件放在 /usr/local/src/目录下。
我们先进入目录
> # cd /usr/local/src/
# wget https://mirrors.cnnic.cn/apache/httpd/httpd-2.4.41.tar.gz
如果此镜像失效,请到官网获取 最新现在地址。

第二步骤、解压源码

	>	[root@admin src]# tar xzvf httpd-2.4.41.tar.gz 
	[root@admin src]# ll
	>总用量 9056
	drwxr-sr-x 11 root dip     4096 8月   9 21:36 httpd-2.4.41
	-rw-r--r--  1 root root 9267917 8月  13 07:37 httpd-2.4.41.tar.gz

第三步配置相关选项并生成Makefile (./configure )

[root@admin httpd-2.4.41]# ./configure --help | less

 >Installation directories:
--prefix=PREFIX         install architecture-independent 				files in PREFIX
                      [/usr/local/apache2]

–exec-prefix=EPREFIX install architecture-dependent files in EPREFIX
[PREFIX]

常用配置选项有 --prefix=PREFIC,它的意思时定义软件包安装路径。
我们把Apache 安装在/usr/local/apache2

[root@admin httpd-2.4.41]# ./configure --prefix=/usr/local/apache2
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.

呦,居然报错了。为什么呢。
是因为没有安装gcc编译器。需要先安装一下。

[root@admin httpd-2.4.41]# yum install gcc*
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
file:///media/cdrom/repodata/repomd.xml: [Errno 14] curl#37 - “Couldn’t open file /media/cdrom/repodata/repomd.xml”

然后重新编译安装

[root@admin httpd-2.4.41]# ./configure –

查看操作是否完成。

[root@admin httpd-2.4.41]# echo $?
0

返回为0说说明执行成功,否则不成功。此时就生成了Makefile了。

(解释一下echo$? 原理。当一个进程执行完毕时,该进程会调用一个名为 _exit 的例程来通知内核它已经做好“消亡”的准备了。该进程会提供一个退出码(一个整数)表明它准备退出的原因。按照惯例,0用来表示正常的或者说“成功”的终止。也就是说我们在执行 echo $? 时反回的值就是进程的退出码。而且,这个退出码是由刚刚执行完的进程提供给系统内核的。)

查看是否生成makefile

[root@admin httpd-2.4.41]# ls -al | grep Ma
-rw-r–r-- 1 root dip 47603 7月 9 16:40 CMakeLists.txt
-rw-r–r-- 1 root dip 11381 11月 28 15:59 Makefile
-rw-r–r-- 1 root dip 11166 8月 8 2018 Makefile.in
-rw-r–r-- 1 root dip 53749 3月 26 2019 Makefile.win

第四步进行编译

[root@admin httpd-2.4.41]#make
[root@admin httpd-2.4.41]# echo $?
0
[root@admin httpd-2.4.41]#

第五步 安装

[root@admin httpd-2.4.41]# make install
[root@admin httpd-2.4.41]# echo $?
0

安装完成。

第六步启动
启动
/usr/local/apache2/bin/apachectl start

在这里插入图片描述

测试可以打开首页。

停止apache
/usr/local/apache2/bin/apachectl stop

第七步添加服务

将apache添加到系统服务,用service来控制apache的停止和启动。

1、以apachectl脚本为模板生成Apache服务控制脚本

grep -v “#” /usr/local/apache2/bin/apachectl > /etc/init.d/apache

2、使用vi编辑/etc/init.d/apache,在文件开头加入下面的行,使之支持chkconfig命令

#!/bin/sh
#chkconfig: 2345 85 15
#description: Apache is a World Wide Web server.

3、执行下面的命令增加apache服务控制脚本的执行权限

chmod +x /etc/init.d/apache

4、执行下面的命令将apache加入到系统服务并打开随系统开机启动

chkconfig --add apache

chkconfig apache on

5、执行下面命令检查apache服务是否已经生效

chkconfig --list apache

命令输出如下结果:

apache 0:关 1:关 2:开 3:开 4:开 5:开 6:关

表明apache服务已经生效,在2、3、4、5运行级别随系统启动而自动启动。

以后可以使用service(在rhel7中使用systemctl)命令控制Apache的启动和停止

5、启动和停止apache服务

systemctl start apache(rhel7以下使用:service apache start)

systemctl stop apache(rhel7以下使用:service apache stop)

6、使用如下命令关闭apache服务开机自动启动

chkconfig apache off

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章