Apache

fansichao 2021-10-23 16:16:35
Categories: Tags:

Apache 简介

Apache 是世界使用排名第一的 Web 服务器软件。它可以运行在几乎所有广泛使用的计算机平台上,由于其跨平台和安全性被广泛使用,是最流行的 Web 服务器端软件之一。它快速、可靠并且可通过简单的 API 扩充,将 Perl/Python 等解释器编译到服务器中。同时 Apache 音译为阿帕奇,是北美印第安人的一个部落,叫阿帕奇族,在美国的西南部。也是一个基金会的名称、一种武装直升机等等. 百度百科 Apache

Apacheweb 服务器软件拥有以下特性:

Apache 功能

TODO Apache 功能 后置,暂无需求

Apache 参数详解

参考链接: centos7 部署 Apache 服务器

其他

Centos7-Apache 配置示例

步骤 1 配置 scfan.conf 配置文件:

1
2
3
4
5
6
7
8
9
10
11
12
# /etc/httpd/conf.d/scfan.conf
User scfan
Group scfan

<VirtualHost *:80>
DocumentRoot "/home/scfan/project/FISAMS/branches/branch_scfan/src/web/fdm"
<Directory "/home/scfan/project/FISAMS/branches/branch_scfan/src/web/fdm">
options Indexes MultiViews
AllowOverride all
Allow from all
</Directory>
</VirtualHost>

步骤 2 重命名 welcome.conf:

1
2
# 重命名后不会再显示 testing 123 而是会显示详细报错
mv /etc/httpd/conf.d/welcome.conf /etc/httpd/conf.d/welcome.conf.bak

步骤 3 重启服务:

1
/bin/systemctl restart httpd.service

报错: StackOverFlow - You don’t have permission to access / on this server.,后续步骤已解决此问题,此处可供参考

步骤 4 目录授权:

1
2
3
4
5
6
7
8
9
10
# 采用单层授权, 避免整个目录文件都权限被变更
[root@fdm conf]# chmod +x /home/scfan/project/FISAMS/branches/branch_scfan/src/web/fdm
[root@fdm conf]# chmod +x /home/scfan/project/FISAMS/branches/branch_scfan/src/web/
[root@fdm conf]# chmod +x /home/scfan/project/FISAMS/branches/branch_scfan/src/
[root@fdm conf]# chmod +x /home/scfan/project/FISAMS/branches/branch_scfan/
[root@fdm conf]# chmod +x /home/scfan/project/FISAMS/branches/
[root@fdm conf]# chmod +x /home/scfan/project/FISAMS/
[root@fdm conf]# chmod +x /home/scfan/project
[root@fdm conf]# chmod +x /home/scfan
[root@fdm conf]# chmod +x /home

步骤 5 修改 配置文件 /etc/httpd/conf/httpd.conf:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 配置监听端口 对应 scfan.conf 配置文件
41 #Listen 12.34.56.78:80
42 Listen 80
43 Listen 8888
44 LimitRequestLine 40940 # 限制 Apache 请求长度,根据实际需要增加

# 注释如下代码
105 # <Directory />
106 # AllowOverride none
107 # Require all denied
108 # </Directory>

# 解决方法
vi /etc/httpd/conf/httpd.conf 加入一句 ServerName localhost:80

步骤 6 重启服务:

1
/bin/systemctl restart httpd.service
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 查看Apache服务状态时,显示 Could not reliably determine the server's fully qualified domain name
[root@41d129b3de9a ~]# /bin/systemctl status httpd.service
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
Active: active (running) since Fri 2020-04-10 11:28:31 UTC; 3s ago
Docs: man:httpd(8)
man:apachectl(8)
Process: 13192 ExecStop=/bin/kill -WINCH ${MAINPID} (code=exited, status=0/SUCCESS)
Main PID: 13199 (httpd)
Status: "Processing requests..."
CGroup: /docker/41d129b3de9ac86439cf7cf46d42d061b26ab31c138520d2090f9d5a9b1f0757/system.slice/httpd.service
├─13199 /usr/sbin/httpd -DFOREGROUND
├─13200 /usr/sbin/httpd -DFOREGROUND
├─13201 /usr/sbin/httpd -DFOREGROUND
├─13202 /usr/sbin/httpd -DFOREGROUND
├─13203 /usr/sbin/httpd -DFOREGROUND
└─13204 /usr/sbin/httpd -DFOREGROUND
‣ 13199 /usr/sbin/httpd -DFOREGROUND

Apr 10 11:28:31 41d129b3de9a systemd[1]: Starting The Apache HTTP Server...
Apr 10 11:28:31 41d129b3de9a httpd[13199]: AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.18.0.86. Set the 'Ser...his message
Apr 10 11:28:31 41d129b3de9a systemd[1]: Started The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.

