Linux下編譯安裝httpd

本章學習內容

        ---------介紹httpd

        ----------rpm和yum不同

        ----------編譯安裝優勢

        ----------編譯httpd




1、Apache和http的關係

    http是一款超文本傳輸協議,早期的Apache團隊利用此協議研發了一款web服務軟件,叫做http。後來通過bug的修復和功能的完善,在http的基礎上開發了另外一款軟件,稱爲A Patchy Server,簡稱Apache,與Apache組織正好重名。後來Apache團隊逐漸強大,最終成爲一個開源軟件基金會(ASF),http也不再是其唯一維護的一款軟件,再將這款服務軟件稱爲Apache顯然不合適,所以我們可以稱其爲httpd。不過有時候因爲歷史的原因,我們還是會將Apache作爲一款Web服務軟件來稱呼,而其We服務規範也成爲業界的標準。

下面的過程爲了更加嚴謹,我們使用httpd來描述這款軟件。




2、介紹rpm包和源碼安裝的區別

    rpm包是將源碼編譯好的軟件包,只要安裝既可以提供服務,而源碼包是最底層的代碼,如果要使用,還要經過編譯,顯然不如rpm包方便,但是倆者各有優勢,生產生活中,源碼編譯安裝居多,如果要是練習使用的話,rpm包安裝居多。




3、編譯安裝的優勢

    <1>自定義軟件功能
    <2>優化編譯參數,提高性能
    <3>解決不必要的軟件間依賴
    <4>方便清理與卸載




4、編譯安裝httpd-2.2.29

    <1>準備開發工具、開發環境(開發庫和頭文件)

[root@centos7/media/cdrom/Packages]#yum groupinstall "Development Tools"

    <2>準備源碼並解壓縮至某目錄

[root@centos7~]#tar -xf httpd-2.2.29.tar.bz2 
[root@centos7~]#cd httpd-2.2.29/
[root@centos7~/httpd-2.2.29]#du -sh
42M    # 只有42M

    安裝之前,可以查看README、INSTALL文檔或者通過./configure --help來了解安裝過程

    <3>執行.configure腳本,指定特性,檢查外部環境,並根據其特性生成特定的makefile文件

[root@centos7~/httpd-2.2.29]#./configure --prefix=/usr/local/httpd --sysconfdir=/etc/httpd
checking for killpg... yes
checking bstring.h usability... no
checking bstring.h presence... no
checking for bstring.h... no
checking for unistd.h... (cached) yes
checking for syslog... yes
checking sys/times.h usability... yes
checking sys/times.h presence... yes
checking for sys/times.h... yes
creating test/Makefile
...
[root@centos7~/httpd-2.2.29]#du -sh
44M    # 增大到44M

    <4>make項目工具編譯,將.c源碼根據makefile文件編譯成應用程序

[root@centos7~/httpd-2.2.29]#make
bmod_status.la modules/generators/libmod_autoindex.la modules/generators/libmod_asis.la modules/generators/libmod_cgi.la modules/mappers/libmod_negotiation.la modules/mappers/libmod_dir.la modules/mappers/libmod_actions.la modules/mappers/libmod_userdir.la modules/mappers/libmod_alias.la modules/mappers/libmod_so.la server/mpm/prefork/libprefork.la os/unix/libos.la -lm /root/httpd-2.2.29/srclib/pcre/libpcre.la /root/httpd-2.2.29/srclib/apr-util/libaprutil-1.la /root/httpd-2.2.29/srclib/apr-util/xml/expat/libexpat.la /root/httpd-2.2.29/srclib/apr/libapr-1.la -lrt -lcrypt -lpthread -ldl 
make[1]: Leaving directory `/root/httpd-2.2.29'
...
[root@centos7~/httpd-2.2.29]#du -sh
74M    # 增大到74M	

    <5>make install,創建目錄並複製文件(二進制程序、庫文件、幫助文檔、配置文件)

[root@centos7~/httpd-2.2.29]#make install
...
mkdir /usr/local/httpd/man
mkdir /usr/local/httpd/man/man1
mkdir /usr/local/httpd/man/man8
mkdir /usr/local/httpd/manual
make[1]: Leaving directory `/root/httpd-2.2.29'

    <6>啓動服務

[root@centos7 bin]# pwd
/usr/local/httpd/bin
[root@centos7 bin]#./apachectl start
# 查看端口
[root@centos7~]#netstat -tnlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 192.168.122.1:53        0.0.0.0:*               LISTEN      2021/dnsmasq        
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1577/sshd           
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      1575/cupsd          
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1974/master         
tcp6       0      0 :::80                   :::*                    LISTEN      74875/httpd         
tcp6       0      0 :::22                   :::*                    LISTEN      1577/sshd           
tcp6       0      0 ::1:631                 :::*                    LISTEN      1575/cupsd          
tcp6       0      0 ::1:25                  :::*                    LISTEN      1974/master

    <7>測試

# telnet測試服務是否可正常使用
[root@centos6 ~]# telnet 10.1.0.17
Trying 10.1.0.17...
telnet: connect to address 10.1.0.17: No route to host
# 清除防火牆規則
[root@centos7~]#iptables -F
# 連接
[root@centos6 ~]# links 10.1.0.17

wKiom1e9O-KSCkwwAABatwDguuI688.jpg

配置基本完成,但是安裝的httpd的二進制文件和man幫助並不在系統PATH變量中,進一步完善

    <8>二進制文件導入PATH環境變量中

       基於文件的方式實現

[root@centos7~]#vim httpd.sh
#!/bin/bash
PATH=$PATH:/usr/local/httpd/bin
# 重讀配置文件
[root@centos7~]#. /etc/profile.d/httpd.sh 
# 查看PATH環境變量
[root@centos7~]#echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/local/httpd2/bin:/root/bin:/usr/local/httpd/bin

    <9>導入幫助手冊

[root@centos7~]#vim /etc/man_db.conf
MANDATORY_MANPATH                       /usr/local/httpd/man
...
# 重讀配置文件
[root@centos7~]#. /etc/man_db.conf

最後一步,爲了提升函數庫的讀取性能和方便開發人員編程時調用的頭文件,將其導入系統內存

    <10>導入庫文件

       基於鏈接的方式實現

[root@centos7~]#vim /etc/ld.so.conf.d/httpd.conf
/usr/local/httpd/lib
# 重讀庫文件列表
ldcofig -v

    <11>導入頭文件

[root@centos7~]#ln -sv /usr/local/httpd/include/ /usr/include/httpd
‘/usr/include/httpd’ -> ‘/usr/local/httpd/include/’


補充一點:

開源程序源代碼的獲取:

官方自建站點:

apache.org (ASF)

mariadb.org

...

代碼託管:

SourceForge.net

Github.com

code.google.com

    

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