下面按常见场景给你一个在 CentOS(7/8/Stream) 上配置 Apache(httpd) 的实用指南,从安装到常见配置。
yum install -y httpd
dnf install -y httpd
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
看到 Apache Test Page 即成功。
/var/www/html
示例:
echo "Hello Apache" > /var/www/html/index.html
mkdir -p /var/www/example.com/public_html
vi /etc/httpd/conf.d/example.com.conf
内容示例:
<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/example.com/public_html
ErrorLog /var/log/httpd/example.com-error.log
CustomLog /var/log/httpd/example.com-access.log combined
</VirtualHost>
systemctl restart httpd
编辑:
vi /etc/httpd/conf/httpd.conf
修改:
Listen 8080
重启:
systemctl restart httpd
<Directory "/var/www/html">
AllowOverride All
</Directory>
Options +Indexes
yum install -y mod_ssl
openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
-keyout /etc/pki/tls/private/apache.key \
-out /etc/pki/tls/certs/apache.crt
vi /etc/httpd/conf.d/ssl.conf
/var/log/httpd/access_log/var/log/httpd/error_logjournalctl -xe
tail -f /var/log/httpd/error_log
httpd -t # 检查配置语法
如果你有具体需求(如 PHP 支持、反向代理、多域名、安全加固),可以告诉我,我可以给你更针对性的配置。