下面给你一份**在 CentOS 上配置 Apache(httpd)**的实用步骤,适用于 CentOS 7 / 8 / Stream。
sudo yum update -y
(CentOS 8 / Stream 可用 dnf)
sudo yum 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 --reload
如需 HTTPS(443):
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
浏览器访问:
http://服务器IP
看到 Apache 测试页 即成功。
/var/www/html
创建测试页面:
echo "Hello CentOS Apache" > /var/www/html/index.html
sudo mkdir -p /var/www/example.com/html
sudo chown -R apache:apache /var/www/example.com
sudo vi /etc/httpd/conf.d/example.com.conf
内容示例:
<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/example.com/html
<Directory /var/www/example.com/html>
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
sudo systemctl restart httpd
# 重启
sudo systemctl restart httpd
# 重载配置
sudo systemctl reload httpd
# 查看错误日志
sudo tail -f /var/log/httpd/error_log
sudo setsebool -P httpd_can_network_connect 1
或修改文件上下文:
sudo semanage fcontext -a -t httpd_sys_content_t "/var/www/example.com(/.*)?"
sudo restorecon -Rv /var/www/example.com
如果你需要:
告诉我你的具体需求即可。