下面给你一份Linux 下 Apache(httpd)常见配置指南,适用于 CentOS / Rocky / Alma(yum/dnf) 和 Ubuntu / Debian(apt)。
sudo dnf install httpd -y
sudo systemctl start httpd
sudo systemctl enable httpd
sudo apt update
sudo apt install apache2 -y
sudo systemctl start apache2
sudo systemctl enable apache2
/etc/httpd/conf/httpd.conf/etc/httpd/conf.d//var/www/html/etc/apache2/apache2.conf/etc/apache2/sites-available//etc/apache2/sites-enabled//var/www/htmlListen 80
# Listen 8080
DocumentRoot "/var/www/html"
<Directory "/var/www/html">
AllowOverride All
Require all granted
</Directory>
修改后重启:
# CentOS
systemctl restart httpd
# Ubuntu
systemctl restart apache2
新建文件:
vim /etc/httpd/conf.d/example.conf
<VirtualHost *:80>
ServerName www.example.com
DocumentRoot /var/www/example
<Directory /var/www/example>
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
vim /etc/apache2/sites-available/example.conf
<VirtualHost *:80>
ServerName www.example.com
DocumentRoot /var/www/example
</VirtualHost>
启用站点:
a2ensite example.conf
systemctl reload apache2
a2enmod rewrite
systemctl restart apache2
vim /etc/httpd/conf/httpd.conf
确保:
LoadModule rewrite_module modules/mod_rewrite.so
.htaccess 示例:
RewriteEngine On
RewriteRule ^page/([0-9]+)$ index.php?id=$1
# CentOS
yum install mod_ssl
# Ubuntu
a2enmod ssl
<VirtualHost *:443>
ServerName www.example.com
DocumentRoot /var/www/example
SSLEngine on
SSLCertificateFile /etc/ssl/certs/example.crt
SSLCertificateKeyFile /etc/ssl/private/example.key
</VirtualHost>
# firewalld
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload
# ufw
ufw allow 80
ufw allow 443
# 查看错误日志
tail -f /var/log/httpd/error_log # CentOS
tail -f /var/log/apache2/error.log # Ubuntu
# 配置语法检查
apachectl configtest
如果你有具体需求(比如:PHP 环境、反向代理、多域名、LNMP 对比、Docker 里跑 Apache),可以直接告诉我,我可以给你针对性配置示例。