在 CentOS 上管理 Apache(通常叫 httpd 服务),主要涉及安装、启动、停止、开机自启、配置、防火墙等。下面按常见场景给你一份实用指南(以 CentOS 7/8 为例,CentOS 6 略有不同)。
sudo yum install httpd -y
(CentOS 8 也可用 dnf)
sudo dnf install httpd -y
sudo systemctl start httpd
sudo systemctl stop httpd
sudo systemctl restart httpd
sudo systemctl reload httpd
sudo systemctl status httpd
sudo systemctl enable httpd
sudo systemctl disable httpd
如果外部访问不了,通常是防火墙问题。
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --reload
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
sudo firewall-cmd --list-all
| 作用 | 路径 |
|---|---|
| 主配置文件 | /etc/httpd/conf/httpd.conf |
| 额外配置 | /etc/httpd/conf.d/ |
| 网站根目录 | /var/www/html |
| 日志目录 | /var/log/httpd |
sudo apachectl configtest # 检查语法
sudo systemctl reload httpd
echo "Hello CentOS Apache" > /var/www/html/index.html
浏览器访问:
http://服务器IP
示例 /etc/httpd/conf.d/example.conf:
<VirtualHost *:80>
ServerName www.example.com
DocumentRoot /var/www/example
<Directory /var/www/example>
Require all granted
</Directory>
</VirtualHost>
创建目录:
sudo mkdir -p /var/www/example
重启:
sudo systemctl restart httpd
查看:
getenforce
允许 httpd 访问目录:
sudo setsebool -P httpd_read_user_content 1
或临时关闭(不推荐):
sudo setenforce 0
sudo tail -f /var/log/httpd/error_log
service httpd start
chkconfig httpd on
如果你有具体需求(比如:
可以直接告诉我,我可以给你更针对性的配置示例。