備註:

安裝 Apache Web Server

簡介

Apache 自 1993 年起即處於主動開發中,而隨著時間已成為全球最受歡迎的 Web 伺服器之一。Apache Web 伺服器為 LAMP (Linux、Apache、Oracle MySQL 和 Perl/PHP) 軟體堆疊的關鍵元件,並繼續在今日廣泛使用。

Apache Web 伺服器可直接從 Oracle Linux 8 或更新版本的「應用程式串流」儲存區域取得,因此部署和設定相當簡單。

必要條件

目標

本教學課程結束後,您應該能夠:

安裝與配置 Web 伺服器套裝軟體

  1. 安裝 httpd 套裝軟體及其相依性。

    sudo dnf install httpd
    
  2. 啟用並啟動 httpd 服務。

    此指令可啟用並啟動 httpd 服務供立即使用,也會在系統重新啟動之後自動啟動服務:

    sudo systemctl enable --now httpd.service
    

    若要檢查服務狀態,請輸入:

    sudo systemctl status httpd
    
  3. (選擇性) 如果您使用的是自訂防火牆設定檔或 Oracle Cloud Infrastructure 執行處理,請開啟 Apache Web 服務的防火牆連接埠 (80)。

    下列指令可啟用 Apache Web 服務的防火牆連接埠,並重新載入預設防火牆服務:

    sudo firewall-cmd --add-service=http --permanent
    sudo firewall-cmd --reload
    
  4. 測試部署。

    使用您的 Web 瀏覽器,移至系統的網域名稱或 IP 位址,例如 http://myserver.example.com.Apache Web 伺服器會開啟預設的測試頁面。

設定 Apache

若要變更 Web 伺服器的根路徑,請勿直接編輯 /etc/httpd/conf/httpd.conf 檔案。請改為使用偏好的方法在 /etc/httpd/conf.d 目錄中建立網站特定的配置檔案。

在下列範例中,建立 /etc/httpd/conf.d/example.com.conf 檔案以包含虛擬主機的組態。

  1. /etc/httpd/conf.d/example.com.conf 中新增下列資訊來建立虛擬主機:

    聆聽 *:80

    <VirtualHost *:80>

    ServerName example.com
    ServerAlias www.example.com

    DocumentRoot /var/www/example.com/html/

    ErrorLog /var/log/httpd/example.com_error.log
    CustomLog /var/log/httpd/example.com_access.log 已結合

    </VirtualHost>

    ServerNameServerAlias 可以是可用於存取服務的主機名稱、網域名稱或 IP 位址。

  2. 建立文件根資料夾。

    根資料夾代管 Apache 提供給瀏覽器的網頁。

    sudo mkdir -p /var/www/example.com/html
    sudo echo "example.com" > /var/www/example.com/html/index.html
    sudo chown -R apache:apache /var/www/example.com/html
    

    在以強制模式啟用 SELinux 且未在 /var/www directory 內提供頁面的系統上,您必須將正確的安全相關資訊環境套用至 DocumentRoot 目錄。例如,若要從 /mnt/example.com 目錄提供網頁服務,請輸入:

    sudo semanage fcontext -a -t httpd_sys_content_t "/mnt/example.com(/.*)?"
    sudo restorecon -Rv /mnt/example.com/
    
  3. 套用您的組態。

    sudo systemctl restart httpd
    

保護 Web 服務

最佳做法是,使用 HTTPS 保護 Web 瀏覽器與 Apache 伺服器之間的所有通訊。為了安全設定,需要 TLS 憑證。

  1. 設定您的 TLS/SSL 憑證。

注意:
Oracle 強烈建議您使用已由外部憑證授權機構 (CA) 簽署的 TLS 憑證。如需詳細資訊,請參閱 Oracle Linux:Managing Certificates and Public Key Infrastructure

  1. 安裝 Apache 的 HTTPS 附加元件。

    sudo dnf install mod_ssl
    sudo systemctl restart httpd
    
  2. 更新 Apache 配置。例如,在 /etc/httpd/conf.d/example.com.conf 中,新增下列組態:

    聆聽 *:443

    <VirtualHost *:443>

    ServerName example.com
    ServerAlias www.example.com

    SSLCertificateFile 上的 SSLEngine /etc/pki/tls/private/certificate.crt
    SSLCertificateKeyFile /etc/pki/tls/private/private.key

    DocumentRoot /var/www/example.com/html/

    ErrorLog /var/log/httpd/example.com_error.log
    CustomLog /var/log/httpd/example.com_access.log 已結合

    </VirtualHost>

  3. 將 HTTP 要求重新導向至 HTTPS。

    將現有的 HTTP VirtualHost 組態取代為重新導向,如下所示:

    <VirtualHost *:80>

    ServerName example.com
    ServerAlias www.example.com

    重導 "/" "https://example.com/"

    </VirtualHost>

  4. 重新啟動 Apache 服務以載入新的配置。

    sudo systemctl restart httpd
    
  5. (可選擇) 配置防火牆。

    這些指令會啟用 Apache HTTPS 服務的防火牆連接埠 443,並重新載入預設防火牆服務:

    sudo firewall-cmd --add-service=https --permanent
    sudo firewall-cmd --reload
    

其他學習資源

探索 docs.oracle.com/learn 上的其他實驗室,或是存取更多免費學習內容至 Oracle Learning YouTube 通道。此外,瀏覽 education.oracle.com/learning-explorer 以成為 Oracle Learning Explorer。

如需產品文件,請瀏覽 Oracle Help Center