你这个问题比较宽泛,“设 Apache”可以指安装、启动、配置虚拟主机、修改端口、配置防火墙等。下面我按CentOS 7/8 常见场景给你一个完整、实用的指南。
yum install httpd -y
dnf install httpd -y
systemctl start httpd
systemctl enable httpd
查看状态:
systemctl status httpd
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/ |
默认网站根目录 |
编辑:
vi /etc/httpd/conf/httpd.conf
修改:
DocumentRoot "/var/www/html"
改为你的目录,例如:
DocumentRoot "/data/www"
<Directory "/data/www">
AllowOverride All
Require all granted
</Directory>
重启:
systemctl restart httpd
在 /etc/httpd/conf.d/ 新建文件:
vi /etc/httpd/conf.d/www.example.com.conf
示例:
<VirtualHost *:80>
ServerName www.example.com
DocumentRoot /var/www/example
ErrorLog /var/log/httpd/example-error.log
CustomLog /var/log/httpd/example-access.log combined
</VirtualHost>
创建目录:
mkdir -p /var/www/example
echo "hello" > /var/www/example/index.html
重启:
systemctl restart httpd
编辑:
vi /etc/httpd/conf/httpd.conf
修改:
Listen 8080
防火墙:
firewall-cmd --permanent --add-port=8080/tcp
firewall-cmd --reload
# 查看错误日志
tail -f /var/log/httpd/error_log
# 检查配置语法
httpd -t
# 权限问题
chown -R apache:apache /var/www
可以告诉我,我可以给你:
如果你能说清楚你是 “怎么设 Apache 的什么”(安装?虚拟主机?HTTPS?端口?性能?),我可以直接给你精准配置。