程序包管理之源代碼編譯安裝及rpm源碼包安裝

一、程序源碼編譯安裝步驟

二、程序路徑管理

三、簡單源碼編譯安裝示例

四、rpm源碼包安裝

 

一、程序源碼編譯安裝步驟

---------------------------------------

    1.cd SOURCE_CODE    #cd到程序源碼文件夾

---------------------------------------

    2../configure    #檢查編譯環境,對程序定製編譯配置,編譯前讀一讀README,INSTALL

                     #常用配置參數:--prefix=安裝路徑,--sysconfdir=配置文件路徑,配置幫助--help

                     #./configure結合Makefile.in生成makefile,Linux中的automake程序幫助生成Makefile.in文件

                     #Linux中的autoconf程序幫助生成configure文件

---------------------------------------

    3.make    #編譯程序,生成安裝文件

              #執行make命令,則結合makefile完成編譯安裝文件

---------------------------------------   

    4.make install   #安裝

 

二、程序路徑管理

 

實際安裝程序時,二進制可執行文件、庫文件、頭文件、man手冊可能都不在系統默認環境變量的搜索路徑中(配置文件程序會自動搜索匹配),此時需要手動將這些文件的路徑添加進去

1、修改PATH

    1.1、臨時性全局:bash下直接運行命令export PATH=$PATH:/usr/local/apache/bin

    1.2、永久全局修改:編輯/etc/profile,加入export PATH=$PATH:/usr/local/apache/bin;或者新建/etc/profile.d/apache.sh,加入export PATH=$PATH:/usr/local/apache/bin

    1.3、針對用戶:編輯~./bashrc或~./bash_profile,添加export PATH=$PATH:/usr/local/apache/bin

 

