【问题标题】:Ubuntu 14.04: Apache 2.4.7 virtualhost not working/redirectingUbuntu 14.04:Apache 2.4.7 虚拟主机不工作/重定向
【发布时间】:2014-07-10 06:07:57
【问题描述】:

我在我的 Ubuntu 14.04 机器上安装了 Apache 2.4.7,但我的一些虚拟主机不同意我的看法。我有 5 个要运行的虚拟主机;其中 3 个有效,2 个无效。两个不起作用的 .conf 文件是:

002-tmpnet.conf

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html/tmpnet
    ServerName  tmpnet
    <Directory />
        Options FollowSymLinks
        AllowOverride All
    </Directory>
    <Directory /var/www/html/tmpnet/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        #Order allow,deny
        #allow from all
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

003-tmpcom.conf

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html/tmpcom
    ServerName  tmpcom
    <Directory />
        Options FollowSymLinks
        AllowOverride All
    </Directory>
    <Directory /var/www/html/tmpcom>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        #Order allow,deny
        #allow from all
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

这两个都返回

500 Internal server errors

当试图拜访他们时。如果我通过 localhost(即 localhost/html/tmpnet)访问它们,它工作得非常好,只是在尝试使用虚拟主机时不行。

我的主机文件是:

127.0.0.1   localhost
127.0.1.1   Eagle
127.0.1.1   tmpcom
127.0.1.1   tmpbiz
127.0.1.1   tmporg
127.0.1.1   tmpnet
127.0.1.1   thatsmybrick

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

而我的 apache2.conf 是:

Mutex file:${APACHE_LOCK_DIR} default
PidFile ${APACHE_PID_FILE}
Timeout 300
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5

# These need to be set in /etc/apache2/envvars
User ${APACHE_RUN_USER}
Group ${APACHE_RUN_GROUP}

HostnameLookups on
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
IncludeOptional mods-enabled/*.load
IncludeOptional mods-enabled/*.conf
Include ports.conf

<Directory />
    Options FollowSymLinks
    AllowOverride All
    Require all denied
</Directory>

<Directory /usr/share>
    AllowOverride All
    Require all granted
</Directory>

<Directory /var/www/>
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

AccessFileName .htaccess

<FilesMatch "^\.ht">
    Require all denied
</FilesMatch>

LogFormat "%v:%p %h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" vhost_combined
LogFormat "%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %O" common
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent

IncludeOptional conf-enabled/*.conf

IncludeOptional sites-enabled/*.conf

我已经坚持了很长时间了,我非常感谢一些帮助。任何指向正确方向的东西都会令人惊叹。

谢谢

【问题讨论】:

    标签: apache ubuntu virtualhost


    【解决方案1】:

    这可能有点晚了,不确定您是否找到了解决方案,但这就是我所做的。在花了很多时间修改我的配置文件和调整我的虚拟主机之后,我决定将 a2dissite 000... 作为默认站点并重新加载 apache。一旦我这样做了,重定向就会像预期的那样工作。我不知道当您在服务器上有多个站点时这将如何展开,如果它会默认为第一个加载的虚拟主机或者......我总是认为 apache 必须有一个默认主机,我可以那就错了。

    【讨论】:

    • 谢谢!!!我运行了a2dissite 000-default.conf 并将.conf 添加到我现有的文件中,它成功了!
    【解决方案2】:

    我不仅必须禁用默认设置:

    sudo a2dissite 000-default.conf
    

    但我还必须将 apache 重新加载为 sudo - 没有 sudo 它无法正确重新加载

    sudo service apache2 reload
    

    【讨论】:

      【解决方案3】:

      我遇到了这个问题,结果我不得不禁用默认虚拟主机。

      sudo a2dissite 000-default.conf
      

      【讨论】:

        【解决方案4】:

        你还在面对这个问题吗?

        您是否查看了位于 /var/log/apache2/error.log 的错误日志。错误日志是不言自明的,可以很好地导致问题。

        我机器上的错误显示了这一点(错误声明的后半部分)

        .htaccess: 无效命令“RewriteEngine”,可能拼写错误或 由未包含在服务器配置中的模块定义

        在我的情况下,Apache 模块重写未启用。我启用了该模块,瞧,虚拟主机设置生效了。

        我还看到您正在使用“AllowOverride All”语句,这表明您正在尝试使用重写模块。如果尚未启用模块“重写”,请使用命令 sudo a2enmod rewrite 然后重新启动 apache2 sudo /etc/init.d/apache2 restart

        看看有没有帮助!

        【讨论】:

          【解决方案5】:

          如果您是从精确 (12.04) 升级,那么您需要将 .conf 扩展名添加到您的虚拟主机配置文件中

          例如,以前你有这个配置

          /etc/apache2/sites-available/project
          

          那么你需要添加 .conf 扩展名,

          /etc/apache2/sites-available/project.conf
          

          当然你必须在启用站点的文件夹中创建一个新的符号链接,cd 到启用站点的文件夹和

          sudo ln -s ../sites-available/project.conf 
          

          重启apache就完成了。

          【讨论】:

            【解决方案6】:

            我正在测试相同的设置:

            • 带有 mod_rewrite 的 Apache 2.4.7(sudo a2enable rewrite)

            • Ubuntu 14.04 LTS

            在使用基本 LAMP (see this link) 进行全新安装时,我在可用站点上使用以下内容(启用站点的软链接)

            <VirtualHost *:80>
              ServerName test.cubeinspire.com
              DocumentRoot /home/test/public_html  
              CustomLog /home/test/log/access_log combined
              ErrorLog /home/test/log/error_log
              <Directory /home/test/public_html>
                Order allow,deny
                Allow from all
                Require all granted
              </Directory>
            </VirtualHost>
            

            只有使用该代码才能正常工作。 (注意 Require all grant 这是 Apache 2.4.7 的新要求)

            当我添加以下代码时:

            <VirtualHost *:80>
              ServerName redirectme.cubeinspire.com
              Options +FollowSymLinks
              RewriteEngine on
              Redirect 301 / http://test.cubeinspire.com/
            <Directory "/home/test/public_html">
              AllowOverride None
              Require all granted
            </Directory>
            </VirtualHost>
            

            然后,当我访问 redirectme.cubeinspire.com 时,我陷入了 301 重定向循环。 这是在以前的 Apache 版本上运行的非常简单的代码,文档中没有太多说明。

            所以我猜那个版本还有一些错误,我不鼓励在生产服务器上使用它。

            【讨论】:

              【解决方案7】:

              嗯,重点是:

              IncludeOptional sites-enabled/*.conf

              这意味着它将包含文件名末尾带有.conf的虚拟主机文件。

              重命名它们,或更改指令。

              Ciao, 杰尔玛

              【讨论】:

              • 我认为你没有读过这个问题。他的文件被命名为 .conf,但仍未加载。
              猜你喜欢
              • 1970-01-01
              • 2015-02-22
              • 2016-04-13
              • 2016-01-22
              • 2013-08-29
              • 2015-06-22
              • 1970-01-01
              • 2018-12-21
              相关资源
              最近更新 更多