在 CentOS 中配置 Apache(通常叫 httpd)一般包括:安装、启动、开机自启、配置文件位置、虚拟主机等。下面按常见用法给你一个完整说明(以 CentOS 7/8 为例,CentOS 6 略有不同)。
# CentOS 7/8
sudo yum install httpd -y
(CentOS 8 也可用 dnf)
sudo dnf install httpd -y
sudo systemctl start httpd
sudo systemctl enable httpd
查看状态:
sudo systemctl status httpd
如果开了 firewalld:
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
浏览器访问:
http://服务器IP
看到 Apache 测试页 说明成功。
网页根目录默认是:
/var/www/html
/etc/httpd/conf/httpd.conf
常用参数:
DocumentRoot "/var/www/html"
<Directory "/var/www/html">
AllowOverride All
</Directory>
修改后重启:
sudo systemctl restart httpd
sudo 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>
</VirtualHost>
sudo mkdir -p /var/www/example
echo "hello" > /var/www/example/index.html
sudo systemctl restart httpd
在 <Directory> 中:
AllowOverride All
并确保模块已加载(默认一般已加载):
LoadModule rewrite_module modules/mod_rewrite.so
systemctl start httpd
systemctl stop httpd
systemctl restart httpd
systemctl reload httpd
如果你能告诉我:
我可以给你更精确的配置示例。