在 CentOS 里,Apache 通常叫 httpd(不是 apache2,apache2 是 Debian/Ubuntu 的叫法)。下面给你一份常用设置指南。
sudo yum install httpd -y
(CentOS 8 可用 dnf)
sudo systemctl start httpd
sudo systemctl enable httpd
查看状态:
sudo systemctl status httpd
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
/var/www/html
放一个测试页:
echo "Hello CentOS Apache" > /var/www/html/index.html
浏览器访问:
http://服务器IP
/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 "example site" > /var/www/example/index.html
重启 Apache:
sudo systemctl restart httpd
确保加载模块(默认一般已加载):
LoadModule rewrite_module modules/mod_rewrite.so
在目录中设置:
AllowOverride All
# 启动
systemctl start httpd
# 停止
systemctl stop httpd
# 重启
systemctl restart httpd
# 测试配置
httpd -t
/var/log/httpd/access_log
/var/log/httpd/error_log
如果你是想:
可以告诉我,我可以按你的具体需求写配置。