溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Linux下Apache服務(wù)如何部署和配置

發(fā)布時間:2021-12-20 09:08:39 來源:億速云 閱讀:829 作者:小新 欄目:開發(fā)技術(shù)

這篇文章主要為大家展示了“Linux下Apache服務(wù)如何部署和配置”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“Linux下Apache服務(wù)如何部署和配置”這篇文章吧。

    1 Apache的作用

    • 解析網(wǎng)頁語言,如html,php,jsp等

    • 接收web用戶的請求,并給予一定的響應(yīng)

    2 Apache的安裝

    安裝apche軟件:dnf install httpd.x86_64 -y

    Linux下Apache服務(wù)如何部署和配置

    3 apache的啟用

    • 開啟apache服務(wù)并設(shè)置開機(jī)啟動:systemctl enable --now httpd

    • 查看apache服務(wù)的狀態(tài):systemctl enable --now httpd

    Linux下Apache服務(wù)如何部署和配置

    • 查看火墻信息:firewall-cmd --list-all 在火墻中永久開啟http服務(wù):firewall-cmd --permanent --add-service=http

    • 在火墻中永久開啟https服務(wù): firewall-cmd --permanent --add-service=https

    • 在不改變當(dāng)前火墻狀態(tài)的情況下刷新防火墻:firewall-cmd --reload

    Linux下Apache服務(wù)如何部署和配置

    4 apache的基本信息

    apche的基本信息

    • 服務(wù)名稱:httpd

    • 主配置文件:/etc/httpd/conf/httpd.conf

    • 子配置文件:/etc/httpd/conf.d/*.conf

    • 默認(rèn)發(fā)布目錄:/var/www/html

    • 默認(rèn)端口:80 (http) ,443(https)

    • 日志文件:/etc/httpd/logs

    • 開啟apche服務(wù)后,輸入ip查看默認(rèn)發(fā)布頁面:

    Linux下Apache服務(wù)如何部署和配置

    (1)更改apche服務(wù)的端口號

    • 查看httpd服務(wù)的默認(rèn)端口號:netstat -antlupe |grep httpd

    Linux下Apache服務(wù)如何部署和配置

    • 編輯配置文件: /etc/httpd/conf/httpd.conf,修改端口號

    Linux下Apache服務(wù)如何部署和配置

    • 重啟httpd服務(wù):systemctl restart httpd

    • 查看httpd服務(wù)的端口號:netsat -antlupe | grep httpd

    Linux下Apache服務(wù)如何部署和配置

    • 更改端口號后,輸入ip后無法正常連接,原因是8080端口未添加在火墻中

    Linux下Apache服務(wù)如何部署和配置

    • 在防火墻里添加888端口號:firewall-cmd --permanent --add-port=888/tcp

    • 在不改變當(dāng)前火墻狀態(tài)的情況下刷新防火墻:firewall-cmd --reload

    Linux下Apache服務(wù)如何部署和配置

    • 輸入IP地址:端口號,可以正常訪問

    Linux下Apache服務(wù)如何部署和配置

    (2)修改apche的默認(rèn)發(fā)布文件

    • 默認(rèn)目錄:cd /var/www/html

    • 在文件默認(rèn)發(fā)布目錄下新建一個文件index.html

    Linux下Apache服務(wù)如何部署和配置

    • 輸入:http://172.25.254.144查看

    Linux下Apache服務(wù)如何部署和配置

    默認(rèn)發(fā)布文件就是訪問apache時沒有指定文件名,即默認(rèn)訪問的文件,此文件可以指定多個,但有訪問順序。

    • 新建文件并編輯:westo.html

    Linux下Apache服務(wù)如何部署和配置

    • 編輯配置文件: /etc/httpd/conf/httpd.conf

    Linux下Apache服務(wù)如何部署和配置

    • 重啟httpd服務(wù):systemctl restart httpd

    Linux下Apache服務(wù)如何部署和配置

    (3)修改apche的默認(rèn)發(fā)布目錄

    • 新建目錄:mkdir -p /westos/html/

    • 創(chuàng)建文件:vim /westos/html/index.html

    Linux下Apache服務(wù)如何部署和配置

    • 編輯apche配置文件: /etc/httpd/conf/httpd.conf

    Linux下Apache服務(wù)如何部署和配置

    • 重啟服務(wù):systemctl restart httpd

    • 測試:瀏覽器中輸入http://172.25.254.144, 看到的是/westos/html/目錄內(nèi)的默認(rèn)發(fā)布文件

    Linux下Apache服務(wù)如何部署和配置

    • 新建發(fā)布目錄:mkdir /var/www/html/westos

    • 新建發(fā)布文件:vim /var/www/html/westos/index.html

    Linux下Apache服務(wù)如何部署和配置

    • 編輯配置文件:vim /etc/httpd/conf/httpd.conf

    Linux下Apache服務(wù)如何部署和配置

    • 重啟服務(wù):systemctl restart httpd

    • 測試:http://172.25.254.144/westos/

    Linux下Apache服務(wù)如何部署和配置

    5 apache的訪問控制

    5.1 基于客戶端ip的訪問控制

    • 基于ip的訪問,規(guī)定了哪些ip可以訪問,那些ip不能訪問,其中配置文件中order中的deny和Allow哪一個順序在前直接決定了黑白名單的屬性

    (1)白名單

    • ip白名單:只有名單內(nèi)的用戶可以訪問

    • 編輯配置文件:vim /etc/httpd/conf/httpd.conf

    Linux下Apache服務(wù)如何部署和配置

    • 重啟服務(wù):systemctl restart httpd

    • 測試: ip=172.25.254.44在ip白名單,可以正常訪問http://172.25.254.44/westos

    Linux下Apache服務(wù)如何部署和配置

    ip=172.25.254.144的主機(jī)不在白名單內(nèi),無法訪問http://172.25.254.44/westos

    Linux下Apache服務(wù)如何部署和配置

    (2)ip黑名單

    • ip黑名單:只有名單內(nèi)的用戶不可以訪問

    • 編輯配置文件:vim /etc/httpd/conf/httpd.conf

    Linux下Apache服務(wù)如何部署和配置

    • 測試:ip=172.25.254.44在ip黑名單中,可以正常訪問http://172.25.254.44/westos

    Linux下Apache服務(wù)如何部署和配置

    ip=172.25.254.144不在黑名單內(nèi),可以正常訪問http://172.25.254.44/westos

    Linux下Apache服務(wù)如何部署和配置

    5.2 基于用戶認(rèn)證的訪問控制

    (1)允許部分用戶通過認(rèn)證訪問共享目錄

    • 生成認(rèn)證文件,建立admin用戶:htpasswd -cm /etc/httpd/htpasswdfile linux

    Linux下Apache服務(wù)如何部署和配置

    • 建立linux用戶,-c會重新建立用戶認(rèn)證文件,覆蓋之前的admin用戶,入密碼會覆蓋之前的用戶: htpasswd -m /etc/httpd/htpasswdfile westos

    Linux下Apache服務(wù)如何部署和配置

    • 只允許部分用戶可以通過認(rèn)證,編輯配置文件:vim /etc/httpd/conf/httpd.conf

    Linux下Apache服務(wù)如何部署和配置

    • 重啟服務(wù):systemctl restart httpd

    • 測試:只有通過認(rèn)證的用戶可以訪問共享目錄

    Linux下Apache服務(wù)如何部署和配置

    Linux下Apache服務(wù)如何部署和配置

    Linux下Apache服務(wù)如何部署和配置

    Linux下Apache服務(wù)如何部署和配置

    (2)允許所有用戶通過認(rèn)證訪問共享目錄

    • 編輯配置文件:vim /etc/httpd/conf/httpd.conf

    Linux下Apache服務(wù)如何部署和配置

    • 重啟服務(wù):systemctl restart httpd

    • 測試:所有用戶都可以通過認(rèn)證訪問共享目錄

    Linux下Apache服務(wù)如何部署和配置

    Linux下Apache服務(wù)如何部署和配置

    6 apache的虛擬主機(jī)

    虛擬主機(jī):在一臺真實主機(jī)上建立多個站點(多個域名),通過域名訪問一臺主機(jī)的不同網(wǎng)頁,從網(wǎng)絡(luò)地址看似乎有多個主機(jī),這些主機(jī)被稱為虛擬主機(jī)

    DNS解析域名的ip

    建立linux,news,media的默認(rèn)發(fā)布目錄: mkdir /var/www/westos.com/{linux,news,media}
    inux的默認(rèn)發(fā)布文件: echo "<h2>hello linux</h2>"  >  /var/www/westos.com/news/index.html
    news的默認(rèn)發(fā)布文件:echo "<h2>hello news </h2>" > /var/www/westos.com/news/index.html
    media的默認(rèn)發(fā)布文件:echo "<h2>hello media </h2>" > /var/www/westos.com/media/index.html
    • 新建apche的子配置文件并編輯:/etc/httpd/conf.d/vhost.conf

    Linux下Apache服務(wù)如何部署和配置

    • 編輯本地域名解析文件:/etc/hosts

    Linux下Apache服務(wù)如何部署和配置

    • 測試實驗效果:

    Linux下Apache服務(wù)如何部署和配置

    Linux下Apache服務(wù)如何部署和配置

    Linux下Apache服務(wù)如何部署和配置

    7 apache的加密訪問

    (1)安裝加密插件

    • 查看apache的加密插件: dnf search apache

    Linux下Apache服務(wù)如何部署和配置

    • 安裝加密插件

    Linux下Apache服務(wù)如何部署和配置

    (2)生成私鑰: openssl genrsa -out /etc/pki/tls/private/www.westos.com.key

    Linux下Apache服務(wù)如何部署和配置

    (3)生成證書簽名文件:openssl req -new -key /etc/pki/tls/private/www.westos.com.key -out /etc/pki/tls/cert/www.westos.com.csr

    Linux下Apache服務(wù)如何部署和配置

    (4)生成證書:

    openssl x509 -req -days 365 -in /etc/pki/tls/certs/www.westos.com.csr -signkey  /etc/pki/tls/private/www.westos.com.key -out /etc/pki/tls/certs/www.westos.com.crt
    ##  x509:證書格式
    ##  -req請求
    ##  -in加載簽證名稱
    ##  -signkey

    Linux下Apache服務(wù)如何部署和配置

    • 編輯配置文件:/etc/httpd/conf.d/ssl.conf

    Linux下Apache服務(wù)如何部署和配置

    • 編輯apche的子配置文件并編輯:/etc/httpd/conf.d/vhost.conf

    Linux下Apache服務(wù)如何部署和配置

    • 重啟服務(wù):systemctl restart httpd

    Linux下Apache服務(wù)如何部署和配置

    Linux下Apache服務(wù)如何部署和配置

    Linux下Apache服務(wù)如何部署和配置

    Linux下Apache服務(wù)如何部署和配置

    • 測試:此時就可以正常使用https加密服務(wù)

    Linux下Apache服務(wù)如何部署和配置

    8 網(wǎng)頁重寫

    在瀏覽器中輸入media.westos.com會自動跳轉(zhuǎn)到如下界面

    Linux下Apache服務(wù)如何部署和配置

    • 如果要使輸入media.westos.com后跳轉(zhuǎn)到https://media.westos.com,可以通過網(wǎng)頁重寫實現(xiàn),也就是當(dāng)訪問http(80端口)時自動跳轉(zhuǎn)到https(443端口)

    • 實現(xiàn)網(wǎng)頁重寫的步驟

    (1)apche的子配置文件并編輯:/etc/httpd/conf.d/vhost.conf

    Linux下Apache服務(wù)如何部署和配置

    (2)重啟服務(wù):systemctl restart httpd

    (3)測試,輸入域名后會自動加載https

    Linux下Apache服務(wù)如何部署和配置

    9 正向代理

    (1) 配置squid客戶端(該主機(jī)可以上網(wǎng))

    Linux下Apache服務(wù)如何部署和配置

    • 編輯配置文件:/etc/squid/squid.conf

    Linux下Apache服務(wù)如何部署和配置

    • 啟動squid服務(wù): systemctl start squid.service

    Linux下Apache服務(wù)如何部署和配置

    (2)客戶端:在不能上網(wǎng)的主上測試,在瀏覽器中輸入:www.baidu.com不能訪問

    Linux下Apache服務(wù)如何部署和配置

    • 加入代理:Preference——>Network settings——>Manual proxy configuration

    Linux下Apache服務(wù)如何部署和配置

    Linux下Apache服務(wù)如何部署和配置

    • 填寫squid服務(wù)的主機(jī)和squid服務(wù)的端口號,設(shè)置完成后,該主機(jī)雖然沒有聯(lián)網(wǎng)但是可以通過代理訪問www.baidu.com及其他網(wǎng)站

    Linux下Apache服務(wù)如何部署和配置

    • 在客戶端測試,能正常訪問www.baidu.com

    Linux下Apache服務(wù)如何部署和配置

    但是在客戶端主機(jī)在仍然不能ping通www.baidu.com

    Linux下Apache服務(wù)如何部署和配置

    10 反向代理

    node1:沒有apache服務(wù)的虛擬機(jī)172.25.254.244

    node2:能正常使用apache服務(wù)的虛擬機(jī)172.25.254.193,配置apache的發(fā)布文件

    Linux下Apache服務(wù)如何部署和配置

    • 下載代理: dnf install squid -y

    Linux下Apache服務(wù)如何部署和配置

    • 編輯配置文件:vim /etc/squid/squid.conf

    Linux下Apache服務(wù)如何部署和配置

    • 重啟squid服務(wù):systemctl restart squid
      原本沒有apache服務(wù)的主機(jī)172.25.254.244可以通過172.25.254.193的80端口(http)緩存數(shù)據(jù)

    Linux下Apache服務(wù)如何部署和配置

    11 apache 支持的語言

    php

    Linux下Apache服務(wù)如何部署和配置

    重啟apache服務(wù):systemctl restart httpd.service

    測試:http://172.25.254.144/index.php

    Linux下Apache服務(wù)如何部署和配置

    cgi

    mkdir /var/www/html/cgi

    vim /var/www/html/cgi/index.cgi

    cd /var/www/html/cgi

    chmod +x index.cgi

    ./var/www/html/cgi/index.cgi

    Linux下Apache服務(wù)如何部署和配置

    測試:http://172.25.254.144/cgi/index.cgi

    Linux下Apache服務(wù)如何部署和配置

    編輯虛擬主機(jī)的配置文件:vim /etc/httpd/conf.d/vhost.conf

    Linux下Apache服務(wù)如何部署和配置

    重啟服務(wù):systemctl restart httpd.service

    測試:http://172.25.254.144/cgi/index.cgi

    Linux下Apache服務(wù)如何部署和配置

    以上是“Linux下Apache服務(wù)如何部署和配置”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

    向AI問一下細(xì)節(jié)

    免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

    AI