(轉)CentOS 7.6 上編譯安裝httpd 2.4.38

原文:https://www.s4lm0x.com/archives/40.htmlhtml

http://www.javashuo.com/article/p-fpkvcrax-dn.html----超詳細 值得收藏 linux CentOS 7 配置Apache服務【轉發+新增】linux

https://blog.51cto.com/13525470/2070375----CentOS 7 Apache服務的安裝與配置c++

httpd是使用排名第一的Web服務器軟件. 它能夠運行在幾乎全部普遍使用的計算機平臺上, 因爲其跨平臺和安全性被普遍使用, 是最流行的Web服務器端軟件之一, 當前httpd版本爲2.4.38. 在CentOS 7上使用yum方式安裝httpd是最簡便的方式. 可是因爲CentOS 7自帶的htpd版本固定, 有時咱們可能須要安裝更新版本的httpd, 或者須要開啓httpd默認未開啓的功能模塊, 此時編譯httpd源碼的安裝方式將是咱們必須掌握的技能. 本文主要描述基於CentOS7. 6以源碼方式安裝httpd-2.4.38正則表達式

編譯安裝的步驟與優勢

  • 源碼的編譯安裝通常由3個步驟組成shell

    • 配置: configure, 一般依賴gcc編譯器, binutilsglibc, 配置軟件特性, 檢查編譯環境, 生成 Makefile文件
      • configure是一個可執行腳本, 它有不少選項, 在待安裝的源碼路徑下使用命令./configure –-help輸出詳細的選項列表
    • 編譯: make
    • 安裝: make install
  • 編譯安裝的優勢apache

    • 自定義軟件功能
    • 優化編譯參數,提升性能
    • 解決沒必要要的軟件間依賴
    • 方便清理與卸載

準備編譯環境

操做系統安裝時爲Minimal install方式, 此時須要安裝編譯器以及編譯器所依賴的軟件包. 最直接的方式是將Development Tools整個卡發包組安裝, 但開發包組中不少的包是沒必要要的, 故此只安裝必要的軟件包vim

  • 使用阿里雲的源安裝相關編譯環境windows

    cd /etc/yum.repos.d
    curl -sO https://mirrors.aliyun.com/repo/epel-7.repo https://mirrors.aliyun.com/repo/Centos-7.repo
    yum clean all
    yum makecache fast
  • 安裝gcc以及相關依賴安全

    yum install -y gcc glibc-devel zlib zlib-devel openssl-devel bzip2 bzip2-devel gcc-c++ expat-devel

安裝依賴包

編譯安裝pcre

PCRE是Perl Compatible Regular Expression的縮寫, 是一個Perl的正則表達式模塊. HTTP核心模塊和rewrite模塊都會用到PCRE的正則表達式語法, 因此這個必須安裝服務器

cd /usr/local/src
curl -sO https://ftp.pcre.org/pub/pcre/pcre-8.43.tar.bz2
tar xf pcre-8.43.tar.bz2
pcre-8.43
./configure --prefix=/usr/local/pcre
make -j 4
make install

編譯安裝apr, apr-util

httpd可在windows, linux, unix等多種平臺部署, 而並不須要爲每種平臺都編寫不一樣的代碼實現, 由於有apr、apr-util

apr: apache portable runtime, 相似於JVM, PVM等虛擬機, 爲apache提供運行環境. 針對不一樣平臺有不一樣的apr, httpd經過不一樣的apr,可運行於不一樣的平臺之上

apr-util依賴於apr, 需先編譯安裝apr, 再編譯安裝apr-util

  • 獲取源碼包並解壓編譯
cd ..
curl -sO http://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-1.6.5.tar.bz2
tar xf apr-1.6.5.tar.bz2
cd apr-1.6.5
./configure --prefix=/usr/local/apr
make -j 4 && make install
cd ..
curl -sO http://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-util-1.6.1.tar.bz2
tar xf apr-util-1.6.1.tar.bz2
cd apr-util-1.6.1
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
make && make install

編譯安裝httpd

在httpd 2.4 中, preforkworkerevent做爲模塊, 可以使用配置文件切換

在執行配置過程時可以使用disable, without, 顯式地指定禁用哪些功能, 不依賴哪些程序包等

httpd 2.4中運行httpd的user, group也變成了daemon而不是apache, 因此在編譯以前無需建立apache用戶與組

  • 獲取源碼包並解壓
cd ..
curl -sO http://mirrors.tuna.tsinghua.edu.cn/apache//httpd/httpd-2.4.38.tar.bz2
tar xf httpd-2.4.38.tar.bz2
cd httpd-2.4.38
  • 編譯前, 隱藏版本信息
    • 大概是在include/ap_release.h文件中的第40-46行
