在 CentOS 上配置 Apache(通常称为 httpd)一般包括安装、启动、开机自启、防火墙配置、虚拟主机等步骤。下面以 CentOS 7 / 8 / Stream 为例说明。
sudo yum install httpd -y
(CentOS 8 / Stream 也可用 dnf)
sudo dnf install httpd -y
sudo systemctl start httpd
sudo systemctl enable httpd
查看状态:
sudo systemctl status httpd
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
浏览器访问:
http://服务器IP
看到 Apache Test Page 即成功。
| 路径 | 说明 |
|---|---|
/etc/httpd/ |
配置目录 |
/etc/httpd/conf/httpd.conf |
主配置文件 |
/etc/httpd/conf.d/ |
额外配置文件 |
/var/www/html/ |
默认网站根目录 |
echo "Hello CentOS Apache" > /var/www/html/index.html
刷新浏览器即可看到内容。
sudo mkdir -p /var/www/example.com/public_html
sudo echo "Example Site" > /var/www/example.com/public_html/index.html
sudo vi /etc/httpd/conf.d/example.conf
内容示例:
<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/example.com/public_html
<Directory /var/www/example.com/public_html>
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
sudo systemctl restart httpd
# 重启
sudo systemctl restart httpd
# 重载配置(不中断服务)
sudo systemctl reload httpd
# 测试配置是否正确
sudo apachectl configtest
sudo setenforce 0 # 临时关闭(测试用)
永久关闭需修改 /etc/selinux/config
sudo chown -R apache:apache /var/www
sudo chmod -R 755 /var/www
如果你有具体需求(如 HTTPS、PHP、反向代理、多端口),可以告诉我,我可以给你更针对性的配置。