2、庫文件搜索路徑:編輯/etc/ld.so.conf或新建/etc/ld.so.conf.d/*.conf,新加路徑後,使用ldconfig -v重新搜索當前系統上所有庫文件搜索路徑下的庫文件,並生成緩存/etc/ld.so.cache,ldd /bin/ls:查看程序運行依賴的庫及所在位置,例如

[root@TESTHOST ~]# vim /etc/ld.so.conf.d/apache.conf    #爲apache單獨建立lib路徑文件
/usr/local/apache/lib
[root@TESTHOST ~]# ldconfig -v    #重建lib緩存

 

3、頭文件:使用鏈接ln -s,例如

[root@TESTHOST ~]# ln -sv /usr/local/apache/include /usr/include/httpd    #系統頭文件目錄爲/usr/include/下,在其中創建一個目錄鏈接即可

 

4、man手冊路徑:編輯/etc/man.config文件中的“MANPATH=”,例如

[root@TESTHOST ~]# vim /etc/man.config
MANPATH /usr/local/apache/share/man    #將程序man路徑加入
MANPATH	/usr/man
MANPATH	/usr/share/man
MANPATH	/usr/local/man
MANPATH	/usr/local/share/man
MANPATH	/usr/X11R6/man


三、簡單源碼編譯安裝示例

 

  編譯安裝axel

[root@TESTHOST ~]# tar xf axel-2.4.tar.bz2    #解包
[root@TESTHOST ~]# ls
RPM-GPG-KEY-CentOS-6    RPM-GPG-KEY-CentOS-6.2  axel-2.4          install.log
RPM-GPG-KEY-CentOS-6.1  anaconda-ks.cfg         axel-2.4.tar.bz2  install.log.syslog
[root@TESTHOST ~]# cd axel-2.4    #cd到源碼文件夾中
[root@TESTHOST axel-2.4]# ls
API      CREDITS   axel.1  axel_zh_CN.1    conf.h     conn.h  ftp.h   http.h  search.c  tcp.h
CHANGES  Makefile  axel.c  axelrc.example  configure  de.po   gui     nl.po   search.h  text.c
COPYING  README    axel.h  conf.c          conn.c     ftp.c   http.c  ru.po   tcp.c     zh_CN.po
[root@TESTHOST axel-2.4]# less README    #./configure前查看其README,或INSTALL文件,獲取相關信息 
[root@TESTHOST axel-2.4]# ./configure --help    #查看./configure支持的選項
Axel configure

Usage: ./configure [OPTIONS]

Option		Description				Default

--prefix=...	Directories to put files in		/usr/local
--bindir=...						$prefix/bin
--etcdir=...						$prefix/etc
--mandir=...						$sharedir/man
--locale=...						$sharedir/locale

--i18n=0/1	Disable/enable internationalization	1
--debug=0/1	Disable/enable debugging		0
--strip=0/1	Disable/enable binary stripping		1
[root@TESTHOST axel-2.4]# ./configure --prefix=/usr/local/axel    #檢查編譯環境,定製程序編譯相關配置,這裏僅定義了安裝目錄
The strip option is enabled. This should not be a problem usually, but on some
systems it breaks stuff.

Configuration done:
  Internationalization enabled.
  Debugging disabled.
  Binary stripping enabled.
[root@TESTHOST axel-2.4]# make    #使用make編譯程序
gcc -c axel.c -o axel.o -Wall -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -Os 
gcc -c conf.c -o conf.o -Wall -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -Os 
gcc -c conn.c -o conn.o -Wall -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -Os 
gcc -c ftp.c -o ftp.o -Wall -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -Os 
gcc -c http.c -o http.o -Wall -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -Os 
gcc -c search.c -o search.o -Wall -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -Os 
gcc -c tcp.c -o tcp.o -Wall -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -Os 
tcp.c: In function 'get_if_ip':
tcp.c:98: warning: dereferencing pointer 'x' does break strict-aliasing rules
tcp.c:97: note: initialized from here
gcc -c text.c -o text.o -Wall -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -Os 
gcc *.o -o axel  -lpthread
strip axel
msgfmt -vo nl.mo nl.po
40 translated messages, 6 fuzzy translations, 4 untranslated messages.
msgfmt -vo de.mo de.po
46 translated messages, 4 fuzzy translations.
msgfmt -vo ru.mo ru.po
46 translated messages, 2 fuzzy translations, 2 untranslated messages.
msgfmt -vo zh_CN.mo zh_CN.po
42 translated messages, 6 fuzzy translations, 2 untranslated messages.
[root@TESTHOST axel-2.4]# make install    #安裝
mkdir -p /usr/local/axel/bin/
cp axel /usr/local/axel/bin/axel
mkdir -p /usr/local/axel/etc/
cp axelrc.example /usr/local/axel/etc/axelrc
mkdir -p /usr/local/axel/share/man/man1/
cp axel.1 /usr/local/axel/share/man/man1/axel.1
mkdir -p /usr/local/axel/share/man/zh_CN/man1/
cp axel_zh_CN.1 /usr/local/axel/share/man/zh_CN/man1/axel.1
Installing locale files...
[root@TESTHOST axel-2.4]# vim /etc/profile.d/axel.sh    #新建文件
export PATH=$PATH:/usr/local/axel/bin    #將程序路徑加入PATH
[root@TESTHOST axel-2.4]# . /etc/profile.d/axel.sh    #source後即刻生效
[root@TESTHOST ~]# man axel    #因爲man手冊路徑不在系統默認目錄中,所以提示未找到    
Cannot open the message catalog "man" for locale "zh_CN.UTF-8"
(NLSPATH="/usr/share/locale/%l/LC_MESSAGES/%N")

No manual entry for axel
[root@TESTHOST ~]# man -M /usr/local/axel/share/man axel    #手動指向man手冊絕對路徑
[root@TESTHOST ~]# vim /etc/man.config    #將axel的man手冊路徑加入系統默認搜索路徑
MANPATH /usr/local/axel/share/man

 

四、rpm源碼包安裝

  

  安裝nginx的src.rpm包

  步驟:1、安裝rpm源碼包----->生成rpmbuild文件夾

        2、通過nginx.spec,編譯rpm包,命令:rpmbuild -ba nginx.spec

        3、安裝生成的rpm程序包

[root@TESTHOST ~]# ls    #src.rpm包
anaconda-ks.cfg  axel-2.4  axel-2.4.tar.bz2  Geo  install.log  install.log.syslog  nginx-1.0.15-5.el6.src.rpm  RPM-GPG-KEY-CentOS-6  RPM-GPG-KEY-CentOS-6.1  RPM-GPG-KEY-CentOS-6.2
[root@TESTHOST ~]# rpm -ivh nginx-1.0.15-5.el6.src.rpm    #安裝後並不是安裝了程序,而相當於解壓
warning: nginx-1.0.15-5.el6.src.rpm: Header V3 RSA/SHA256 Signature, key ID 0608b895: NOKEY
   1:nginx                  ########################################### [100%]
[root@TESTHOST ~]# ls    #安裝完後文件存放在rpmbuild文件夾中
anaconda-ks.cfg  axel-2.4  axel-2.4.tar.bz2  Geo  install.log  install.log.syslog  nginx-1.0.15-5.el6.src.rpm  rpmbuild  RPM-GPG-KEY-CentOS-6  RPM-GPG-KEY-CentOS-6.1  RPM-GPG-KEY-CentOS-6.2
[root@TESTHOST ~]# cd rpmbuild/
[root@TESTHOST rpmbuild]# ls    #rpmbuild下有兩個文件夾
SOURCES  SPECS
[root@TESTHOST rpmbuild]# ls SOURCES/    #文件夾下有程序的源碼,配置文件、補丁等其他信息文件
404.html  50x.html  default.conf  index.html  nginx-1.0.15.tar.gz  nginx-auto-cc-gcc.patch  nginx.conf  nginx.init  nginx-logo.png  nginx.logrotate  nginx.sysconfig  poweredby.png  ssl.conf  virtual.conf
[root@TESTHOST rpmbuild]# cd SPECS/
[root@TESTHOST SPECS]# ls    #SPECS文件夾下存放的是編譯配置文件,使用它能結合SOURCES文件夾中的文件生成rpm安裝包
nginx.spec
[root@TESTHOST SPECS]# rpmbuild -bs^C nginx.spec    #-bs只編譯源碼rpm包
[root@TESTHOST SPECS]# rpmbuild -bb^C nginx.spec    #-bb只編譯二進制rpm包
[root@TESTHOST SPECS]# rpmbuild -ba nginx.spec    #同時編譯二進制和源碼rpm包,這裏我們用-ba編譯程序
------------------------------
······編譯過程省略
------------------------------
[root@TESTHOST SPECS]# ls /root/rpmbuild/    #編譯完,rpm文件夾下多出了幾個文件夾
BUILD  BUILDROOT  RPMS  SOURCES  SPECS  SRPMS
[root@TESTHOST SPECS]# ls /root/rpmbuild/RPMS/
x86_64
[root@TESTHOST SPECS]# ls /root/rpmbuild/RPMS/x86_64/    #此文件夾下爲編譯好的rpm包
nginx-1.0.15-5.el6.x86_64.rpm  nginx-debuginfo-1.0.15-5.el6.x86_64.rpm
[root@TESTHOST SPECS]# ls /root/rpmbuild/SRPMS/    #此文件夾爲此次編譯同時生成的源碼rpm包
nginx-1.0.15-5.el6.src.rpm
[root@TESTHOST SPECS]# rpm -ivh /root/rpmbuild/RPMS/x86_64/nginx-1.0.15-5.el6.x86_64.rpm    #安裝程序
Preparing...                ########################################### [100%]
   1:nginx                  ########################################### [100%]
[root@TESTHOST SPECS]# /etc/init.d/iptables stop    #爲了快速測試,暫時關閉防火牆
iptables: Setting chains to policy ACCEPT: filter          [  OK  ]
iptables: Flushing firewall rules:                         [  OK  ]
iptables: Unloading modules:                               [  OK  ]

 

在瀏覽器中輸入主機地址,出現下圖提示,成功!!!

QQ截圖20150423173705

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