建立 Apache 在 Linux 伺服器上安裝 apache2/httpd 軟體包,啟用並啟動該服務,在防火牆中允許 80/443 端口,為您的網域建立虛擬主機,檢查配置是否存在語法錯誤,並使用 Let's Encrypt 啟用 HTTPS。不同發行版的步驟略有不同。
本指南適合初學者,介紹如何進行設置 Apache 在 Linux 伺服器上,您將學習安裝、設定、保護和最佳化的具體步驟。 Apache 在 Ubuntu/Debian 和 CentOS/RHEL/AlmaLinux/Rocky Linux 等主流發行版上,我都會提供 (httpd) 服務。身為資深主機代管專家,我也會分享一些實用技巧、安全加固方法以及效能調校技巧,幫助您經營生產級網站。
所需物品(先決條件)開始之前,請確認以下事項:
一台運行基於 Ubuntu/Debian 或 CentOS/RHEL 發行版的 Linux 伺服器(VPS、雲端伺服器或獨立伺服器)root 或 sudo 訪問註冊域名(可選,但建議)防火牆存取權限,開放連接埠 80 (HTTP) 和 443 (HTTPS)小包裝 manage已準備好:Ubuntu/Debian 使用 apt,基於 RHEL 的系統使用 dnf/yumApache Linux 系統中:重要的命名差異跨分佈, Apache 使用不同的軟體包和服務名稱:
Ubuntu/Debian:軟體包 apache2, 服務 apache2,配置 /etc/apache2/RHEL/CentOS/AlmaLinux/Rocky:軟體包 httpd, 服務 httpd,配置 /etc/httpd/運行命令時請記住這些要點。 編輯設定檔.
逐步安裝指南 ApacheUbuntu的/ Debian的sudo apt update
sudo apt install -y apache2
sudo systemctl enable --now apache2
sudo ufw allow "Apache Full" # enables 80 and 443
sudo systemctl status apache2
curl -I http://
sudo systemctl enable --now httpd
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
sudo systemctl status httpd
curl -I http://
了解你的鑰匙 Apache 檔案和目錄Ubuntu的/ Debian的: /etc/apache2/apache2.conf, /etc/apache2/ports.conf, /etc/apache2/sites-available/, /etc/apache2/sites-enabled/登入 /var/log/apache2/基於RHEL的: /etc/httpd/conf/httpd.conf, /etc/httpd/conf.d/登入 /var/log/httpd/私人訂製 management(適用於兩個家庭):
sudo systemctl status apache2|httpd
sudo systemctl reload apache2|httpd
sudo systemctl restart apache2|httpd建立您的第一個虛擬主機(網站)1)準備文檔根目錄和權限sudo mkdir -p /var/www/example.com/public_html
echo "
Hello from Apache
" | sudo tee /var/www/example.com/public_html/index.html# Ownership differs by distro:
# Ubuntu/Debian user is "www-data", RHEL-based user is "apache"
# Ubuntu/Debian:
sudo chown -R www-data:www-data /var/www/example.com
# RHEL-based:
# sudo chown -R apache:apache /var/www/example.com2)新增虛擬主機配置Ubuntu/Debian(sites-available + a2ensite):
sudo nano /etc/apache2/sites-available/example.com.conf
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com/public_html
AllowOverride All
Require all granted
Options -Indexes
ErrorLog ${APACHE_LOG_DIR}/example_error.log
CustomLog ${APACHE_LOG_DIR}/example_access.log combined
sudo a2ensite example.com.conf
sudo a2enmod rewrite
sudo apache2ctl configtest
sudo systemctl reload apache2RHEL/CentOS/AlmaLinux/Rocky(conf.d):
sudo nano /etc/httpd/conf.d/example.com.conf
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com/public_html
AllowOverride All
Require all granted
Options -Indexes
ErrorLog /var/log/httpd/example_error.log
CustomLog /var/log/httpd/example_access.log combined
sudo apachectl -t
sudo systemctl reload httpd如果 SELinux 正在強制執行,且您的文件根目錄位於預設路徑之外,請設定正確的上下文:
# RHEL-based SELinux example for a custom docroot:
sudo semanage fcontext -a -t httpd_sys_content_t "/var/www/example.com(/.*)?"
sudo restorecon -Rv /var/www/example.com使用 Let's Encrypt 啟用 HTTPS(免費) SSL)Ubuntu的/ Debian的sudo apt install -y certbot python3-certbot-apache
sudo certbot --apache -d example.com -d www.example.com
# Test auto-renewal
sudo certbot renew --dry-runRHEL/CentOS/AlmaLinux/Rocky# Enable EPEL if needed (RHEL/CentOS):
sudo dnf install -y epel-release
sudo dnf install -y certbot python3-certbot-apache
sudo certbot --apache -d example.com -d www.example.com
sudo certbot renew --dry-runCertbot 新增 SSL 虛擬主機,安裝證書,並配置 HTTP 到 HTTPS 重定向。證書透過 systemd 定時器自動續期。
基本安全加固隱藏伺服器詳情並停用列表# Ubuntu/Debian: edit /etc/apache2/conf-available/security.conf (or apache2.conf)
# RHEL-based: edit /etc/httpd/conf.d/security.conf (create if missing)
ServerTokens Prod
ServerSignature Off
# In each
Options -Indexes防火牆、權限和 SELinux只保持連接埠 80/443 開放;除非必要,否則阻止其他連接埠。對網站根目錄使用最小權限原則。檔案權限:644,目錄權限:755。在 RHEL 系統中,對於出站連線(例如,到應用程式伺服器的連線),允許: sudo setsebool -P httpd_can_network_connect 1.安全性模組和 HTTPS 最佳實踐考慮使用 ModSecurity WAF:Ubuntu sudo apt install libapache2-mod-security2RHEL sudo dnf install mod_security.完全使用 HTTPS 時,請使用強 TLS 加密演算法並啟用 HSTS。定期打補丁: sudo apt upgrade or sudo dnf upgrade.針對真實流量的效能調優選擇合適的MPM對於大多數現代網站而言, 項目 MPM 的可擴充性優於 prefork的如果你運行 PHP將事件 MPM 與 PHP-FPM(不是 mod_php)。
# Ubuntu/Debian:
sudo a2dismod mpm_prefork
sudo a2enmod mpm_event proxy_fcgi setenvif
sudo a2enconf php*-fpm # choose your installed PHP-FPM version
sudo systemctl reload apache2
# RHEL-based:
sudo dnf install -y php-fpm
sudo systemctl enable --now php-fpm
# Example (in your vhost) to pass PHP via FPM socket:
#
# SetHandler "proxy:unix:/run/php-fpm/www.sock|fcgi://localhost/"
# 壓縮和快取# Enable modules (Ubuntu/Debian):
sudo a2enmod deflate headers expires brotli
sudo systemctl reload apache2
# Example rules (add inside vhost or a conf file):
AddOutputFilterByType DEFLATE text/html text/plain text/css application/javascript application/json
# If Brotli installed:
# AddOutputFilterByType BROTLI_COMPRESS text/html text/plain text/css application/javascript application/json
# Caching static assets:
ExpiresActive On
ExpiresByType image/webp "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType text/css "access plus 7 days"
ExpiresByType application/javascript "access plus 7 days"連接限制和 KeepAlive調音 RAM/CPU作為 2-4 GB VPS 的初始配置:
# event MPM example (Ubuntu: /etc/apache2/mods-available/mpm_event.conf)
# RHEL: in /etc/httpd/conf.modules.d/ prefixed file or main httpd.conf
StartServers 2
MinSpareThreads 25
MaxSpareThreads 75
ThreadLimit 64
ThreadsPerChild 25
MaxRequestWorkers 150
MaxConnectionsPerChild 0
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5使用真實流量進行測試並進行調整。每次更改後務必重新加載並監控。 內存使用情況 防止交換。
日誌、監控和維護訪問日誌: /var/log/apache2/access.log or /var/log/httpd/access_log錯誤日誌: /var/log/apache2/error.log or /var/log/httpd/error_log服務日誌: journalctl -u apache2 or journalctl -u httpd尾部即時日誌: sudo tail -f /var/log/apache2/error.log日誌輪替:由 logrotate 處理;請驗證設定。 /etc/logrotate.d/為了取得流量洞察,像 GoAccess 這樣的工具可以提供來自存取日誌的即時報告,而不會增加額外的開銷。 Apache.
解決常見問題連接埠已被佔用: sudo ss -tulpn | grep :80 尋找衝突。停止其他服務或更改 Apache“ Listen 移入 ports.conf (Ubuntu)或 httpd.conf (RHEL)。403 禁止存取:檢查檔案權限/所有權和 Apache
# Ubuntu/Debian:
sudo apt install -y php php-fpm php-mysql mariadb-server
# RHEL-based:
sudo dnf install -y php php-fpm php-mysqlnd mariadb-server
sudo systemctl enable --now mariadb比較喜歡 PHP-FPM 與 Apache's event MPM 比 mod_php 有更好的並發性。
何時使用 YouStable 對於 Apache 託管如果你運行的是生產工作負載,那麼一個可靠的平台至關重要。 YouStable的 SSD 驅動 VPS 和專用伺服器可提供穩定的 I/O 和 DDoS 防護, IPv6 支援和免費的 Let's Encrypt SSL我們的工程師可以進行預硬化處理。 Apache, 配置 PHP-FPM,並設定自動備份-這樣您就可以專注於您的網站,而不是伺服器救火。
按照上述步驟操作,您現在就知道如何設定了。 Apache 在 Linux 伺服器上從安裝到 SSL安全性和調優。有了堅實的基礎和合適的託管服務, Apache 能夠為快速、安全且可擴展的網站提供支援。
常見問題:如何設定 Apache 在 Linux 伺服器上Is Apache or Nginx 更適合在 Linux 系統上使用 WordPress 嗎?兩者都運作良好。 Apache 提供靈活的 .htaccess 和深度模組支援。 Nginx 擅長處理靜態資源和高並發性。常見的生產模式是 Nginx 作為反向代理位於前面 Apache+PHP-FPM,或 Apache 僅支援事件管理模式 (MPM) 和快取。請根據您的技術堆疊和團隊專業知識進行選擇。
我該如何改變? Apache預設連接埠是 80 嗎?Ubuntu/Debian:編輯 /etc/apache2/ports.conf 以及與之相符的 VirtualHost,例如: Listen 8080 以及
如何在同一台伺服器上託管多個網站? Apache 服務器?使用虛擬主機。為每個網域建立單獨的文檔根目錄和虛擬主機設定檔。在 Ubuntu/Debian 系統上,將設定檔放置在… sites-available 並啟用 a2ensite在基於 RHEL 的系統中,新增一個 .conf 每個站點 /etc/httpd/conf.d/。觀點 DNS A/AAAA 記錄指向您伺服器的 IP 位址。
如何在網站上自動續訂 Let's Encrypt 憑證? Apache?Certbot 預設會設定一個 systemd 定時器。請使用以下命令驗證: systemctl list-timers | grep certbot 並進行一次預演: sudo certbot renew --dry-run請確保您的防火牆允許 TCP 80/443 端口,並且您的網域指向該伺服器,以便 HTTP-01/ALPN 可以進行驗證。
在哪 Apache Linux 系統上的設定檔?Ubuntu/Debian 將配置儲存在 /etc/apache2/ (網站在 sites-available/sites-enabled) 帶有日誌 /var/log/apache2/RHEL/CentOS/AlmaLinux/Rocky 使用 /etc/httpd/ (虛擬主機) conf.d) 帶有日誌 /var/log/httpd/主要文件有: apache2.conf or httpd.conf.