1. 程式人生 > >Linux CentOS7 系統下Apache2.4.17的安裝過程

Linux CentOS7 系統下Apache2.4.17的安裝過程

Linux系統下安裝Apache Server2.4.17。還是先宣告一下,Linux命令我不進行講解,因為我不是講Linux命令的。有需要注意的地方,我會上圖,沒什麼值得的注意的地方,我就不上圖了。還有就是怎樣將壓縮包上傳到Linux系統中去,我不進行講解,還是那句話:網上教程太多了!最後,確保你的Linux是連網的!

工具/原料

  • Linux系列系統
  • Apache Server2.4.17原始碼包
  • APR原始碼包
  • APR-Util原始碼包
  • PCRE原始碼包

方法/步驟

 
  1.  

    首先,秉承我一貫的風格,來看一小段官方文件:

    The following requirements exist for building Apache httpd:

    APR and APR-Util

    Make sure you have APR and APR-Util already installed on your system. 

    Perl-Compatible Regular Expressions Library (PCRE)

    This library is required but not longer bundled with httpd. 

    Disk Space

    Make sure you have at least 50 MB of temporary free disk space available. 

    ANSI-C Compiler and Build System

    Make sure you have an ANSI-C compiler installed. The GNU C compiler (GCC) from the Free Software Foundation (FSF) is recommended.

    Accurate time keeping

    Elements of the HTTP protocol are expressed as the time of day. So, it's time to investigate setting some time synchronization facility on your system. Usually the ntpdate or xntpd programs are used for this purpose which are based on the Network Time Protocol (NTP). 

    我來翻譯一下:

    要編譯Apache httpd,存在以下要求:

    APR and APR-Util

    確保你的系統中已經安裝了APR和APR-Util。

    Perl-Compatible Regular Expressions Library (PCRE)

    這個庫是必需的,但它不再與httpd捆綁在一起。

    Disk Space

    確保你有至少50MB的臨時空閒磁碟空間可以使用。

    ANSI-C Compiler and Build System

    確保你已經安裝了一個ANSI-C編譯器。推薦使用由自由軟體基金會(FSF)編寫的GNU C編譯器(GCC)。

     

    測試(檢視版本資訊)

     

    檢視gcc版本資訊

     

    gcc --version

     

     

    Accurate time keeping

    HTTP協議的元素會以一天的時間進行表示。所以,它的時間會參照你係統中的一些時鐘同步裝置的設定。通常基於網路時間協議(NTP)的程式ntpdate或xntpd就是用來做這樣的事情。

    通過官方文件我們瞭解到,在安裝Apache2.4之前我們要做的是:安裝APR、APR-Util和PCRE

  2.  

    無論你是要安裝APR、APR-Util、PCRE還是Apache Server或者是其他的一些軟體,總之,只要是原始碼安裝,那麼就需要一個ANSI-C編譯器。如果你沒有,那就用yum裝一個吧,既然推薦使用GCC,那就裝個GCC吧。執行命令:yum  install  -y  gcc。如果,你閒得發慌,你可以不依賴yum,自己進行安裝GCC試試,相信你會爽歪歪的!

  3.  

    接下來,就是要安裝APR、APR-Util和PCRE。安裝之前,來看下怎麼下載它們。先來看下APR與APR-Util的下載,大家可以去http://apr.apache.org進行下載,看下面兩張圖:

    Linux系統下Apache2.4.17的安裝過程 Linux系統下Apache2.4.17的安裝過程
  4.  

    再來看下怎麼下載PCRE。大家可以去http://www.pcre.org進行下載,看下面四張圖:

    Linux系統下Apache2.4.17的安裝過程 Linux系統下Apache2.4.17的安裝過程 Linux系統下Apache2.4.17的安裝過程 Linux系統下Apache2.4.17的安裝過程
  5.  

    將下載的3個壓縮包上傳到你的Linux系統中去。我上傳到了我Linux系統下/mysoft目錄中。進入這個目錄:cd  /mysoft。我們先解包APR,並檢視/mysoft目錄下的檔案,依次執行命令:

    gzip  -d  apr-1.5.2.tar.gz

    tar  -xf  apr-1.5.2.tar

    ls  -l

    可以看到/mysoft目錄下多了一個目錄apr-1.5.2。

    Linux系統下Apache2.4.17的安裝過程
  6.  

    進入apr-1.5.2目錄:cd  ./apr-1.5.2/。然後,進行APR的原始碼安裝,依次執行命令:

    ./configure

    make

    make install

    安裝完成後,我們可以在make install過程中看到如下圖所示資訊,我們可以看到APR的安裝路徑/usr/local/apr。當然,你也可以使用  --prefix=目錄  選項來指定一個安裝目錄。

    Linux系統下Apache2.4.17的安裝過程
  7.  

    接著,我們來解包APR-Util,並檢視/mysoft目錄下的檔案,依次執行命令:

    gzip  -d  apr-util-1.5.4.tar.gz

    tar  -xf  apr-util-1.5.4.tar

    ls  -l

    可以看到/mysoft目錄下多了一個目錄apr-util-1.5.4。

    Linux系統下Apache2.4.17的安裝過程
  8.  

    進入apr-util-1.5.4目錄:cd  ./apr-util-1.5.4/。然後,進行APR-Util的原始碼安裝,依次執行命令:

    ./configure  --with-apr=/usr/local/apr

    make

    make install

    在安裝APR-Util時,需要指定APR的安裝路徑,所以需要帶上選項--with-apr=/usr/local/apr,如果你不帶上這個選項,在configure的時候會看到下圖的錯誤提示。

    Linux系統下Apache2.4.17的安裝過程
  9.  

    下面,我們來安裝PCRE。首先,解包PCRE,並檢視/mysoft目錄下的檔案,依次執行命令:

    gzip  -d  pcre-8.36.tar.gz

    tar  -xf  pcre-8.36.tar

    ls  -l

    可以看到/mysoft目錄下多了一個目錄pcre-8.36。

    Linux系統下Apache2.4.17的安裝過程
  10.  

    進入pcre-8.36目錄:cd  ./pcre-8.36/。然後,進行pcre-8.36原始碼安裝,但是,當你在執行./configure時,會看到下圖所示提示:

    configure: error: You need a C++ compiler for C++ support

    不用擔心,yum又派上用場了,執行命令:yum  install  -y  gcc  gcc-c++ 。

    gcc-c++安裝完成後,我們繼續我們的pcre-8.36原始碼安裝,依次執行命令:

    ./configure

    make

    make  install

    Linux系統下Apache2.4.17的安裝過程
  11.  

    至此,準備工作就緒,開始準備安裝Apache Server2.4,首先,來看下怎麼下載Apache2.4,我們可以去http://httpd.apache.org進行下載,看下圖。

    Linux系統下Apache2.4.17的安裝過程
  12.  

    安裝Apache之前,我們再來看一小段官方文件:

     --enable-mods-shared=MODULE-LIST

    Defines a list of modules to be enabled and build as dynamic shared modules. This mean, these module have to be loaded dynamically by using the LoadModule directive.MODULE-LIST is a space separated list of modulenames enclosed by quotation marks. The module names are given without the preceding mod_. For example:

    --enable-mods-shared='headers rewrite dav'

    Additionally you can use the special keywords reallyall, all, most, few and none. For example,

    --enable-mods-shared=most will compile most modules and build them as DSO modules

    --enable-mods-shared=few will only compile a very basic set of modules

    The default set is most.

    The LoadModule directives for the chosen modules will be automatically generated in the main configuration file. By default, all those directives will be commented out except for the modules that are either required or explicitly selected by a configure --enable-foo argument. You can change the set of loaded modules by activating or deactivating the LoadModule directives in httpd.conf. In addition the LoadModule directives for all built modules can be activated via the configure option --enable-load-all-modules.

    --enable-mods-static=MODULE-LIST

    This option behaves similar to --enable-mods-shared, but will link the given modules statically. This mean, these modules will always be present while running httpd. They need not be loaded with LoadModule.

    --enable-modules=MODULE-LIST

    This option behaves like to --enable-mods-shared, and will also link the given modules dynamically. The special keyword none disables the build of all modules.

    我來翻譯一下:

     --enable-mods-shared=MODULE-LIST

     定義一個要啟用的模組列表,並且這些模組會以動態共享模組進行編譯。這就意味著,這些模組需要通過使用LoadModule指令進行動態載入。MODULE-LIST是一個由引號引起來且由空格進行分隔的模組名列表。這些模組名要以去掉前面的mod_的形式給出。例如:

     --enable-mods-shared='headers rewrite dav'

     你還可以使用特殊關鍵字reallyall,all,most,few和none。例如:

     --enable-mods-shared=most 編譯大部分模組並將它們作為動態共享模組進行編譯

     --enable-mods-shared=few 只編譯一組比較基本的模組

    預設關鍵字是most。

    對於已選擇的模組來說,LoadModule指令會自動在主要配置檔案中生成。預設情況下,所有這些指令會被註釋掉,除了一些必需的或是通過一個--enable-foo引數明確指定的模組。你可以在httpd.conf檔案中通過啟用或登出LoadModule指令來改變已載入模組的設定。另外,可以通過配置選項--enable-load-all-modules來將所有已編譯模組的LoadModule指令啟用。

    --enable-mods-static=MODULE-LIST

    這個配置選項的行為與--enable-mods-shared類似,但會靜態連結給出的模組。這就意味著,在執行httpd的時候,這些模組總是存在。它們不需要用LoadModule指令進行載入。

    --enable-modules=MODULE-LIST

    這個配置選項的行為與--enable-mods-shared類似,也會動態連結給出的模組。可以使用特殊關鍵字none來禁用所有要編譯的模組。

  13.  

    更多的內容大家可以自己去官網看,這裡我就不再多講了。進入正題,解包httpd,並檢視/mysoft目錄下的檔案,依次執行命令:

    gzip  -d  httpd-2.4.17.tar.gz

    tar  -xf  httpd-2.4.17.tar

    ls  -l

    可以看到/mysoft目錄下多了一個目錄httpd-2.4.17。

    Linux系統下Apache2.4.17的安裝過程
  14.  

    進入httpd-2.4.17目錄:cd  ./httpd-2.4.17/。然後,進行Apache原始碼安裝,依次執行命令:

    ./configure  --enable-mods-shared=all

    make

    make  install

    Apache預設安裝在/usr/local/apache2目錄下。同樣,這裡你也可以使用

    --prefix=目錄名

    來指定一個安裝目錄,例如:

    ./configure  --prefix=/usr/mydir  --enable-mods-shared=all

    Linux系統下Apache2.4.17的安裝過程
  15.  

    Apache已經安裝成功,現在啟動httpd服務。執行命令:

    /usr/local/apache2/bin/apachectl  -k  start。

    大家會發現,httpd服務並沒有啟動,而是出現了一條如下圖所示的錯誤資訊:

    AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain. Set the 'ServerName' directive globally to suppress this message

    Linux系統下Apache2.4.17的安裝過程
  16.  

    出現上述錯誤資訊,是因為配置檔案httpd.conf中的ServerName沒有修改。

    我們用Vim開啟httpd.conf配置檔案,執行命令:

    vi  /usr/local/apache2/conf/httpd.conf

    設定Vim在執行搜尋時不區分大小寫,執行命令:

    :set ic

    搜尋"servername"字串,執行命令:

    /servername

    找到servername後,按A鍵進行編輯。將ServerName前的#號去掉,將ServerName後面的字串改為:localhost:80,如下圖所示。

    修改完成後,按Esc鍵由編輯模式切換到命令模式,然後執行儲存退出命令:

    :wq

    Linux系統下Apache2.4.17的安裝過程
  17.  

    在啟動httpd之前,我們先為其建立一個軟連結(相當於windows下的快捷方式),否則每次重啟Apache都要輸那麼一長串,很費勁。

    為httpd建立軟連結,並檢視一下軟連結是否建立成功,依次執行命令:

    ln  -s  /usr/local/apache2/bin/apachectl  /mylinks/httpd。

    ls  -l  /mylinks

    來吧,啟動httpd服務,執行命令:/mylinks/httpd  -k  start

    Linux系統下Apache2.4.17的安裝過程
  18.  

    在測試Apache是否已經正常工作之前,我們先看下防火牆的狀態:

    systemctl  status  firewalld

    如果處於running執行狀態,就把它關掉:

    systemctl  stop  firewalld

    然後,檢視一下防火牆是否已經關掉:

    systemctl  status  firewalld

    如果處於dead狀態,就說明已經關掉了。

    如果你希望每次系統啟動的時候都不要啟動防火牆,可以執行命令:

    systemctl  disable  firewalld

    同樣,如果你希望每次系統啟動的時候都啟動防火牆,可以執行命令:

    systemctl  enable  firewalld

    Linux系統下Apache2.4.17的安裝過程 Linux系統下Apache2.4.17的安裝過程 Linux系統下Apache2.4.17的安裝過程
  19.  

    防火牆關閉後,接著來看下你係統的IP地址:ifconfig  -a。可以看到我的IP是192.168.0.104。

    Linux系統下Apache2.4.17的安裝過程
  20.  

    開啟瀏覽器,在位址列中輸入:192.168.0.104,回車。可以看到It  works!

    Linux系統下Apache2.4.17的安裝過程
  21.