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

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