天天看點

Centos7安裝httpd源碼包方式

1.0 為什麼要用源碼包的方式安裝httpd

httpd可以使用yum的方式安裝,簡單,一步到位。也可以使用源碼包的方式安裝,步驟繁瑣。但是源碼包方式安裝的httpd性能要比yum方式高5%~10%,是以各有優缺點,都是需要掌握的。

1.1安裝apr

apr全稱是Apache portable run-time libraries,Apache可移植運作庫。可以為httpd提供一個支援跨作業系統平台的底層接口庫。我們可以将apr了解為一個安裝包,httpd可以直接調用使用它。Apr有三個開發包:apr、apr-util、apr-iconv。本次實驗隻安裝apr和apr-util。

[[email protected] ~]# cd /usr/local/src 
[[email protected] src]# wget http://archive.apache.org/dist/apr/apr-1.4.2.tar.gz
[[email protected] src]# tar -xf apr-1.4.2.tar.gz
[[email protected] apr-1.4.2]# cd apr-1.4.2
[[email protected] apr-1.4.2]# ./configure --prefix=/usr/local/apr //指定安裝位置
[[email protected] apr-1.4.2]# make && make install
           

一點建議:在安裝軟體的或者進行其他步驟繁瑣的操作時,最好每進行重要的一步就執行一次# echo $?,檢查是否執行成功,可以有效的減少後期錯誤的出現。

1.2 安裝apr-util

[[email protected] ~]# cd /usr/local/src 
[[email protected] src]# wget http://archive.apache.org/dist/apr/apr-util-1.3.10.tar.gz
[[email protected] src]# tar -xf apr-util-1.3.10.tar.gz 
[[email protected] src]# cd apr-util-1.3.10
[[email protected] apr-util-1.3.10]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr            
//安準apr-util依賴apr,需指定apr安裝的位置
[[email protected] apr-util-1.3.10]# echo $?
0   //0表示執行成功,非0表示執行失敗。
[[email protected] apr-util-1.3.10]# make && make install
[[email protected] apr-util-1.3.10]# echo $?
           

1.3 安裝pcre

很多應用程式安裝都依賴于pcre,比如:httpd、nginx等等。Pcre是一個perl庫,用來處理正規表達式的。

[[email protected] src]# yum   gcc-c++  //安裝編譯環境,pcre來解決C語言中使用正規表達式的問題
[[email protected] ~]# cd /usr/local/src 
[[email protected] ~]# wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.42.tar.gz
[[email protected] src]# tar xf pcre-8.42.tar.gz 
[[email protected] src]# cd pcre-8.42
[[email protected] pcre-8.42]# ./configure --prefix=/usr/local/pcre
[[email protected] pcre-8.42]# make && make install
           

1.4 安裝httpd,非常重要的一步。

[[email protected] ~]# cd /usr/local/src
[[email protected] src]# wget https://mirrors.tuna.tsinghua.edu.cn/apache/httpd/httpd-2.4.37.tar.gz
[[email protected] src]# tar xf httpd-2.4.37.tar.gz
[[email protected] src]# cd httpd-2.4.37
[[email protected] httpd-2.4.37]# ./configure --prefix=/HttpApp/httpd-2.4.37 --enable-deflate --enable-headers --enable-modules=most --enable-so --enable-rewrite --with-mpm=worker  --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --with-pcre=/usr/local/pcre
[[email protected] httpd-2.4.37]# echo $?
0
[[email protected] httpd-2.4.37]# make && make install
           

對配置那一步詳細說明一下:

–prefix 指定安裝位置,最好是放在一個新的檔案夾下友善修改應用程式的屬組和屬主,增加程式的安全性。安裝的具體目錄最好帶着版本号,用于以後友善修改軟連接配接更新。

–enable-deflate 啟用壓縮功能,隻有這個參數啟用,配置檔案中的壓縮功能才能啟用。啟用後,伺服器向用戶端發送資料時先壓縮後發送,節省帶寬。用戶端浏覽器收到後先解壓後浏覽。

–enable-headers 啟用頭文相關件

–enable-modules=most 啟用大多數子產品,這些子產品平時已經足夠用了。

–enable-so 啟用so子產品,httpd和php連接配接時通過php5.so子產品

–enable-rewrite 啟用重寫功能,就是因為這個功能我們之前才安裝pcre

–with-mpm=worker 啟用worker模式

–with-apr=/usr/local/apr 指定apr安裝目錄,apr為依賴環境

–with-apr-util=/usr/local/apr-util 指定apr-util安裝目錄,apr-util為依賴環境

–with-pcre=/usr/local/pcre 指定pcre安裝目錄

1.5 做軟連接配接

Httpd有了新版本後需要更新,如果隻是安裝上新版本後,在将一些使用老版本httpd的路徑檔案修改為新版的路徑時會很麻煩,比如配置的環境變量。使用軟連接配接後,可以建立一個檔案夾用于連結到目前使用的httpd檔案夾,安裝新版本後在連結到新安裝目錄,有效解決了上述問題。

