Centos7安裝Web伺服器–Apache2.4.18安裝

米花兒團兒發表於2016-01-02

這是我首次嘗試的經驗(Centos7最小化安裝),走了很多坑,藉此記錄一下,希望朋友們少走一些彎路;
我是通過下載原始碼安裝的,所有的下載檔案為*.tar.bz2壓縮包(不同壓縮方式,解壓方式不同,我就下載同一種了);

一. 下載安裝包

一次性把檔案下載好,其中包括:
apr-1.5.2
apr-util-1.5.4
pcre-8.37(如果沒記錯的話,這個不要下載最新版;記錯的話,以上三個有一個不要下載最新版——我好不負責)
httpd-2.4.18

二. 檢視是否有編譯器

# gcc -v

如果沒有的話,安裝檔案時會報錯,可以根據提示安裝相應檔案;
我是通過yum install安裝的gcc 、c++

[root@root]# yum install gcc -y
[root@root]# yum install gcc-c++ -y

三. 解壓原始碼包

把第一步下載好的原始碼包解壓:

tar -jxvf DirName.tar.bz2

當然,如果你沒有解壓工具,請通過:

yum search bzip2查詢安裝解壓工具;

四. 安裝關聯檔案

如果直接安裝Apache的話(我就不直接安裝),它有可能會報錯(我都是一定會報錯):

checking for chosen layout... Apache
checking for working mkdir -p... yes
checking for grep that handles long lines and -e... /bin/grep
checking for egrep... /bin/grep -E
checking build system type... i686-pc-linux-gnu
checking host system type... i686-pc-linux-gnu
checking target system type... i686-pc-linux-gnu
configure:
configure: Configuring Apache Portable Runtime library...
configure:
checking for APR... no
configure: error: APR not found.  Please read the documentation.
這時候第一步下載的檔案就用的上了;
安裝順序apr -> apr-util ,當然pcre可以亂入(順序不重要);
安裝過程,配置 -> 編譯 -> 安裝 三部曲;

主要說配置:
進入解壓後的apr檔案包:
    [root@root]# ./configure --prefix=/usr/local/apr/(這裡配置的是安裝路徑)
    # make
    # make intall
進入解壓後的apr-util檔案包:
     [root@root]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/bin/apr-1-config(配置apr-util安裝路徑,並關聯apr檔案——確保路徑正確)
    # make
    # make intall
進入解壓後的pcre檔案包:
    [root@root]# ./configure --prefix=/usr/local/pcre(配置安裝路徑)
    # make
    # make intall  

五. 配置、編譯、安裝

關聯檔案安裝結束後就是主菜了:

進入解壓後的httpd檔案包:
[root@root]# ./configure --prefix=/usr/local/apache/ 
--sysconfdir=/etc/httpd  //指定Apache伺服器的配置檔案存放位置
--with-apr=/usr/local/apr 
--with-apr-util=/usr/local/apr-util/ 
--with-pcre=/usr/local/pcre/  
--enable-so  //以動態共享物件(DSO)編譯---記得要加,否則以後手動修改配置檔案載入新的模組,比如,不配置的話,安裝好PHP後,要手動修改conf來loadmodule……
--enable-deflate=shared  //縮小傳輸編碼的支援
--enable-expires=shared  //期滿頭控制
--enable-rewrite=shared  //基於規則的URL操控
--enable-static-support //建立一個靜態連結版本的支援
# make
# make intall  

六. 啟動

進入Apache的目錄/etc/httd/

修改httpd.conf配置檔案(Apache2.4與2.2配置不同)
Require all denied(禁止外部訪問)
Require all granted(允許外部訪問)

進入Apache的安裝目錄/usr/local/apache/bin(如果你沒自定義的話):

./apachectl start  #啟動服務
./apachectl stop   #關閉服務
./apachectl restart  #重啟服務

我用的Centos7不會提醒服務已啟動,所以,我一般一個命令執行兩次,第二次它會報告服務已啟動/已關閉;

七. 除錯

啟動就ok了?我曾經是這樣認為的——可是,坑還在繼續……

外網/區域網訪問不了:

排查1:
    伺服器/虛擬機器自身網路是否正常,可以通過ping http://www.baidu.com度娘地址試試;
排查2:
    服務是否啟動,佔用80埠的是否為http服務:
    netstat -ntpl  #檢視埠監聽(我的是,所以,我就沒有了關閉其它程式的經驗)
    如果你不一定非要80埠的話,可以修改/usr/local/apache/conf/httpd.conf監聽埠;
排查3:
    You don`t have permission to access / on this server
    沒有許可權,解決方案:
    chmod o+x /usr/
    chmod o+x /usr/local/
    chmod o+x /usr/local/apache/
    chmod o+x /usr/local/apache/htdocs(每一級目錄都要執行)
    
    以上問題可以通過curl http://127.0.0.1在伺服器/虛擬機器上檢驗本地是否可以訪問;
排查4:
    防火牆設定——Centos7預設使用firewalld代替iptables(網上多數是iptables的設定解決方案,**好吧!發現Centos7有的小版本還在用iptables**所以,只能嘗試兩個命令,存在的就是用的該防火牆)    
    systemctl start firewalld  #啟動
    systemctl status firewalld  #檢視狀態
    systemctl disable firewalld  #停止
    systemctl stop firewalld  #禁用(可以直接禁用防火牆,也可以新增80埠到例外)
    檢視活動區域狀態:
    # firewall-cmd --get-active-zones(firewall-cmd是一個合成詞)
    返回:
    [root@localhost bin]# firewall-cmd --get-active-zones
    **public**
         interfaces: eno16777736
    重要的是**public**、dmz,或者其它。根據這個值的不同,新增埠的語句也不同;
    加入一個埠到dmz區域:
    # firewall-cmd --zone=dmz --add-port=80/tcp
    加入一個埠到public區域:
    # firewall-cmd --zone=public --add-port=80/tcp
    永久生效再加上 --permanent 然後reload防火牆
    # firewall-cmd --zone=dmz --add-port=80/tcp --permanent
    或者 
    # firewall-cmd --zone=public --add-port=80/tcp --permanent
    # firewall-cmd --complete-reload
    
    檢視所有活動的埠:
    # firewall-cmd --zone=dmz --list-ports
    或者
    # firewall-cmd --zone=public --list-ports
    

由於我進行了很多步操作來解決外部訪問的問題,所以,最終僅僅通過以上除錯方法能否得到外部訪問的結果,不敢保證;

如不行:
檢查一下selinux設定(其它問題只能自行百度了)……

    
    

相關文章