【笔记】Apache学习笔记

前言

Apache服务器Httpd学习笔记

MacOS

安装Apache

brew

1
brew install apache2
1
brew install httpd

启动停止重启服务

1
brew services start httpd
1
brew services stop httpd
1
brew services restart httpd

默认主页存放位置

  • /opt/homebrew/var/www/

Linux

安装Apache

CentOS

1
yum install httpd

启动停止重启服务

CentOS

1
systemctl start httpd
1
systemctl stop httpd

Debian

1
service httpd start
1
service httpd stop
1
service httpd restart

默认主页存放位置

  • /var/www/html/

配置文件

缺省配置

MacOS缺省配置

/opt/homebrew/etc/httpd/httpd.conf
1
2
3
4
5
6
7
8
ServerRoot "/opt/homebrew/opt/httpd"

Listen 8080

User _www
Group _www

DocumentRoot "/opt/homebrew/var/www"

Linux缺省配置

/etc/httpd/conf/httpd.config
1
2
3
4
5
6
7
8
ServerRoot "/etc/httpd"

Listen 80

User apache
Group apache

DocumentRoot "/var/www/html"

所有配置

httpd.config
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 工作目录
ServerRoot ""

# 默认端口
Listen 80

# 启动时使用的用户身份
User apache
Group apache

# 管理员邮箱
ServerAdmin [email protected]

# 站点根目录
DocumentRoot ""

# 引入其他子配置
IncludeOptional conf.d/*.conf

容器配置

httpd.config
1
2
3
4
5
6
7
8
9
10
11
12
# 相对于站点根目录的站点主页路径
DirectoryIndex index.html

<Directory "/var/www/html">
# 是否允许在找不到主页的时候以目录的形式展现网页
## 允许
Options Indexes FollowSymLinks
## 不允许
#Options FollowSymLinks
AllowOverride None
Require all granted
</Directory>

权限配置

允许所有
httpd.config
1
2
3
4
5
6
7
8
9
DirectoryIndex index.html

<Directory "/var/www/html">
Options Indexes FollowSymLinks
AllowOverride None

# 允许全部
Require all granted
</Directory>
拒绝所有
httpd.config
1
2
3
4
5
6
7
8
9
10
11
DirectoryIndex index.html

<Directory "/var/www/html">
Options Indexes FollowSymLinks
AllowOverride None

<RequireALL>
# 拒绝全部
Require all denied
<RequireALL>
</Directory>
允许部分(白名单)
通过IP进行筛选
httpd.config
1
2
3
4
5
6
7
8
9
10
11
12
13
DirectoryIndex index.html

<Directory "/var/www/html">
Options Indexes FollowSymLinks
AllowOverride None

<RequireALL>
# 指定允许的IP
Require ip 192.168.1.1
# 其他的全部拒绝
Require all denied
<RequireALL>
</Directory>
通过域名进行筛选
httpd.config
1
2
3
4
5
6
7
8
9
10
11
12
13
DirectoryIndex index.html

<Directory "/var/www/html">
Options Indexes FollowSymLinks
AllowOverride None

<RequireALL>
# 指定允许的域名
Require host example.com
# 其他的全部拒绝
Require all denied
<RequireALL>
</Directory>
拒绝部分(黑名单)
通过IP进行筛选
httpd.config
1
2
3
4
5
6
7
8
9
10
11
12
13
DirectoryIndex index.html

<Directory "/var/www/html">
Options Indexes FollowSymLinks
AllowOverride None

<RequireALL>
# 指定允许的IP
Require not ip 127.0.0.1
# 其他的全部允许
Require all granted
<RequireALL>
</Directory>
通过域名进行筛选
httpd.config
1
2
3
4
5
6
7
8
9
10
11
12
13
DirectoryIndex index.html

<Directory "/var/www/html">
Options Indexes FollowSymLinks
AllowOverride None

<RequireALL>
# 指定允许的域名
Require not host example.com
# 其他的全部允许
Require all granted
<RequireALL>
</Directory>

完成

参考文献

哔哩哔哩——千锋教育网络安全学院
CSDN——灵魂自由的忙人