本地主机工作正常,但其他创建的虚拟主机不工作 - 显示 404 页面错误

问题描述

enter image description here

我的本地主机工作正常,但我的虚拟主机不工作。我创建的虚拟主机给了我一个“404 页面未找到”错误 这是我的 testing.conf 文件的屏幕截图

Here's the screenshot of my test.conf file

当我在页面刷新后检查 apache2 错误日志给我一个“404 页面错误”时,我得到一个没有内容文件,这意味着没有错误。但, 当我重新启动 apache 时,我在 apache2 错误日志中得到了这个。

[Thu Jul 08 18:08:47.037483 2021] [mpm_prefork:notice] [pid 242161] AH00163: Apache/2.4.41 (Ubuntu) 配置——恢复正常操作 [Thu Jul 08 18:08:47.037571 2021] [core:notice] [pid 242161] AH00094:命令行:'/usr/sbin/apache2'

解决方法

这里有几点需要注意:

  1. “当我在页面刷新后检查 apache2 错误日志时出现 404 页面错误,我得到一个没有内容的文件,这意味着没有错误”
    ⇢ 这不一定准确。您的 testing.conf 文件正在将错误日志写入 /var/www/testing/error.log,这不是 Apache 期望写入日志的位置。您可能需要通过 AppArmor 授予 Apache 更多权限或将日志写入正确的 /var/log/apache 目录。
  2. “当我重新启动 apache 时,我在 apache2 错误日志中得到了这个”
    ⇢ 这很正常。这是 Apache 说“服务器已重新启动”的方式。

需要注意的一些项目:

  1. 您的虚拟主机配置文件已指定端口 8081。Apache 是否配置为侦听此端口上的流量?
  2. 您的配置文件中没有 <Directory> 记录。主机可以访问 /var/www/testing 吗?

如果您想让虚拟主机响应默认端口 80 上的流量,请考虑将您的配置文件如下:

<VirtualHost *:80>
        ServerAdmin enels@socials.net
        DocumentRoot /var/www/testing/public_html

        ServerName testing
        ServerAlias testing *.testing testing.*
        DirectoryIndex index.php index.html

        ErrorLog ${APACHE_LOG_DIR}/testing-error.log
        CustomLog ${APACHE_LOG_DIR}/testing-access.log combined

        <Directory /var/www/testing/public_html>
               Options FollowSymLinks
               AllowOverride All

               Order Allow,Deny
               Allow From All
        </Directory>
</VirtualHost>

您可能不想要 ServerAlias 行,但是,如果您决定拥有子域或伪 TLD(例如 api.testing 或 testing.local),这将捕获请求并相应地引导它们。