[[email protected] ~]# ln -s /HttpAppapp/httpd-2.4.37/ /HttpApp/httpd
[[email protected]~]# ll /HttpApp
lrwxrwxrwx  1 root root  18 Nov 30 20:08 httpd -> /HttpApp/httpd-2.4.37/
drwxr-xr-x 14 root root 164 Nov 30 20:05 httpd-2.4.37
           

1.6 配置環境變量

配置環境變量的作用是可以在任意的位置使用httpd自帶的指令,而不需要把指令的完整路徑也寫出。

[[email protected] ~]# vi /etc/profile
export PATH=$PATH:/app/httpd/bin/
[[email protected] ~]# source /etc/profile
           

1.7 啟動httpd

[[email protected] ~]# vi /HttpApp/httpd/conf/httpd.conf
#ServerName www.example.com:80
ServerName httpd-1
[[email protected] ~]# apachectl start
           

在配置檔案中設定web伺服器主機名,如果不設定,在啟動過程中會出現“ Could not reliably determine the server’s fully qualified domain name”的報錯。

1.8 檢視httpd狀态

檢視httpd程序

[[email protected] ~]# ps -ef | grep httpd
root     7140    1  0 16:33 ?    00:00:00 /app/httpd-2.4.37/bin/httpd -k start
daemon   7144   7140  0 16:33 ?   00:00:00 /app/httpd-2.4.37/bin/httpd -k start
daemon   7145   7140  0 16:33 ?     00:00:00 /app/httpd-2.4.37/bin/httpd -k start
daemon     7146   7140  0 16:33 ?    00:00:00 /app/httpd-2.4.37/bin/httpd -k start
root       7239   7087  0 16:39 pts/0    00:00:00 grep --color=auto httpd
           

檢視httpd端口和檔案描述符

[[email protected] ~]# netstat -lnput | grep httpd       
tcp6       0      0 :::80           :::          LISTEN      7140/httpd
[[email protected] ~]# lsof -i :80
COMMAND  PID   USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
httpd   7140   root    4u  IPv6  36156      0t0  TCP *:http (LISTEN)
httpd   7144 daemon    4u  IPv6  36156      0t0  TCP *:http (LISTEN)
httpd   7145 daemon    4u  IPv6  36156      0t0  TCP *:http (LISTEN)
httpd   7146 daemon    4u  IPv6  36156      0t0  TCP *:http (LISTEN)
           

1.9 設定開機自啟動

因為不是yum方式安裝的httpd,是以不能通過systemctl enable指令設定開機自啟。源碼包方式設定開機自啟可以通過配置/etc/rc.d/rc.local檔案,這是一個開啟啟動檔案,開機時會逐行執行檔案中的指令。

[[email protected] ~]# vi /etc/rc.local
/HTTPApp/httpd/bin/apachectl start
//注意的是這裡邊的啟動指令需要完整的路徑,不能隻是一個apachectl,因為此時環境配置檔案還未生效
[[email protected] ~]# chmod +x /etc/rc.d/rc.local
[[email protected] ~]# reboot
           

1.10 httpd相關的指令

[[email protected] ~]# apachectl -help   //檢視apachectl指令的使用方式
[[email protected] ~]# apachectl -v     //檢視安裝的httpd版本和安裝時間
Server version: Apache/2.4.37 (Unix)
Server built:   Jun 10 2019 17:51:34
[[email protected] ~]# apachectl -V
//檢視httpd版本、服務子產品、apr和apr-util版本、平台架構以及其他編譯資訊
Server version: Apache/2.4.37 (Unix)
Server built:   Jun 10 2019 17:51:34
Server's Module Magic Number: 20120211:83
Server loaded:  APR 1.4.2, APR-UTIL 1.3.10
Compiled using: APR 1.4.2, APR-UTIL 1.3.10
Architecture:   64-bit
Server MPM:     worker
  threaded:     yes (fixed thread count)
    forked:     yes (variable process count)
Server compiled with....  //詳細部分可以自行檢視
[[email protected] ~]# apachectl -M  //檢視httpd加載子產品
Loaded Modules:
 core_module (static)
 so_module (static)
 http_module (static)
 mpm_worker_module (static) .... //詳細部分自行檢視
[[email protected] ~]# apachectl -l   //檢視httpd編譯子產品
Compiled in modules:
  core.c
  mod_so.c
  http_core.c
  worker.c
           

1.11 httpd優雅重新開機

什麼叫優雅重新開機呢?

優雅重新開機 ( Graceful Restart )是一種旨在最小化路由協定重新開機影響的機制。說白了優雅重新開機就是在對目前環境影響最小的情況下重新開機。

在httpd使用優雅重新開機時,會等正在處理的請求全部完成後再重新開機,防止使用者正在使用時突然失去連接配接,這個方式重新開機後,伺服器将不再接受請求直到重新開機完成。使用者體驗比restart要好。

[[email protected] ~]# apachectl graceful
           

繼續閱讀