vim include/ap_release.h
#define AP_SERVER_BASEVENDOR "Apache Software Foundation" #服務的供應商名稱
#define AP_SERVER_BASEPROJECT "Apache HTTP Server" #服務的項目名稱
#define AP_SERVER_BASEPRODUCT "Apache" #服務的產品名
#define AP_SERVER_MAJORVERSION_NUMBER 2 #主要版本號
#define AP_SERVER_MINORVERSION_NUMBER 4 #小版本號
#define AP_SERVER_PATCHLEVEL_NUMBER 6 #補丁級別
  • 編譯安裝
./configure --prefix=/usr/local/httpd24 --with-pcre=/usr/local/pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --with-layout=Apache --enable-load-all-modules --enable-so --enable-rewrite --enable-ssl --enable-cgi --with-zlib --enable-proxy --enable-proxy-http --disable-version
make -j 4
make install
若是編譯安裝報錯:

make[2]: *** [ab] 錯誤 1
make[2]: Leaving directory `/software/httpd-2.4.9/support'
make[1]: *** [all-recursive] 錯誤 1
make[1]: Leaving directory `/software/httpd-2.4.9/support'
make: *** [all-recursive] 錯誤 1

1.檢查configure有沒有添加--with-ssl=/usr/local/openssl-1.0.1g(從新編譯安裝後openssl的路徑)的路徑,添加後可解決以上報錯的問題。

./configure --prefix=/home/apache-249  --enable-ssl --with-ssl=/usr/local/openssl-1.0.1g --with-pcre=/usr/local/pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --enable-so  --enable-rewrite --enable-modules=most --enable-mods-shared=most --enable-mpms-shared=all --with-mpm=event

若是不使用ssl,也能夠在編譯時候去掉--enable-ssl --with-ssl=/usr/local/openssl-1.0.1g,加上--disable-ssl

  • 編譯時的選項
    • —enable-so: 支持動態加載模塊
    • —enable-rewrite: 啓用URL重寫
    • —enable-ssl: 支持ssl加密
    • -–enable-mpms-shared=[prefork | worker | event | all]: 指定編譯哪些MPM模塊, 可以使用all表示全編譯
    • –with-mpm=[prefork | worker | event]: 如3種MPM模塊都編譯了,則經過此項指定默認使用哪一種
    • –enable-cgi: 啓用cgi
    • –enable-modules=[all | most | ……]: 指定啓用哪些模塊。可指定具體的模塊名,也可以使用all表示啓用全部模塊,most表示啓用大多數經常使用模塊。通常使用most便可
    • –with-zlib: 依賴zlib庫用於頁面壓縮
    • –with-pcre: 指定pcre的路徑,依賴pcre庫用於解決正則表達式問題
    • -–with-apr: 指定依賴的apr的路徑
    • –with-apr-util: 指定依賴的apr-util的路徑
    • —enable-proxy: 啓用代理模塊
    • —disable-version:

配置httpd

配置環境變量

  • /usr/local/httpd24/bin加入到系統PATH搜索路徑中
cd /usr/local/httpd24/bin
echo 'PATH=$PATH:/usr/local/httpd24/bin' > /etc/profile.d/httpd.sh
. /etc/profile.d/httpd.sh
  • 配置firewalld, 容許http服務被訪問
firewall-cmd --permanent --add-service=http
firewall-cmd --reload
  • 啓動httpd服務
apachectl start
curl 127.0.0.1

爲httpd添加Unit file

  • 建立httpd的Unit file, 使用systemd管理http服務
vim /usr/lib/systemd/system/httpd.service
[Unit]
Description=The Apache HTTP Server
After=network.target remote-fs.target nss-lookup.target
Documentation=man:httpd(8)
Documentation=man:apachectl(8)

[Service]
Type=notify
EnvironmentFile=/usr/local/httpd24/conf/httpd.conf
ExecStart=/usr/local/httpd24/bin/apachectl $OPTIONS -DFOREGROUND
ExecReload=/usr/local/httpd24/bin/apachectl $OPTIONS -k graceful
ExecStop=/bin/kill -WINCH ${MAINPID}
KillSignal=SIGCONT
PrivateTmp=true

[Install]
WantedBy=multi-user.target
  • 從新生成Unit之間的依賴關係
systemctl daemon-reload
apachectl stop
systemctl start httpd
curl 127.0.0.1
相關文章
相關標籤/搜索