在 CentOS 上配置 Apache(通常叫 httpd)一般分为:安装 → 启动 → 配置虚拟主机/目录 → 防火墙 → 测试。下面按常见场景给你一套实用流程。
yum install httpd -y
dnf install httpd -y
systemctl start httpd
systemctl enable httpd
查看状态:
systemctl status httpd
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload
(如果是云服务器,还要在控制台安全组放行 80/443)
CentOS 下 Apache 默认根目录是:
/var/www/html
放一个测试页:
echo "Hello Apache" > /var/www/html/index.html
浏览器访问服务器 IP 即可。
主配置文件:
/etc/httpd/conf/httpd.conf
额外配置目录(推荐放虚拟主机):
/etc/httpd/conf.d/
日志位置:
/var/log/httpd/
例如配置 www.example.com
mkdir -p /var/www/example
echo "example site" > /var/www/example/index.html
vi /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>
ErrorLog /var/log/httpd/example-error.log
CustomLog /var/log/httpd/example-access.log combined
</VirtualHost>
httpd -t
systemctl reload httpd
确保:
AllowOverride All
并加载模块(默认一般已加载):
httpd -M | grep rewrite
yum install mod_ssl -y
vi /etc/httpd/conf.d/ssl.conf
或用 Let’s Encrypt:
yum install certbot python2-certbot-apache -y
certbot --apache
Require all granted 没配httpd -t 检查语法,再 reload如果你愿意,可以告诉我:
我可以直接给你一份可直接用的配置。