SUSE Linux Apache HTTP Server 設定指南
Apache HTTP Server(apache2)是 SUSE 預設的 Web 伺服器,可用於提供網站與應用服務。本篇將介紹如何在 SUSE Linux 上安裝與設定 Apache,以 www.suse.tw
為範例,並涵蓋 SSL/HTTPS、Firewall、AppArmor 及日誌檢查方法。
目錄
安裝與啟動 Apache
- 安裝 Apache
sudo zypper install apache2 -y
- 啟動並設定開機自動啟動
sudo systemctl enable --now apache2
- 檢查服務狀態
sudo systemctl status apache2
若顯示
active (running)
,表示 Apache 已正常運行。
基本設定
- 主設定檔:
/etc/apache2/httpd.conf
- 虛擬主機目錄:
/etc/apache2/vhosts.d/
- 模組設定:在
/etc/apache2/sysconfig.d/
或LoadModule
指令於httpd.conf
中
範例調整:編輯 /etc/apache2/httpd.conf
,確認或新增:
ServerTokens Prod
ServerSignature Off
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
套用設定後:
sudo systemctl restart apache2
設定網站 www.suse.tw
- 建立網站目錄
sudo mkdir -p /srv/www/htdocs/suse.tw sudo chown -R wwwrun:www /srv/www/htdocs/suse.tw
- 建立首頁
echo "<h1>Welcome to SUSE TW</h1>" | sudo tee /srv/www/htdocs/suse.tw/index.html
-
新增虛擬主機設定
在/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>
- 測試設定並重新啟動
sudo apachectl configtest sudo systemctl restart apache2
啟用 SSL / HTTPS
- 安裝 Certbot
sudo zypper install certbot python3-certbot-apache -y
- 申請並安裝 Let's Encrypt 憑證
sudo certbot --apache -d www.suse.tw -d suse.tw
- 自動續期設定
sudo systemctl enable --now certbot-renew.timer
- SSL 虛擬主機檢查:
/etc/apache2/vhosts.d/suse.tw-le-ssl.conf
防火牆與 AppArmor 設定
- 開放 HTTP/HTTPS
sudo firewall-cmd --permanent --add-service=http sudo firewall-cmd --permanent --add-service=https sudo firewall-cmd --reload
- AppArmor 設定:允許 Apache 存取網站目錄
sudo aa-complain /usr/sbin/httpd sudo aa-autodep /srv/www/htdocs/suse.tw
日誌檢查與錯誤排除
- 主錯誤日誌:
sudo tail -f /var/log/apache2/error_log
- 主存取日誌:
sudo tail -f /var/log/apache2/access_log
- 網站專屬日誌:
sudo tail -f /var/log/apache2/suse.tw-error.log sudo tail -f /var/log/apache2/suse.tw-access.log
- 檢查 404:
sudo grep ' 404 ' /var/log/apache2/suse.tw-access.log
測試與驗證 Apache 伺服器
- 確認可訪問:
http://www.suse.tw
https://www.suse.tw - SSL 憑證狀態:
openssl s_client -connect www.suse.tw:443 -servername www.suse.tw
結語
本指南示範如何在 SUSE Linux 上安裝、設定 Apache,並以 www.suse.tw
為範例進行虛擬主機、SSL、Firewall、AppArmor 及日誌檢查。透過這些步驟,您可快速架設安全穩定的 Web 伺服器。