下面按**“从零在 CentOS 上配置 Apache(httpd)”的思路,给你一份实用、可操作**的说明,涵盖安装、启动、目录、虚拟主机、防火墙、常见配置。
sudo yum install httpd -y
(CentOS 8 也可用 dnf install httpd -y)
sudo systemctl start httpd
sudo systemctl enable httpd
systemctl status httpd
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
如果是云服务器,还要在安全组放行 80、443。
/var/www/html
默认访问:
http://服务器IP
测试:
echo "hello apache" > /var/www/html/index.html
/etc/httpd/conf/httpd.conf
常用参数:
DocumentRoot "/var/www/html"
Listen 80
ServerName localhost
修改后重载:
systemctl reload httpd
mkdir -p /var/www/example.com/public_html
echo "example.com" > /var/www/example.com/public_html/index.html
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>
ErrorLog /var/log/httpd/example.com-error.log
CustomLog /var/log/httpd/example.com-access.log combined
</VirtualHost>
httpd -t
systemctl reload httpd
sudo yum install php php-cli php-common -y
systemctl restart httpd
测试:
echo "<?php phpinfo(); ?>" > /var/www/html/info.php
访问:
http://IP/info.php
yum install certbot python3-certbot-apache -y
certbot --apache -d example.com
自动配置 80 → 443 跳转。
chown -R apache:apache /var/www
chmod -R 755 /var/www
setsebool -P httpd_read_user_content 1
httpd -t
我可以直接给你一套完整配置。