📖 Apache 伺服器架設

分類:伺服器管理 | 作者:pake | 發布時間:2024-09-04 14:19

SUSE Linux Apache HTTP Server 設定指南

Apache HTTP Server(apache2)是 SUSE 預設的 Web 伺服器,可用於提供網站與應用服務。本篇將介紹如何在 SUSE Linux 上安裝與設定 Apache,以 www.suse.tw 為範例,並涵蓋 SSL/HTTPS、Firewall、AppArmor 及日誌檢查方法。


目錄


安裝與啟動 Apache

  1. 安裝 Apache
    sudo zypper install apache2 -y
  2. 啟動並設定開機自動啟動
    sudo systemctl enable --now apache2
  3. 檢查服務狀態
    sudo systemctl status apache2

    若顯示 active (running),表示 Apache 已正常運行。


基本設定

範例調整:編輯 /etc/apache2/httpd.conf,確認或新增:

ServerTokens Prod
ServerSignature Off
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5

套用設定後:

sudo systemctl restart apache2

設定網站 www.suse.tw

  1. 建立網站目錄
    sudo mkdir -p /srv/www/htdocs/suse.tw
    sudo chown -R wwwrun:www /srv/www/htdocs/suse.tw
  2. 建立首頁
    echo "<h1>Welcome to SUSE TW</h1>" | sudo tee /srv/www/htdocs/suse.tw/index.html
  3. 新增虛擬主機設定
    /etc/apache2/vhosts.d/suse.tw.conf 加入:

    <VirtualHost *:80>
     ServerName www.suse.tw
     ServerAlias suse.tw
     DocumentRoot "/srv/www/htdocs/suse.tw"
     DirectoryIndex index.html
    
     <Directory "/srv/www/htdocs/suse.tw">
       AllowOverride All
       Require all granted
     </Directory>
    
     ErrorLog /var/log/apache2/suse.tw-error.log
     CustomLog /var/log/apache2/suse.tw-access.log combined
    </VirtualHost>
  4. 測試設定並重新啟動
    sudo apachectl configtest
    sudo systemctl restart apache2

啟用 SSL / HTTPS

  1. 安裝 Certbot
    sudo zypper install certbot python3-certbot-apache -y
  2. 申請並安裝 Let's Encrypt 憑證
    sudo certbot --apache -d www.suse.tw -d suse.tw
  3. 自動續期設定
    sudo systemctl enable --now certbot-renew.timer
  4. SSL 虛擬主機檢查/etc/apache2/vhosts.d/suse.tw-le-ssl.conf

防火牆與 AppArmor 設定

  1. 開放 HTTP/HTTPS
    sudo firewall-cmd --permanent --add-service=http
    sudo firewall-cmd --permanent --add-service=https
    sudo firewall-cmd --reload
  2. AppArmor 設定:允許 Apache 存取網站目錄
    sudo aa-complain /usr/sbin/httpd
    sudo aa-autodep /srv/www/htdocs/suse.tw

日誌檢查與錯誤排除


測試與驗證 Apache 伺服器


結語

本指南示範如何在 SUSE Linux 上安裝、設定 Apache,並以 www.suse.tw 為範例進行虛擬主機、SSL、Firewall、AppArmor 及日誌檢查。透過這些步驟,您可快速架設安全穩定的 Web 伺服器。

⬅ 上一篇 下一篇 ➡
🔙 返回 伺服器管理 📚 返回教學列表 🏠 返回首頁