下面以 CentOS 7/8 为例,说明 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
curl http://localhost
或浏览器访问服务器 IP。
| 目录 / 文件 | 作用 |
|---|---|
/etc/httpd/ |
主配置目录 |
/etc/httpd/conf/httpd.conf |
主配置文件 |
/etc/httpd/conf.d/ |
自定义配置文件 |
/var/www/html/ |
默认网站根目录 |
/var/log/httpd/ |
日志目录 |
echo "Hello CentOS Apache" > /var/www/html/index.html
访问:
http://服务器IP
vim /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>
ErrorLog /var/log/httpd/example_error.log
CustomLog /var/log/httpd/example_access.log combined
</VirtualHost>
mkdir -p /var/www/example
echo "Example Site" > /var/www/example/index.html
httpd -t
systemctl restart httpd
ServerName 使用真实域名确认模块已加载(默认已加载):
httpd -M | grep rewrite
在目录中开启:
AllowOverride All
getenforce
setenforce 0
setsebool -P httpd_can_network_connect 1
semanage fcontext -a -t httpd_sys_content_t "/var/www/example(/.*)?"
restorecon -Rv /var/www/example
systemctl start httpd
systemctl stop httpd
systemctl restart httpd
httpd -t
ss -lntp | grep 80
如果你需要:
可以告诉我你的具体需求,我可以按实际使用场景一步一步讲。