查找“apache2.service:失败,结果为‘退出代码’。”和“无法启动 Apache HTTP 服务器。”的原因。

查找“apache2.service:失败,结果为‘退出代码’。”和“无法启动 Apache HTTP 服务器。”的原因。

我有一个 VirtualBox 设置,带有一个 Ubuntu 机器 20.04(在 Vagrant 设置里面),当我启动它时,apache2 服务器不会自动启动。

启动机器后,当我通过 SSH 进入机器并写入:时sudo service apache2 status,我看到了以下内容:

● apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
     Active: failed (Result: exit-code) since Sun 2022-04-03 21:42:41 CEST; 35s ago
       Docs: https://httpd.apache.org/docs/2.4/

Apr 03 21:42:41 vagrant apachectl[777]: (2)No such file or directory: AH02291: Cannot access directory '/host/logs/apache2/sites/' for error>
Apr 03 21:42:41 vagrant apachectl[777]: (2)No such file or directory: AH02291: Cannot access directory '/host/logs/apache2/sites/' for error>
Apr 03 21:42:41 vagrant apachectl[777]: (2)No such file or directory: AH02291: Cannot access directory '/host/logs/apache2/sites/' for error>
Apr 03 21:42:41 vagrant apachectl[777]: (2)No such file or directory: AH02291: Cannot access directory '/host/logs/apache2/sites/' for error>
Apr 03 21:42:41 vagrant apachectl[777]: AH00014: Configuration check failed
Apr 03 21:42:41 vagrant apachectl[759]: Action 'start' failed.
Apr 03 21:42:41 vagrant apachectl[759]: The Apache error log may have more information.
Apr 03 21:42:41 vagrant systemd[1]: apache2.service: Control process exited, code=exited, status=1/FAILURE
Apr 03 21:42:41 vagrant systemd[1]: apache2.service: Failed with result 'exit-code'.
Apr 03 21:42:41 vagrant systemd[1]: Failed to start The Apache HTTP Server.

它看起来像是切断了这条线,因为我认为它以一条被切断的路径结束:所以它说(加粗字体是我所缺少的):

... 在 /et 定义的 vhost 的错误日志c/啦啦/啦啦

问题1:这个状态在哪里,打印了什么sudo service apache2 status,它是从哪里拉出来的(这样我才能看到该行的其余部分)?回答:它取自:/var/log/syslog

问题2:为什么我的 Apache2 在虚拟机启动时不想启动?

如果我通过 SSH 进入机器并输入:sudo service apache2 start- 那么它apache每次都会启动。每次都必须手动执行此操作,这很烦人。


解决方案尝试 1:错误和访问日志

它们都是空的,如下所示:

$ tail -n 100 /var/log/apache2/error.log /var/log/apache2/access.log

# RESULT:
==> /var/log/apache2/error.log <==

==> /var/log/apache2/access.log <==

解决方案尝试 2:尝试自动启动 apache2

我在这里找到了一个解决方案:如何在启动时启动 apache2-server,内容如下:

sudo update-rc.d apache2 defaults

然后运行vagrant halt​​。vagrant up但出现相同的错误。


解决方案尝试 3:玩/etc/init.d/apache

我尝试了一下,开始是这样的:

# Goto dir
cd /etc/init.d/

# See man-pages
man apache2

但我的尝试均无成功。


解决方案尝试 4:确保/host/logs/apache2/sites/存在

我可以看到它存在。以下是该文件夹的权限:

站点文件夹


解决方案尝试 5:检查 AppArmor

我以前从未听说过这个。但如果我运行以下命令:

$ sudo systemctl status apparmor

# Result:
● apparmor.service - Load AppArmor profiles
     Loaded: loaded (/lib/systemd/system/apparmor.service; enabled; vendor preset: enabled)
     Active: active (exited) since Fri 2022-04-01 12:28:20 CEST; 1h 7min ago

如果我运行以下命令:

$ sudo apparmor_status

# Result: 
apparmor module is loaded.
15 profiles are loaded.
15 profiles are in enforce mode.
   /usr/bin/man
   /usr/lib/NetworkManager/nm-dhcp-client.action
   /usr/lib/NetworkManager/nm-dhcp-helper
   /usr/lib/connman/scripts/dhclient-script
   /usr/lib/snapd/snap-confine
   /usr/lib/snapd/snap-confine//mount-namespace-capture-helper
   /usr/sbin/haveged
   /usr/sbin/mysqld
   /usr/sbin/tcpdump
   /{,usr/}sbin/dhclient
   lsb_release
   man_filter
   man_groff
   nvidia_modprobe
   nvidia_modprobe//kmod
0 profiles are in complain mode.
2 processes have profiles defined.
2 processes are in enforce mode.
   /usr/sbin/haveged (533)
   /usr/sbin/mysqld (692)
0 processes are in complain mode.
0 processes are unconfined but have a profile defined.

