📖 Apache 伺服器架設

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

學習如何設定 Apache

Apache HTTP Server(httpd)是一款流行的 Web 伺服器,可用於提供網站、應用服務等。本篇將介紹如何在 Fedora 上安裝與設定 Apache,並以 www.fedora.tw 為範例,包含 HTTPS/SSL 設定與日誌(Log)檢查方法。


目錄


安裝與啟動 Apache

安裝 Apache

sudo dnf install httpd -y

啟動並設定開機自動啟動

sudo systemctl enable --now httpd

檢查 Apache 服務狀態

sudo systemctl status httpd

如果 Apache 正常運行,應顯示 active (running)


設定 Apache 伺服器

Apache 的主設定檔位於 /etc/httpd/conf/httpd.conf,網站配置檔案通常位於 /etc/httpd/conf.d/

編輯主設定檔 /etc/httpd/conf/httpd.conf

確保包含以下內容:

ServerTokens Prod
ServerSignature Off
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5

重啟 Apache 以套用變更:

sudo systemctl restart httpd

設定網站 www.fedora.tw

建立網站目錄

sudo mkdir -p /var/www/html/fedora.tw
sudo chown -R apache:apache /var/www/html/fedora.tw

建立首頁文件

echo "<h1>Welcome to Fedora TW</h1>" | sudo tee /var/www/html/fedora.tw/index.html

新增網站設定檔 /etc/httpd/conf.d/fedora.tw.conf

<VirtualHost *:80>
    ServerName www.fedora.tw
    DocumentRoot /var/www/html/fedora.tw
    DirectoryIndex index.html

    <Directory "/var/www/html/fedora.tw">
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog /var/log/httpd/fedora.tw-error.log
    CustomLog /var/log/httpd/fedora.tw-access.log combined
</VirtualHost>

測試配置並重新啟動 Apache

sudo apachectl configtest
sudo systemctl restart httpd

啟用 SSL / HTTPS

安裝 Certbot 以取得免費 SSL 憑證

sudo dnf install certbot python3-certbot-apache -y

申請 Let's Encrypt SSL 憑證

sudo certbot --apache -d www.fedora.tw -d fedora.tw

系統會要求輸入電子郵件並同意 Let's Encrypt 條款,然後自動設定 SSL。

讓憑證自動續期

sudo systemctl enable --now certbot-renew.timer

或手動測試續約:

sudo certbot renew --dry-run

確保 HTTPS 設定在 /etc/httpd/conf.d/fedora.tw-le-ssl.conf

<VirtualHost *:443>
    ServerName www.fedora.tw

    DocumentRoot /var/www/html/fedora.tw
    DirectoryIndex index.html

    SSLEngine on
    SSLCertificateFile /etc/letsencrypt/live/www.fedora.tw/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/www.fedora.tw/privkey.pem

    <Directory "/var/www/html/fedora.tw">
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog /var/log/httpd/fedora.tw-ssl-error.log
    CustomLog /var/log/httpd/fedora.tw-ssl-access.log combined
</VirtualHost>

重新啟動 Apache

sudo apachectl configtest
sudo systemctl restart httpd

防火牆與 SELinux 設定

開放 HTTP 與 HTTPS 連接埠

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

設定 SELinux

如果 SELinux 啟用,請允許 Apache 存取網站目錄:

sudo setsebool -P httpd_can_network_connect 1
sudo chcon -R -t httpd_sys_content_t /var/www/fedora.tw

日誌檢查與錯誤排除

檢查 Apache 錯誤日誌

sudo tail -f /var/log/httpd/error_log

檢查網站存取日誌

sudo tail -f /var/log/httpd/access_log

檢查特定網站的日誌

sudo tail -f /var/log/httpd/fedora.tw-error.log
sudo tail -f /var/log/httpd/fedora.tw-access.log

找出錯誤請求

sudo grep "error" /var/log/httpd/error_log

找出 404(未找到的請求)

sudo grep " 404 " /var/log/httpd/fedora.tw-access.log

找出頻繁存取的 IP

sudo awk '{print $1}' /var/log/httpd/fedora.tw-access.log | sort | uniq -c | sort -nr | head -10

測試與驗證 Apache 伺服器

確認網站可以存取

打開瀏覽器,訪問:

http://www.fedora.tw
https://www.fedora.tw

檢查 SSL 憑證狀態

openssl s_client -connect www.fedora.tw:443 -servername www.fedora.tw

或使用 SSL Labs 檢查:

https://www.ssllabs.com/ssltest/


結語

本篇介紹了如何在 Fedora 上安裝 Apache,並以 www.fedora.tw 為範例,說明了如何設定網站、啟用 SSL 憑證、開放防火牆、配置 SELinux,並提供日誌檢查與錯誤排除的方法。透過這些步驟,您可以成功架設一個安全的 Apache 伺服器。

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