在 CentOS 上配置 Apache(注意:CentOS 里叫 httpd,不是 apache2,apache2 是 Debian/Ubuntu 的叫法)可以按下面步骤来。
yum install -y httpd
dnf install -y httpd
systemctl start httpd
systemctl enable httpd
查看状态:
systemctl status httpd
如果启用了 firewalld:
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload
浏览器访问:
http://服务器IP
看到 Testing 123… 页面说明成功。
| 文件/目录 | 说明 |
|---|---|
/etc/httpd/conf/httpd.conf |
主配置文件 |
/etc/httpd/conf.d/ |
额外配置文件目录 |
/var/www/html/ |
默认网站根目录 |
/var/log/httpd/ |
日志目录 |
vim /etc/httpd/conf/httpd.conf
找到:
DocumentRoot "/var/www/html"
<Directory "/var/www/html">
改为:
DocumentRoot "/data/www"
<Directory "/data/www">
AllowOverride All
Require all granted
</Directory>
mkdir -p /data/www
echo "Hello CentOS Apache" > /data/www/index.html
systemctl restart httpd
例如配置 www.example.com:
vim /etc/httpd/conf.d/www.example.com.conf
内容:
<VirtualHost *:80>
ServerName www.example.com
DocumentRoot /data/www
<Directory /data/www>
AllowOverride All
Require all granted
</Directory>
ErrorLog /var/log/httpd/www-error.log
CustomLog /var/log/httpd/www-access.log combined
</VirtualHost>
重启:
systemctl restart httpd
确保配置中有:
AllowOverride All
chmod -R 755 /data/www
setenforce 0 # 临时关闭
永久关闭(不推荐生产):
vim /etc/selinux/config
SELINUX=disabled
systemctl start httpd
systemctl stop httpd
systemctl restart httpd
systemctl status httpd
httpd -t # 检查配置文件语法
如果你需要 配置 HTTPS(SSL)、PHP 环境、Apache + PHP + MySQL,或者 CentOS 7/8 的具体版本,可以继续问我。