解决方案尝试 6:WU-TANG 的下方建议

这看起来是个非常可行的解决方案。我确实尝试了很多次,但遗憾的是没有成功。

当我写下以下内容时,结果是这样的systemctl list-units --type=mount --all

  UNIT                          LOAD   ACTIVE SUB     DESCRIPTION
---------------------------------------------------------------------
  -.mount                       loaded active mounted Root Mount
  boot.mount                    loaded active mounted /boot
  dev-hugepages.mount           loaded active mounted Huge Pages File System
  dev-mqueue.mount              loaded active mounted POSIX Message Queue File System
  host.mount                    loaded active mounted /host
  proc-fs-nfsd.mount            loaded active mounted NFSD configuration filesystem
  run-rpc_pipefs.mount          loaded active mounted RPC Pipe File System
  run-user-1000.mount           loaded active mounted /run/user/1000
  sys-fs-fuse-connections.mount loaded active mounted FUSE Control File System
  sys-kernel-config.mount       loaded active mounted Kernel Configuration File System
  sys-kernel-debug.mount        loaded active mounted Kernel Debug File System
  sys-kernel-tracing.mount      loaded active mounted Kernel Trace File System
  var-nfs.mount                 loaded active mounted /var/nfs

LOAD   = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB    = The low-level unit activation state, values depend on unit type.

13 loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use 'systemctl list-unit-files'.

当我写下:sudo vim /lib/systemd/system/apache2.service我看到了这个:

[Unit]
Description=The Apache HTTP Server
After=network.target remote-fs.target nss-lookup.target
Documentation=https://httpd.apache.org/docs/2.4/

[Service]
Type=forking
Environment=APACHE_STARTED_BY_SYSTEMD=true
ExecStart=/usr/sbin/apachectl start
ExecStop=/usr/sbin/apachectl stop
ExecReload=/usr/sbin/apachectl graceful
PrivateTmp=true
Restart=on-abort

[Install]
WantedBy=multi-user.target

After=...我尝试多次尝试该行,之后我做了一些vagrant halt && vagrant up事后处理。无论我做什么,问题仍然存在。以下是After=...我尝试过的不同行:

# Add host.mount
After=network.target remote-fs.target nss-lookup.target host.mount

# Try host.target, since all the other ones are ending with .target
After=network.target remote-fs.target nss-lookup.target host.target

# Try adding the units that had to do with NFS and NFSD
After=network.target remote-fs.target nss-lookup.target host.mount var-nfs.mount proc-fs-nfsd.mount 

# Try adding the units that had to do with NFS and NFSD, but ending with .target
After=network.target remote-fs.target nss-lookup.target host.target var-nfs.target proc-fs-nfsd.target 

# Same thing as the two lines above, but with changed order
After=host.mount var-nfs.mount proc-fs-nfsd.mount network.target remote-fs.target nss-lookup.target 

After=host.target var-nfs.target proc-fs-nfsd.target network.target remote-fs.target nss-lookup.target 

仍然...启动 VM 时,apache 服务器启动时出现本文开头显示的错误。

更新:已添加vboxadd.service到“After=...”行

我还尝试了慷慨的 WU-TANG 所添加的建议,它使我的“After=...”行看起来像这样:

# Attempt1:
After=host.mount vboxadd.service network.target remote-fs.target nss-lookup.target

# Attempt2:
After=network.target remote-fs.target nss-lookup.target host.mount vboxadd.service

apache但是当我运行时它仍然没有启动vagrant up


快速解决

对于其他面临此问题的人,我找到了这个快速修复解决方案。方法是将其添加到 Vagrantfile 中,就在最后一个end, 之前(因此它位于Vagrant.configure( '2' ) do |config|:-if 语句内):

# To quick-fix apache not starting, when starting the box
# Source: https://askubuntu.com/a/1123052/332403
config.vm.provision :shell, inline: 'sudo systemctl restart apache2', run: 'always'

这看起来像是一个“胶带”解决方案。我希望能够“正确”地修复它,就像 WU-TANG 一直走的方向一样。

答案1

在 Apache 启动之前,您需要确保发生一些事情。

挂载自定义日志目录/分区Apache 启动。

systemctl list-units --type=mount希望在“描述”下向您显示未及时安装的分区...注意“单元”输出中的名称。

systemctl status apache2为您提供服务文件位置(如您所见)。编辑该文件,(在“已加载:”执行以下命令(在输出中): 并设置 apache 在挂载该目录后启动。

sudo vim /lib/systemd/system/apache2.service
编辑“之后”行包括注意的“单元”日志的挂载分区的名称...(如果存在多个单元,则用空格分隔)

如果没有 Guest Additions,则无法在 VirtualBox 客户端中安装共享驱动器。请确保在启动 Apache 之前已启动 Guest Additions。此外,在“之后”行,附加vboxadd.service

保存并退出

journalctl -xe可能回答了您的第一个问题。

相关内容