# 解决方法
vi /etc/httpd/conf/httpd.conf 加入一句 ServerName localhost:80

Apache 测试页面

Apache 测试页意味着:

Apache 测试页,意味着您的服务器已正确配置并可以使用。从技术上讲,此页面是首次安装 Apache Web 服务器时的默认索引页面。

那么如何使 Apache 测试页消失呢:

只需打开/var/www/index.html 文件并对其进行修改或删除文件(尽管它可能会触发新的错误)。在 Red Hat Enterprise Linux/CentOS/Fedora Core 下,重命名或删除文件/etc/httpd/conf.d/welcome.conf 以确保您没有看到 Apache 测试页。

您现在可以将内容添加到目录/var/www/html/中。请注意,在您这样做之前,访问您网站的用户将看到默认页面,而不是您的内容。要防止使用此页面,请遵循文件/etc/httpd/conf.d/welcome.conf 中的说明。

常用命令

1
2
3

# 查看 Apache日志
/var/log/httpd

附件

参考资源

使用 & 简单配置 & 复杂配置 & 参数详解 & 相关说明

问题

Apache has not been designed to serve pages while running as root

问题说明:

root 用户下无法启动 httpd 服务

日志信息:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[root@cf8d90d17e9a ~]# systemctl status httpd.service
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
Active: failed (Result: exit-code) since Mon 2019-12-16 02:06:58 UTC; 25s ago
Docs: man:httpd(8)
man:apachectl(8)
Process: 9553 ExecStop=/bin/kill -WINCH ${MAINPID} (code=exited, status=1/FAILURE)
Process: 9552 ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND (code=exited, status=1/FAILURE)
Main PID: 9552 (code=exited, status=1/FAILURE)

Dec 16 02:06:58 cf8d90d17e9a systemd[1]: Starting The Apache HTTP Server...
Dec 16 02:06:58 cf8d90d17e9a httpd[9552]: AH00526: Syntax error on line 1 of /etc/httpd/conf.d/fdm.conf:
Dec 16 02:06:58 cf8d90d17e9a httpd[9552]: Error:\tApache has not been designed to serve pages while\n\trunning as root. There are known ...
Dec 16 02:06:58 cf8d90d17e9a systemd[1]: httpd.service: main process exited, code=exited, status=1/FAILURE
Dec 16 02:06:58 cf8d90d17e9a kill[9553]: kill: cannot find process ""
Dec 16 02:06:58 cf8d90d17e9a systemd[1]: httpd.service: control process exited, code=exited status=1
Dec 16 02:06:58 cf8d90d17e9a systemd[1]: Failed to start The Apache HTTP Server.
Dec 16 02:06:58 cf8d90d17e9a systemd[1]: Unit httpd.service entered failed state.
Dec 16 02:06:58 cf8d90d17e9a systemd[1]: httpd.service failed.

解决方法:

用于 /etc/httpd/conf.d/fdm.conf 配置中 User 和 Group 不能为root,修改为其他用户即可

1
2
3
4
5
6
7
8
9
10
User fdm
Group fdm
<VirtualHost *:80>
DocumentRoot /home/fdm/web/fdm
<Directory /home/fdm/web/fdm>
options Indexes MultiViews
AllowOverride all
Allow from all
</Directory>
</VirtualHost>

Forbidden You don’t have permission to access xxx.html

问题说明:

文件权限不足

解决方法 1:

1
2
3
4
5
# 一层层授权
chmod +x /dira
chmod +x /dira/dirb/
chmod +x /dira/dirb/index.html
# 或者 直接 chmod 777 -R /dira/dirb/index.html

解决方法 2:

修改配置文件/etc/httpd/conf/httpd.conf

1
DocumentRoot "/home/fdm/web/fdm"

解决方法 3:
修改配置文件/etc/httpd/conf/httpd.conf

1
2
3
4
5
# 注释如下代码
105 # <Directory />
106 # AllowOverride none
107 # Require all denied
108 # </Directory>

以上情况,不同问题解决方法不同,可以叠加使用。

Failed to connect to bus: No such file or directory

1
2
3
# Docker中安装 apache
[root@6431285efdee conf.d]# /bin/systemctl restart httpd.service
Failed to connect to bus: No such file or directory

参考链接:https://github.com/microsoft/WSL/issues/2941

1
2
3
# 解决方案 启动dbus
$ sudo mkdir -p /run/dbus
$ sudo dbus-daemon --system
1
2
3
4
5
6
7
8
[root@6431285efdee conf.d]# /bin/systemctl restart httpd.service
Failed to restart httpd.service: Failed to activate service 'org.freedesktop.systemd1': timed out (service_start_timeout=25000ms)
See system logs and 'systemctl status httpd.service' for details.

[root@6431285efdee conf.d]# systemctl status httpd.service
Failed to get properties: Connection timed out